Display Product collection issue in Magento 2Remove add to cart button for some productsdisplay product list in product view pagemagento show product collection by idMagento 2: add fields to products collectionMaximum price display first collectionCustom product collection on list.phtml do not outputs price and product nameProduct grid display issueMagento 2 collection loop issueHow to create custom form in Magento 2.2.3List Product Collection into template file in Magento 2

Did the IBM PC use the 8088's NMI line?

Where to place an artificial gland in the human body?

The Sword in the Stone

Convert every file from JPEG to GIF in terminal

Decreasing star size

How can I prevent corporations from growing their own workforce?

Can the 2019 UA Artificer's Returning Weapon and Radiant Weapon infusions stack on the same weapon?

What is the difference between 1/3, 1/2, and full casters?

Is a fighting a fallen friend with the help of a redeemed villain story too much for one book

Why/when is AC-DC-AC conversion superior to direct AC-Ac conversion?

Assuring luggage isn't lost with short layover

What does "see" in "the Holy See" mean?

How do professional electronic musicians/sound engineers combat listening fatigue?

How do I address my Catering staff subordinate seen eating from a chafing dish before the customers?

How do campaign rallies gain candidates votes?

what happens if i forgot to feed my sourdough starter?

Is there anything wrong with Thrawn?

AC contactor 1 pole or 2?

Anybody know what this small Nintendo stand is for?

High income, sudden windfall

Why isn't there a serious attempt at creating a third mass-appeal party in the US?

Explain why watch 'jobs' does not work but watch 'ps' work?

Writing a clean implementation of Rock, Paper, Scissors game in c++

Spoken encryption



Display Product collection issue in Magento 2


Remove add to cart button for some productsdisplay product list in product view pagemagento show product collection by idMagento 2: add fields to products collectionMaximum price display first collectionCustom product collection on list.phtml do not outputs price and product nameProduct grid display issueMagento 2 collection loop issueHow to create custom form in Magento 2.2.3List Product Collection into template file in Magento 2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have a product collection and i have used below code to display in my template file as follows.



 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>


Here is my getProductBysku function



 public function getProductBySku($sku)

return $this->productRepository->get($sku);// injecting this class in my block file MagentoCatalogApiProductRepositoryInterface $productRepository,




The above code working fine if all of the SKUs are simple products,



I need to prepare for configurable products also. Can we get the product info
and display here config products with all the variations as swatches.



All I am looking here is how to display configurable products here exactly like in the category list page with the swatches being shown.



Where all the color and size swatches shows with clickable option.



Can anyone help me to implement this one?



Thanks in Advance!!










share|improve this question
























  • What is not working / showing exactly? My guess is that only price and the toCartUrl is not working, can you confirm that?

    – Arno Vande Cappelle
    Jul 15 at 10:28











  • @ArnoVandeCappelle, If any of the sku in array config product, I need to list that product with all combination selection like in default category list page. where color and size shown.

    – jafar pinjar
    Jul 15 at 10:30











  • @ArnoVandeCappelle, Are you clear with it now?

    – jafar pinjar
    Jul 15 at 10:34











  • Share your $block->getProductBySku($productSku); code

    – Chirag Patel
    Jul 17 at 10:41











  • @ChiragPatel, I have updated the code

    – jafar pinjar
    Jul 17 at 10:48

















2















I have a product collection and i have used below code to display in my template file as follows.



 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>


Here is my getProductBysku function



 public function getProductBySku($sku)

return $this->productRepository->get($sku);// injecting this class in my block file MagentoCatalogApiProductRepositoryInterface $productRepository,




The above code working fine if all of the SKUs are simple products,



I need to prepare for configurable products also. Can we get the product info
and display here config products with all the variations as swatches.



All I am looking here is how to display configurable products here exactly like in the category list page with the swatches being shown.



Where all the color and size swatches shows with clickable option.



Can anyone help me to implement this one?



Thanks in Advance!!










share|improve this question
























  • What is not working / showing exactly? My guess is that only price and the toCartUrl is not working, can you confirm that?

    – Arno Vande Cappelle
    Jul 15 at 10:28











  • @ArnoVandeCappelle, If any of the sku in array config product, I need to list that product with all combination selection like in default category list page. where color and size shown.

    – jafar pinjar
    Jul 15 at 10:30











  • @ArnoVandeCappelle, Are you clear with it now?

    – jafar pinjar
    Jul 15 at 10:34











  • Share your $block->getProductBySku($productSku); code

    – Chirag Patel
    Jul 17 at 10:41











  • @ChiragPatel, I have updated the code

    – jafar pinjar
    Jul 17 at 10:48













2












2








2








I have a product collection and i have used below code to display in my template file as follows.



 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>


Here is my getProductBysku function



 public function getProductBySku($sku)

return $this->productRepository->get($sku);// injecting this class in my block file MagentoCatalogApiProductRepositoryInterface $productRepository,




The above code working fine if all of the SKUs are simple products,



I need to prepare for configurable products also. Can we get the product info
and display here config products with all the variations as swatches.



All I am looking here is how to display configurable products here exactly like in the category list page with the swatches being shown.



Where all the color and size swatches shows with clickable option.



Can anyone help me to implement this one?



Thanks in Advance!!










share|improve this question
















I have a product collection and i have used below code to display in my template file as follows.



 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>


Here is my getProductBysku function



 public function getProductBySku($sku)

return $this->productRepository->get($sku);// injecting this class in my block file MagentoCatalogApiProductRepositoryInterface $productRepository,




The above code working fine if all of the SKUs are simple products,



I need to prepare for configurable products also. Can we get the product info
and display here config products with all the variations as swatches.



All I am looking here is how to display configurable products here exactly like in the category list page with the swatches being shown.



Where all the color and size swatches shows with clickable option.



Can anyone help me to implement this one?



Thanks in Advance!!







magento2 product product-collection product-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 23 at 11:11







jafar pinjar

















asked Jul 15 at 10:25









jafar pinjarjafar pinjar

1,0584 silver badges24 bronze badges




1,0584 silver badges24 bronze badges












  • What is not working / showing exactly? My guess is that only price and the toCartUrl is not working, can you confirm that?

    – Arno Vande Cappelle
    Jul 15 at 10:28











  • @ArnoVandeCappelle, If any of the sku in array config product, I need to list that product with all combination selection like in default category list page. where color and size shown.

    – jafar pinjar
    Jul 15 at 10:30











  • @ArnoVandeCappelle, Are you clear with it now?

    – jafar pinjar
    Jul 15 at 10:34











  • Share your $block->getProductBySku($productSku); code

    – Chirag Patel
    Jul 17 at 10:41











  • @ChiragPatel, I have updated the code

    – jafar pinjar
    Jul 17 at 10:48

















  • What is not working / showing exactly? My guess is that only price and the toCartUrl is not working, can you confirm that?

    – Arno Vande Cappelle
    Jul 15 at 10:28











  • @ArnoVandeCappelle, If any of the sku in array config product, I need to list that product with all combination selection like in default category list page. where color and size shown.

    – jafar pinjar
    Jul 15 at 10:30











  • @ArnoVandeCappelle, Are you clear with it now?

    – jafar pinjar
    Jul 15 at 10:34











  • Share your $block->getProductBySku($productSku); code

    – Chirag Patel
    Jul 17 at 10:41











  • @ChiragPatel, I have updated the code

    – jafar pinjar
    Jul 17 at 10:48
















What is not working / showing exactly? My guess is that only price and the toCartUrl is not working, can you confirm that?

– Arno Vande Cappelle
Jul 15 at 10:28





What is not working / showing exactly? My guess is that only price and the toCartUrl is not working, can you confirm that?

– Arno Vande Cappelle
Jul 15 at 10:28













@ArnoVandeCappelle, If any of the sku in array config product, I need to list that product with all combination selection like in default category list page. where color and size shown.

– jafar pinjar
Jul 15 at 10:30





@ArnoVandeCappelle, If any of the sku in array config product, I need to list that product with all combination selection like in default category list page. where color and size shown.

– jafar pinjar
Jul 15 at 10:30













@ArnoVandeCappelle, Are you clear with it now?

– jafar pinjar
Jul 15 at 10:34





@ArnoVandeCappelle, Are you clear with it now?

– jafar pinjar
Jul 15 at 10:34













Share your $block->getProductBySku($productSku); code

– Chirag Patel
Jul 17 at 10:41





Share your $block->getProductBySku($productSku); code

– Chirag Patel
Jul 17 at 10:41













@ChiragPatel, I have updated the code

– jafar pinjar
Jul 17 at 10:48





@ChiragPatel, I have updated the code

– jafar pinjar
Jul 17 at 10:48










5 Answers
5






active

oldest

votes


















1














 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">

$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($product->getStoreId(), $product);


if($product->getTypeId() == "configurable"):
$productTypeInstance = $_objectManager->get('MagentoConfigurableProductModelProductTypeConfigurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $attribute):
$attributeId = $attribute["attribute_id"];
?>
<div class="control">
<span><?=$attribute["label"]?></span>
<select name="super_attribute[<?=$attributeId?>]" id="select_<?=$attributeId?>" class=" required super-attribute-selectadmin__control-select" title="<?=$attribute["label"] ?>" aria-required="true">
<option value="">-- Please Select --</option>
<?php foreach ($attribute["options"] as $attrOption):?>
<option value="<?=$attrOption["value"]?>" ><?= $attrOption["label"]?></option>
<?php endforeach;?>
</select>
</div>
<?php endforeach;?>
<?php endif;?>

<?php
$customOptions = $_objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
foreach ($customOptions as $option):
$optionId = $option->getOptionId();
?>
<div class="control">
<span><?=$option->getTitle()?></span>
<select name="options[<?php echo $optionId?>]" id="select_<?php echo $optionId?>" class=" required product-custom-option admin__control-select" title="<?=$option->getTitle()?>" data-selector="options[<?php echo $optionId?>]" aria-required="true">
<option value="">-- Please Select --</option>
<?php
foreach ($option->getValues() as $value) :
$optionData = $value->getData();
$optionTypeId = $optionData["option_type_id"];
$optionPrice = $optionData["price"];
$optionTitle = $optionData["title"];
?>
<option value="<?=$optionTypeId?>" price="<?= $optionPrice?>"><?= $optionTitle?></option>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>

<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>





share|improve this answer








New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

    – jafar pinjar
    Jul 23 at 9:01


















0














You can check module-configurable-product in Magento



You can override configurable.phtml file to your module.




vendor/magento/module-configurable-product/view/frontend/templates/product/view/type/options/configurable.phtml




As i said in comments, you can check condition like this



$product->getTypeId()=="configurable"



and show your html according to the product.



Hope It helps.






share|improve this answer

























  • hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

    – jafar pinjar
    Jul 18 at 8:01











  • hello @Mohit, can you please brief me how can i use your answer as per my requirement?

    – jafar pinjar
    Jul 22 at 7:42











  • I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

    – jafar pinjar
    Jul 22 at 8:04












  • you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

    – Mohit Rane
    Jul 22 at 9:36











  • yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

    – jafar pinjar
    Jul 22 at 9:38


















0














Please check with below code to do, get configurable products collection with options.



<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');

$repository = $obj->create('MagentoCatalogModelProductRepository');
$product = $repository->getById('67');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$options = array();

foreach($data as $attr)
foreach($attr as $p)
$options[$p['sku']][$p['attribute_code']] = $p['option_title'];




foreach($options as $sku =>$d)
$pr = $repository->get($sku);
foreach($d as $k => $v)
echo $k.' - '.$v.' ';
echo ' : '.$pr->getPrice()."n";






share|improve this answer























  • hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

    – jafar pinjar
    Jul 18 at 8:43



















0














Try following way...



Create an extension simple and my code add in your helper file after you can get all data in phtml file



<?php
namespace CompanyModuleHelper;

use MagentoCatalogModelResourceModelCategoryCollectionFactory;

class Data extends MagentoFrameworkAppHelperAbstractHelper

/**
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
private $productCollection;

public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
)
parent::__construct($context);
$this->productCollection = $productCollection;


public function getProductCollection()

$productIds = [1,2,3,4];
$collection = $this->productCollection->create()
->addAttributeToSelect('*')
->addStoreFilter()
->addFieldToFilter(
'entity_id',
['in' => $productIds]
);

// sort categories by some value
if ($sortBy)
$collection->addOrderField($sortBy);


// select certain number of categories
if ($pageSize)
$collection->setPageSize($pageSize);


return $collection;








share|improve this answer























  • helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

    – jafar pinjar
    Jul 19 at 13:41











  • yes but you can add your product id in my collection after you can showing products like listing page

    – Rakesh Donga
    Jul 19 at 14:27











  • yes, pls share the code to display collection like category page

    – jafar pinjar
    Jul 19 at 14:33



















0














You might be able to inject a MagentoSwatchesBlockProductRendererConfigurable in to your block file (which is responsible for rendering the image/label variations of a product). You can then, in your AbstractBlock::_toHtml() function add something like so:



$this->configurable->setProduct($this->getProductBySku('my-sku');
$this->setChild('swatches', $this->configurable);
return parent::_toHtml();


You can then output the data of swatches in your template by using <?= $block->getChildHtml('swatches'); ?>






share|improve this answer























  • hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

    – jafar pinjar
    Jul 22 at 5:56












  • hello @Adam, can you please elaborate?

    – jafar pinjar
    Jul 22 at 10:03













Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f282089%2fdisplay-product-collection-issue-in-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">

$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($product->getStoreId(), $product);


if($product->getTypeId() == "configurable"):
$productTypeInstance = $_objectManager->get('MagentoConfigurableProductModelProductTypeConfigurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $attribute):
$attributeId = $attribute["attribute_id"];
?>
<div class="control">
<span><?=$attribute["label"]?></span>
<select name="super_attribute[<?=$attributeId?>]" id="select_<?=$attributeId?>" class=" required super-attribute-selectadmin__control-select" title="<?=$attribute["label"] ?>" aria-required="true">
<option value="">-- Please Select --</option>
<?php foreach ($attribute["options"] as $attrOption):?>
<option value="<?=$attrOption["value"]?>" ><?= $attrOption["label"]?></option>
<?php endforeach;?>
</select>
</div>
<?php endforeach;?>
<?php endif;?>

<?php
$customOptions = $_objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
foreach ($customOptions as $option):
$optionId = $option->getOptionId();
?>
<div class="control">
<span><?=$option->getTitle()?></span>
<select name="options[<?php echo $optionId?>]" id="select_<?php echo $optionId?>" class=" required product-custom-option admin__control-select" title="<?=$option->getTitle()?>" data-selector="options[<?php echo $optionId?>]" aria-required="true">
<option value="">-- Please Select --</option>
<?php
foreach ($option->getValues() as $value) :
$optionData = $value->getData();
$optionTypeId = $optionData["option_type_id"];
$optionPrice = $optionData["price"];
$optionTitle = $optionData["title"];
?>
<option value="<?=$optionTypeId?>" price="<?= $optionPrice?>"><?= $optionTitle?></option>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>

<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>





share|improve this answer








New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

    – jafar pinjar
    Jul 23 at 9:01















1














 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">

$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($product->getStoreId(), $product);


if($product->getTypeId() == "configurable"):
$productTypeInstance = $_objectManager->get('MagentoConfigurableProductModelProductTypeConfigurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $attribute):
$attributeId = $attribute["attribute_id"];
?>
<div class="control">
<span><?=$attribute["label"]?></span>
<select name="super_attribute[<?=$attributeId?>]" id="select_<?=$attributeId?>" class=" required super-attribute-selectadmin__control-select" title="<?=$attribute["label"] ?>" aria-required="true">
<option value="">-- Please Select --</option>
<?php foreach ($attribute["options"] as $attrOption):?>
<option value="<?=$attrOption["value"]?>" ><?= $attrOption["label"]?></option>
<?php endforeach;?>
</select>
</div>
<?php endforeach;?>
<?php endif;?>

<?php
$customOptions = $_objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
foreach ($customOptions as $option):
$optionId = $option->getOptionId();
?>
<div class="control">
<span><?=$option->getTitle()?></span>
<select name="options[<?php echo $optionId?>]" id="select_<?php echo $optionId?>" class=" required product-custom-option admin__control-select" title="<?=$option->getTitle()?>" data-selector="options[<?php echo $optionId?>]" aria-required="true">
<option value="">-- Please Select --</option>
<?php
foreach ($option->getValues() as $value) :
$optionData = $value->getData();
$optionTypeId = $optionData["option_type_id"];
$optionPrice = $optionData["price"];
$optionTitle = $optionData["title"];
?>
<option value="<?=$optionTypeId?>" price="<?= $optionPrice?>"><?= $optionTitle?></option>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>

<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>





share|improve this answer








New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

    – jafar pinjar
    Jul 23 at 9:01













1












1








1







 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">

$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($product->getStoreId(), $product);


if($product->getTypeId() == "configurable"):
$productTypeInstance = $_objectManager->get('MagentoConfigurableProductModelProductTypeConfigurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $attribute):
$attributeId = $attribute["attribute_id"];
?>
<div class="control">
<span><?=$attribute["label"]?></span>
<select name="super_attribute[<?=$attributeId?>]" id="select_<?=$attributeId?>" class=" required super-attribute-selectadmin__control-select" title="<?=$attribute["label"] ?>" aria-required="true">
<option value="">-- Please Select --</option>
<?php foreach ($attribute["options"] as $attrOption):?>
<option value="<?=$attrOption["value"]?>" ><?= $attrOption["label"]?></option>
<?php endforeach;?>
</select>
</div>
<?php endforeach;?>
<?php endif;?>

<?php
$customOptions = $_objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
foreach ($customOptions as $option):
$optionId = $option->getOptionId();
?>
<div class="control">
<span><?=$option->getTitle()?></span>
<select name="options[<?php echo $optionId?>]" id="select_<?php echo $optionId?>" class=" required product-custom-option admin__control-select" title="<?=$option->getTitle()?>" data-selector="options[<?php echo $optionId?>]" aria-required="true">
<option value="">-- Please Select --</option>
<?php
foreach ($option->getValues() as $value) :
$optionData = $value->getData();
$optionTypeId = $optionData["option_type_id"];
$optionPrice = $optionData["price"];
$optionTitle = $optionData["title"];
?>
<option value="<?=$optionTypeId?>" price="<?= $optionPrice?>"><?= $optionTitle?></option>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>

<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>





share|improve this answer








New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









 <ol class="products list items product-items">
<?php
$productCollectionSku = array('111', '222', '333', '444', '555', '666'); // these are the comma separated skus.

foreach($productCollectionSku as $productSku)
$product = $block->getProductBySku($productSku);
$productName = $product->getName();
$shortDescription = $product->getshortDescription();
$productUrl = $product->getProductUrl();
$productId = $product->getId();

$productPrice = $product->getPrice();
$addtoCartUrl = $block->getAddToCartUrlForProduct($productSku);
?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-grid">
<a href="<?php echo $productUrl; ?>" class="product photo product-item-photo">
<span class="product-image-wrapper">
<img class="product-image-photo" src="<?php echo $productImage; ?>" width="240" height="300" alt="<?php echo $productName; ?>"></span>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name">
<a class="product-item-link" href="<?php echo $productUrl; ?>">
<?php echo $productName; ?></a>
</strong>
<div class="price-box price-final_price">
<span class="price-container price-final_price">
<span class="price-wrapper ">
<span class="price"><?php echo $productPrice; ?></span>
</span>
</span>
</div>
<form action="<?php echo $addtoCartUrl; ?>" method="post" class="best-items">
<?php echo $block->getBlockHtml('formkey')?>
<input type="hidden" name="productId" value="<?php echo $productId; ?>">

$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($product->getStoreId(), $product);


if($product->getTypeId() == "configurable"):
$productTypeInstance = $_objectManager->get('MagentoConfigurableProductModelProductTypeConfigurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $attribute):
$attributeId = $attribute["attribute_id"];
?>
<div class="control">
<span><?=$attribute["label"]?></span>
<select name="super_attribute[<?=$attributeId?>]" id="select_<?=$attributeId?>" class=" required super-attribute-selectadmin__control-select" title="<?=$attribute["label"] ?>" aria-required="true">
<option value="">-- Please Select --</option>
<?php foreach ($attribute["options"] as $attrOption):?>
<option value="<?=$attrOption["value"]?>" ><?= $attrOption["label"]?></option>
<?php endforeach;?>
</select>
</div>
<?php endforeach;?>
<?php endif;?>

<?php
$customOptions = $_objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($product);
foreach ($customOptions as $option):
$optionId = $option->getOptionId();
?>
<div class="control">
<span><?=$option->getTitle()?></span>
<select name="options[<?php echo $optionId?>]" id="select_<?php echo $optionId?>" class=" required product-custom-option admin__control-select" title="<?=$option->getTitle()?>" data-selector="options[<?php echo $optionId?>]" aria-required="true">
<option value="">-- Please Select --</option>
<?php
foreach ($option->getValues() as $value) :
$optionData = $value->getData();
$optionTypeId = $optionData["option_type_id"];
$optionPrice = $optionData["price"];
$optionTitle = $optionData["title"];
?>
<option value="<?=$optionTypeId?>" price="<?= $optionPrice?>"><?= $optionTitle?></option>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>

<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</form>
</div>
</div>
</li>
<?php ?>
</ol>






share|improve this answer








New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this answer



share|improve this answer






New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








answered Jul 22 at 21:00









Sagar GaikwadSagar Gaikwad

1296 bronze badges




1296 bronze badges




New contributor



Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Sagar Gaikwad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

    – jafar pinjar
    Jul 23 at 9:01

















  • hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

    – jafar pinjar
    Jul 23 at 9:01
















hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

– jafar pinjar
Jul 23 at 9:01





hello @Sagar, can you please update me the code for swatches, i am looking for swatches to show over there

– jafar pinjar
Jul 23 at 9:01













0














You can check module-configurable-product in Magento



You can override configurable.phtml file to your module.




vendor/magento/module-configurable-product/view/frontend/templates/product/view/type/options/configurable.phtml




As i said in comments, you can check condition like this



$product->getTypeId()=="configurable"



and show your html according to the product.



Hope It helps.






share|improve this answer

























  • hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

    – jafar pinjar
    Jul 18 at 8:01











  • hello @Mohit, can you please brief me how can i use your answer as per my requirement?

    – jafar pinjar
    Jul 22 at 7:42











  • I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

    – jafar pinjar
    Jul 22 at 8:04












  • you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

    – Mohit Rane
    Jul 22 at 9:36











  • yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

    – jafar pinjar
    Jul 22 at 9:38















0














You can check module-configurable-product in Magento



You can override configurable.phtml file to your module.




vendor/magento/module-configurable-product/view/frontend/templates/product/view/type/options/configurable.phtml




As i said in comments, you can check condition like this



$product->getTypeId()=="configurable"



and show your html according to the product.



Hope It helps.






share|improve this answer

























  • hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

    – jafar pinjar
    Jul 18 at 8:01











  • hello @Mohit, can you please brief me how can i use your answer as per my requirement?

    – jafar pinjar
    Jul 22 at 7:42











  • I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

    – jafar pinjar
    Jul 22 at 8:04












  • you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

    – Mohit Rane
    Jul 22 at 9:36











  • yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

    – jafar pinjar
    Jul 22 at 9:38













0












0








0







You can check module-configurable-product in Magento



You can override configurable.phtml file to your module.




vendor/magento/module-configurable-product/view/frontend/templates/product/view/type/options/configurable.phtml




As i said in comments, you can check condition like this



$product->getTypeId()=="configurable"



and show your html according to the product.



Hope It helps.






share|improve this answer















You can check module-configurable-product in Magento



You can override configurable.phtml file to your module.




vendor/magento/module-configurable-product/view/frontend/templates/product/view/type/options/configurable.phtml




As i said in comments, you can check condition like this



$product->getTypeId()=="configurable"



and show your html according to the product.



Hope It helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 18 at 8:15

























answered Jul 18 at 7:57









Mohit RaneMohit Rane

98518 bronze badges




98518 bronze badges












  • hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

    – jafar pinjar
    Jul 18 at 8:01











  • hello @Mohit, can you please brief me how can i use your answer as per my requirement?

    – jafar pinjar
    Jul 22 at 7:42











  • I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

    – jafar pinjar
    Jul 22 at 8:04












  • you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

    – Mohit Rane
    Jul 22 at 9:36











  • yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

    – jafar pinjar
    Jul 22 at 9:38

















  • hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

    – jafar pinjar
    Jul 18 at 8:01











  • hello @Mohit, can you please brief me how can i use your answer as per my requirement?

    – jafar pinjar
    Jul 22 at 7:42











  • I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

    – jafar pinjar
    Jul 22 at 8:04












  • you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

    – Mohit Rane
    Jul 22 at 9:36











  • yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

    – jafar pinjar
    Jul 22 at 9:38
















hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

– jafar pinjar
Jul 18 at 8:01





hi @Mohit, is any chance to update as per my collection, I already have product object just need to display it, the above one is not complete answer..

– jafar pinjar
Jul 18 at 8:01













hello @Mohit, can you please brief me how can i use your answer as per my requirement?

– jafar pinjar
Jul 22 at 7:42





hello @Mohit, can you please brief me how can i use your answer as per my requirement?

– jafar pinjar
Jul 22 at 7:42













I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

– jafar pinjar
Jul 22 at 8:04






I need to show swatches there, the approach u given is for drop downs, Also pls brief me your answer..

– jafar pinjar
Jul 22 at 8:04














you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

– Mohit Rane
Jul 22 at 9:36





you can check in default magento to see how it's working in core magento /var/www/html/magento_226/vendor/magento/module-configurable-product/view/frontend/layout also check the module-catalog module

– Mohit Rane
Jul 22 at 9:36













yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

– jafar pinjar
Jul 22 at 9:38





yes i checked over there, that module is for showing option in dropdown but in my case i am using swatches. What code i need to use to make it work like default list page

– jafar pinjar
Jul 22 at 9:38











0














Please check with below code to do, get configurable products collection with options.



<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');

$repository = $obj->create('MagentoCatalogModelProductRepository');
$product = $repository->getById('67');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$options = array();

foreach($data as $attr)
foreach($attr as $p)
$options[$p['sku']][$p['attribute_code']] = $p['option_title'];




foreach($options as $sku =>$d)
$pr = $repository->get($sku);
foreach($d as $k => $v)
echo $k.' - '.$v.' ';
echo ' : '.$pr->getPrice()."n";






share|improve this answer























  • hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

    – jafar pinjar
    Jul 18 at 8:43
















0














Please check with below code to do, get configurable products collection with options.



<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');

$repository = $obj->create('MagentoCatalogModelProductRepository');
$product = $repository->getById('67');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$options = array();

foreach($data as $attr)
foreach($attr as $p)
$options[$p['sku']][$p['attribute_code']] = $p['option_title'];




foreach($options as $sku =>$d)
$pr = $repository->get($sku);
foreach($d as $k => $v)
echo $k.' - '.$v.' ';
echo ' : '.$pr->getPrice()."n";






share|improve this answer























  • hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

    – jafar pinjar
    Jul 18 at 8:43














0












0








0







Please check with below code to do, get configurable products collection with options.



<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');

$repository = $obj->create('MagentoCatalogModelProductRepository');
$product = $repository->getById('67');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$options = array();

foreach($data as $attr)
foreach($attr as $p)
$options[$p['sku']][$p['attribute_code']] = $p['option_title'];




foreach($options as $sku =>$d)
$pr = $repository->get($sku);
foreach($d as $k => $v)
echo $k.' - '.$v.' ';
echo ' : '.$pr->getPrice()."n";






share|improve this answer













Please check with below code to do, get configurable products collection with options.



<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');

$repository = $obj->create('MagentoCatalogModelProductRepository');
$product = $repository->getById('67');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$options = array();

foreach($data as $attr)
foreach($attr as $p)
$options[$p['sku']][$p['attribute_code']] = $p['option_title'];




foreach($options as $sku =>$d)
$pr = $repository->get($sku);
foreach($d as $k => $v)
echo $k.' - '.$v.' ';
echo ' : '.$pr->getPrice()."n";







share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 18 at 8:37









Anas MansuriAnas Mansuri

1,1251 silver badge16 bronze badges




1,1251 silver badge16 bronze badges












  • hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

    – jafar pinjar
    Jul 18 at 8:43


















  • hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

    – jafar pinjar
    Jul 18 at 8:43

















hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

– jafar pinjar
Jul 18 at 8:43






hello, @Anas, yes above code will give the options that' fine, if you look into my code that will list collection with all info, same like how can i display in the same template, I am looking exactly for that, I need to fetch all option info if there is config product and display that like in category page, Pls dont share how to get the option info and all, that i can get it. Thanks. Help me to display the config product exactly like in category page, This is what i am looking for..

– jafar pinjar
Jul 18 at 8:43












0














Try following way...



Create an extension simple and my code add in your helper file after you can get all data in phtml file



<?php
namespace CompanyModuleHelper;

use MagentoCatalogModelResourceModelCategoryCollectionFactory;

class Data extends MagentoFrameworkAppHelperAbstractHelper

/**
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
private $productCollection;

public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
)
parent::__construct($context);
$this->productCollection = $productCollection;


public function getProductCollection()

$productIds = [1,2,3,4];
$collection = $this->productCollection->create()
->addAttributeToSelect('*')
->addStoreFilter()
->addFieldToFilter(
'entity_id',
['in' => $productIds]
);

// sort categories by some value
if ($sortBy)
$collection->addOrderField($sortBy);


// select certain number of categories
if ($pageSize)
$collection->setPageSize($pageSize);


return $collection;








share|improve this answer























  • helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

    – jafar pinjar
    Jul 19 at 13:41











  • yes but you can add your product id in my collection after you can showing products like listing page

    – Rakesh Donga
    Jul 19 at 14:27











  • yes, pls share the code to display collection like category page

    – jafar pinjar
    Jul 19 at 14:33
















0














Try following way...



Create an extension simple and my code add in your helper file after you can get all data in phtml file



<?php
namespace CompanyModuleHelper;

use MagentoCatalogModelResourceModelCategoryCollectionFactory;

class Data extends MagentoFrameworkAppHelperAbstractHelper

/**
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
private $productCollection;

public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
)
parent::__construct($context);
$this->productCollection = $productCollection;


public function getProductCollection()

$productIds = [1,2,3,4];
$collection = $this->productCollection->create()
->addAttributeToSelect('*')
->addStoreFilter()
->addFieldToFilter(
'entity_id',
['in' => $productIds]
);

// sort categories by some value
if ($sortBy)
$collection->addOrderField($sortBy);


// select certain number of categories
if ($pageSize)
$collection->setPageSize($pageSize);


return $collection;








share|improve this answer























  • helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

    – jafar pinjar
    Jul 19 at 13:41











  • yes but you can add your product id in my collection after you can showing products like listing page

    – Rakesh Donga
    Jul 19 at 14:27











  • yes, pls share the code to display collection like category page

    – jafar pinjar
    Jul 19 at 14:33














0












0








0







Try following way...



Create an extension simple and my code add in your helper file after you can get all data in phtml file



<?php
namespace CompanyModuleHelper;

use MagentoCatalogModelResourceModelCategoryCollectionFactory;

class Data extends MagentoFrameworkAppHelperAbstractHelper

/**
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
private $productCollection;

public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
)
parent::__construct($context);
$this->productCollection = $productCollection;


public function getProductCollection()

$productIds = [1,2,3,4];
$collection = $this->productCollection->create()
->addAttributeToSelect('*')
->addStoreFilter()
->addFieldToFilter(
'entity_id',
['in' => $productIds]
);

// sort categories by some value
if ($sortBy)
$collection->addOrderField($sortBy);


// select certain number of categories
if ($pageSize)
$collection->setPageSize($pageSize);


return $collection;








share|improve this answer













Try following way...



Create an extension simple and my code add in your helper file after you can get all data in phtml file



<?php
namespace CompanyModuleHelper;

use MagentoCatalogModelResourceModelCategoryCollectionFactory;

class Data extends MagentoFrameworkAppHelperAbstractHelper

/**
* @var MagentoCatalogModelResourceModelProductCollectionFactory
*/
private $productCollection;

public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollection
)
parent::__construct($context);
$this->productCollection = $productCollection;


public function getProductCollection()

$productIds = [1,2,3,4];
$collection = $this->productCollection->create()
->addAttributeToSelect('*')
->addStoreFilter()
->addFieldToFilter(
'entity_id',
['in' => $productIds]
);

// sort categories by some value
if ($sortBy)
$collection->addOrderField($sortBy);


// select certain number of categories
if ($pageSize)
$collection->setPageSize($pageSize);


return $collection;









share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 19 at 13:14









Rakesh DongaRakesh Donga

2,8596 silver badges24 bronze badges




2,8596 silver badges24 bronze badges












  • helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

    – jafar pinjar
    Jul 19 at 13:41











  • yes but you can add your product id in my collection after you can showing products like listing page

    – Rakesh Donga
    Jul 19 at 14:27











  • yes, pls share the code to display collection like category page

    – jafar pinjar
    Jul 19 at 14:33


















  • helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

    – jafar pinjar
    Jul 19 at 13:41











  • yes but you can add your product id in my collection after you can showing products like listing page

    – Rakesh Donga
    Jul 19 at 14:27











  • yes, pls share the code to display collection like category page

    – jafar pinjar
    Jul 19 at 14:33

















helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

– jafar pinjar
Jul 19 at 13:41





helllo @Rakesh, My question is to display the collection not to get the collection. Already i have collection just i need to display like product list as same like category page

– jafar pinjar
Jul 19 at 13:41













yes but you can add your product id in my collection after you can showing products like listing page

– Rakesh Donga
Jul 19 at 14:27





yes but you can add your product id in my collection after you can showing products like listing page

– Rakesh Donga
Jul 19 at 14:27













yes, pls share the code to display collection like category page

– jafar pinjar
Jul 19 at 14:33






yes, pls share the code to display collection like category page

– jafar pinjar
Jul 19 at 14:33












0














You might be able to inject a MagentoSwatchesBlockProductRendererConfigurable in to your block file (which is responsible for rendering the image/label variations of a product). You can then, in your AbstractBlock::_toHtml() function add something like so:



$this->configurable->setProduct($this->getProductBySku('my-sku');
$this->setChild('swatches', $this->configurable);
return parent::_toHtml();


You can then output the data of swatches in your template by using <?= $block->getChildHtml('swatches'); ?>






share|improve this answer























  • hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

    – jafar pinjar
    Jul 22 at 5:56












  • hello @Adam, can you please elaborate?

    – jafar pinjar
    Jul 22 at 10:03















0














You might be able to inject a MagentoSwatchesBlockProductRendererConfigurable in to your block file (which is responsible for rendering the image/label variations of a product). You can then, in your AbstractBlock::_toHtml() function add something like so:



$this->configurable->setProduct($this->getProductBySku('my-sku');
$this->setChild('swatches', $this->configurable);
return parent::_toHtml();


You can then output the data of swatches in your template by using <?= $block->getChildHtml('swatches'); ?>






share|improve this answer























  • hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

    – jafar pinjar
    Jul 22 at 5:56












  • hello @Adam, can you please elaborate?

    – jafar pinjar
    Jul 22 at 10:03













0












0








0







You might be able to inject a MagentoSwatchesBlockProductRendererConfigurable in to your block file (which is responsible for rendering the image/label variations of a product). You can then, in your AbstractBlock::_toHtml() function add something like so:



$this->configurable->setProduct($this->getProductBySku('my-sku');
$this->setChild('swatches', $this->configurable);
return parent::_toHtml();


You can then output the data of swatches in your template by using <?= $block->getChildHtml('swatches'); ?>






share|improve this answer













You might be able to inject a MagentoSwatchesBlockProductRendererConfigurable in to your block file (which is responsible for rendering the image/label variations of a product). You can then, in your AbstractBlock::_toHtml() function add something like so:



$this->configurable->setProduct($this->getProductBySku('my-sku');
$this->setChild('swatches', $this->configurable);
return parent::_toHtml();


You can then output the data of swatches in your template by using <?= $block->getChildHtml('swatches'); ?>







share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 19 at 16:00









Adam MellenAdam Mellen

715 bronze badges




715 bronze badges












  • hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

    – jafar pinjar
    Jul 22 at 5:56












  • hello @Adam, can you please elaborate?

    – jafar pinjar
    Jul 22 at 10:03

















  • hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

    – jafar pinjar
    Jul 22 at 5:56












  • hello @Adam, can you please elaborate?

    – jafar pinjar
    Jul 22 at 10:03
















hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

– jafar pinjar
Jul 22 at 5:56






hello @Adam, this i got the idea about this, but of you could update to my question that will be a great help, is it possible to update to my question with the code i used there.

– jafar pinjar
Jul 22 at 5:56














hello @Adam, can you please elaborate?

– jafar pinjar
Jul 22 at 10:03





hello @Adam, can you please elaborate?

– jafar pinjar
Jul 22 at 10:03

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f282089%2fdisplay-product-collection-issue-in-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?