0

I hope someone can help - editing the Magento_Catalog/templates/product/list.phtml most likely buy inserting a new phtml file, how can I list all of the child SKU's of the parent category on the category page?

Example... Parent category - Electrical 8 child categories, 1 of which being Batteries Batteries has 2 products assigned, SKU 1234 and 2345

What I would like, on the Electrical category page, the two SKU's (1234, 2345) listed on the Batteries category card

A visual example... http://i.ibb.co/VM1D222/Example2.jpg

  • Please clear your exact requirement. What I understand is you want to products of parent categories on current category. Right ? OR you want to show product of child categories on current category page ?? – Narendra Vyas Mar 27 at 12:52
  • Ok, So I have a category called Electrical, which has 8 sub categories, one of those sub categories is called Batteries which has 2 products assigned. On the Electrical category page, under the Batteries card, I would like the child SKUs listed. Like this i.ibb.co/VM1D222/Example2.jpg – Ally Gillespie Mar 27 at 12:54
  • Okay now which products do you want to be shown on Electricals (0 as it dont have any products) and on Batteries page (you want to show products associated with Batteries category). Right ? Means do you want to show products of current category – Narendra Vyas Mar 27 at 12:57
  • in question you mentioned "how can I list all of the child SKU's of the parent category on the category page" – Narendra Vyas Mar 27 at 12:57
  • Please see my example visual example i.ibb.co/VM1D222/Example2.jpg – Ally Gillespie Mar 27 at 13:00
0

Try below code:

 <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $current_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
  $parent_category = $current_category->getParentCategory();
  $cat_id = $current_category->getId();   
  $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');         
  $category = $categoryFactory->create()->load($cat_id);         
  $categoryProducts = $category->getProductCollection()
                                 ->addAttributeToSelect('*');

    foreach ($categoryProducts as $product) {

        echo $product->getSku() . ' - ' . $product->getName() . '<br />';
    }
  • That is close, the problem I have now is, I have a category called Electrical, which has no products, under that category I have a category called Batteries which has a product assigned, but every category under Electrical is showing the child SKU, here is a an example... i.ibb.co/7nSTgKw/example.jpg – Ally Gillespie Mar 27 at 12:16
  • what is the relation in Electrical and Batteries – Narendra Vyas Mar 27 at 12:22
  • Electrical is the parent category of Batteries category – Ally Gillespie Mar 27 at 12:24
  • Basically what I am trying to achieve is this... soakandsleep.com/furniture/all - in the source under the TrustPilot widget there is all the SKU's listed (<div class="trustpilot-widget" data-locale="en-US" data-template-id="54d39695764ea907c0f34825" data-businessunit-id="52fe0d35000064000578390b" data-style-height="33px" data-style-width="100%" data-sku="Clerkenwell_furniture_collection_Grouped,FB2-CL,FB3-CL,FB4-CL,FST-CL,FCH-CL,FCHMCL,LWC2FG,bamboo_and_canvas_storage") – Ally Gillespie Mar 27 at 12:27
  • One parent can have N no of children categories you want all child products ?? – Narendra Vyas Mar 27 at 12:32
0

Try this one:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$current_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$parentId = $current_category->getParentCategory()->getId();

$cat_id = $current_category->getId();   
$childCats = $current_category->getChildrenCategories();

$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$category = $categoryFactory->create()->load($parentId);         
$categoryProducts = $category->getProductCollection()
                             ->addAttributeToSelect('*');

// echo $parentId;

// echo 'count -- '.$category->getProductCount();
foreach ($categoryProducts as $product) {

    echo $product->getSku() . ' - ' . $product->getName() . '<br />';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.