0

We use the following method to group related products based on their category: https://bitbucket.org/magstaging/groupedrelatedproduct/src/master/

Works perfect, thanks to Herve, but we want to extend this code by also adding the cross sell items on the product page.

So what we want to is to display cross sell products on the product page in the same way, when they exists. Best solution would be to use the same template file for both. So that we can check if related or otherwise cross sell exists.

How can we achieve that?

1
  • 1
    What have you tried? Are you stuck on a specific part?
    – Akif
    Jan 10, 2022 at 11:37

1 Answer 1

1

the template and layout are the only files from the modules that you need to modify:

  • the layout file currently only mentions related reference and it needs on top to mention either product.info.upsell or checkout.cart.crosssell. My advice is to use upsell on product page instead of cross sells as cross sells is for cart page?

  • then, the template file can be modified and the modification may come from the core file vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml

the lines below are what you should focus onto:

case 'upsell':
        /** @var \Magento\Catalog\Block\Product\ProductList\Upsell $block */
        if ($exist = count($block->getItemCollection()->getItems())) {
            $type = 'upsell';
            $class = $type;

            $image = 'upsell_products_list';
            $title = __('We found other products you might like!');
            $items = $block->getItemCollection()->getItems();
            $limit = $block->getItemLimit('upsell');
            $shuffle = 0;
            $isWeightedRandom = 0;

            $showAddTo = false;
            $showCart = false;
            $templateType = null;
            $description = false;
            $canItemsAddToCart = false;
        }
        break;    
case 'crosssell-rule':
            /** @var \Magento\Catalog\Block\Product\ProductList\Crosssell $block */
            if ($exist = $block->hasItems()) {
                $type = 'crosssell';
                $class = $type;
    
                $image = 'cart_cross_sell_products';
                $title = __('More Choices:');
                $items = $block->getItemCollection();
    
                $showAddTo = true;
                $showCart = true;
                $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
                $description = false;
                $canItemsAddToCart = false;
            }
            break;
    
        case 'crosssell':
            /** @var \Magento\Catalog\Block\Product\ProductList\Crosssell $block */
            if ($exist = count($block->getItems())) {
                $type = 'crosssell';
                $class = $type;
    
                $image = 'cart_cross_sell_products';
                $title = __('More Choices:');
                $items = $block->getItems();
    
                $showAddTo = true;
                $showCart = true;
                $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
                $description = false;
                $canItemsAddToCart = false;
            }
            break;

these blocks may need to be part of your custom list.phtml template

1
  • Thanks Herve! How exactly should this layout file look like, to also implement upsell?
    – JGeer
    Jan 21, 2022 at 12:31

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.