4

Magento version 2.3

I want to show a particular category in shopping cart page using programming, instead of using backend widget settings.

I have tried to modify the following files with no luck:

Magento_Checkout/layout/checkout_cart_index.xml

<block type="catalog/product_list" category_id="4" template="catalog/product/list.phtml" />

which returns error, it says that block cannot set type attribute

source

Magento_Checkout/templates/cart.phtml

if ($block->getItemsCount()) {
    echo $block->getChildHtml('with-items');
    $this->getLayout()->createBlock('catalog/product_list')
          ->setData('category_id','$YourCategoryId')
          ->setTemplate('catalog/product/list.phtml')->toHtml(); 
} else {
    echo $block->getChildHtml('no-items');
}

returns error code 500

source

2

Try below code in your

Magento_Checkout/layout/checkout_cart_index.xml

<block type="Magento\Catalog\Block\Product\ListProduct" category_id="4" template="catalog/product/list.phtml" />

Magento_Checkout/templates/cart.phtml:

$YourCategoryId = $this->getData("category_id");
if ($block->getItemsCount()) {
    echo $block->getChildHtml('with-items');
    $this->getLayout()->createBlock('catalog/product_list')
          ->setData('category_id',$YourCategoryId)
          ->setTemplate('catalog/product/list.phtml')->toHtml(); 
} else {
    echo $block->getChildHtml('no-items');
}
1

In addition to display the specific category wise products on Cart page, you need to include following code

vendor/magento/module-checkout/view/frontend/layout/checkout_cart_index.xml (Override the module as per your convinience)

<block class="Magento\Catalog\Block\Product\ListProduct"  name="category.products.list" template="Magento_Catalog::product/list.phtml">
                    <arguments>                            
                        <argument name="category_id" xsi:type="number">3</argument>
                    </arguments>
                </block>

Note :

  1. In Above code we have reused the default product list template, you can Create the separate template for that and modify the code accordingly.
  2. In Above code "3" is the category id, Change the category id as per your requirement.
  • Hi there, I actually wrote a similar code to yours. But I still tried your code just in case. I still get the same error. Class catalog/product_list does not exist AND Invalid block type: catalog/product_list – wltprgm Jun 10 at 8:22
  • I think there is no catalog/product_list in layout folder, that's why it's causing this issue. But I am not sure. – wltprgm Jun 10 at 8:25
  • An answer for another question said that I have to whitelist catalog/product_list, but there is no such settings in backend magento.stackexchange.com/a/98084/80197 – wltprgm Jun 10 at 8:27
0

I suggest you create a Wiget to add product list with a category on Shopping Cart this option without using any custom

enter image description here

Update 2 I follow your activity I got you are using Magento 2 CMS, but you are using Block type and code style of Magento 1. which this option you should use. Magento\Catalog\Block\Product\ListProduct instead of catalog\product_list

<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<action method="setToolbarBlockName">
     <argument name="category_id" xsi:type="number">[category id]</argument>
</action>
</block>

If you need to customer order product collection by own. You need to create new Block extends Magento\Catalog\Block\Product\ListProduct and custom _getProductCollection function.

New Block XML

<block class="Namespace\ModuleName\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
    <action method="setToolbarBlockName">
         <argument name="category_id" xsi:type="number">[category id]</argument>
    </action>
 </block>

Namespace\ModuleName\Block\Product\ListProduct

protected function _getProductCollection()
{
    if ($this->_productCollection === null) {
        $this->_productCollection = $this->initializeProductCollection();
        $this->_productCollection->setOrder('[attribute code]', 'desc');
    }

    return $this->_productCollection;
}
  • I modified my question, I need to solve this programmatically, in order to achieve certain feature – wltprgm Jun 12 at 2:10
  • Updated answer. You need to define Magento Version you have used. – HoangHieu Jun 12 at 2:52
  • What does this sentence means? "If you need to customer order product collection by own." – wltprgm Jun 12 at 3:39
  • How do I create new block? in what folder? app/code, app/design? – wltprgm Jun 12 at 3:39
  • You need to learn how to create block Magento 2 first. – HoangHieu Jun 12 at 4:07

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.