1

I tried to create a layout and phtml template and a block for add something to cart view page.

I added something, but When I want to choose referenceBlock the content disappear.

Actually I'm going to add some column like price and number to cart table view, but I think it could not find the block! I appreciate any help of you

Here is my code:

MagenCheckout/CheckoutField/Block/Productpoint.php`

<?php

namespace MagenCheckout\CheckoutField\Block;

class Productpoint extends \Magento\Framework\View\Element\Template
{

   public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = [])
    {
       parent::__construct($context, $data);
    }
}

MagenCheckout/CheckoutField/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MagenCheckout_CheckoutField" setup_version="0.0.8">
    <sequence>
        <module name="Magento_Customer"/>
    </sequence>
</module>

MagenCheckout/CheckoutField/view/frontend/layout/checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
    <referenceBlock name="checkout.cart.form">
        <block class="MagenCheckout\CheckoutField\Block\Productpoint" name="cart_item_addional_info" template="MagenCheckout_CheckoutField::checkout/cart/productpoint.phtml"/>
    </referenceBlock>
   </body>
</page>

MagenCheckout/CheckoutField/view/frontend/templates/checkout/cart/productpoint.phtml

<p>
   <?php echo 'Your Discount'; ?>
</p>

MagenCheckout/CheckoutField/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'MagenCheckout_CheckoutField',
__DIR__
);
Prince Patel
  • 22,708
  • 10
  • 97
  • 119
M Taheri
  • 55
  • 2
  • 9

1 Answers1

2

You can try this.

  1. Update MagenCheckout/CheckoutField/etc/module.xml to

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="MagenCheckout_CheckoutField" setup_version="0.0.8">
           <sequence>
            <module name="Magento_Customer"/>
            <module name="Magento_Checkout"/>
           </sequence>
        </module>
    </config>
    
  2. Update MagenCheckout/CheckoutField/view/frontend/layout/checkout_cart_index.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
       <body>
           <referenceBlock name="checkout.cart.form">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">MagenCheckout_CheckoutField::cart/form.phtml</argument>
            </action>
            <block class="MagenCheckout\CheckoutField\Block\Productpoint" name="cart_item_addional_info" template="MagenCheckout_CheckoutField::checkout/cart/productpoint.phtml"/>
           </referenceBlock>
       </body>
    </page>
    
  3. Copy vendor/magento/module-checkout/view/frontend/templates/cart/form.phtml to MagenCheckout/CheckoutField/view/frontend/templates/cart/form.phtml, and place this code to the end of file

    <?php echo $block->getChildHtml('cart_item_addional_info'); ?>
    
Quan Le
  • 1,698
  • 1
  • 13
  • 18
  • You mean I should edit form.phtml file in magento core? – M Taheri Sep 26 '17 at 07:39
  • @MTaheri of course not :) you should override it in your own module or in your theme. – Quan Le Sep 26 '17 at 07:45
  • Actually I'm new to magento. :-| Could you please explain more about how should I override form.phtml in my module? – M Taheri Sep 26 '17 at 07:55
  • @MTaheri I've updated the answer, could you try it and tell me the result :) – Quan Le Sep 26 '17 at 08:23
  • quan-le Thank you for your reply, my cart table disappeared now! I don't know the problem – M Taheri Sep 26 '17 at 08:44
  • Problem was the address of this: MagenCheckout_CheckoutField::checkout/cart/form.phtml – M Taheri Sep 26 '17 at 09:04
  • quan-le Now I'm going to add a tag which contains some info of product, but product row of form shows by getItemHtml($_item) ?> and how should I access to tags seperately? – M Taheri Sep 26 '17 at 09:12
  • You could add it by override vendor/magento/module-checkout/view/frontend/templates/cart/item/default.phtml. Please refer to topics about override template file like this topic to do it yourself :) – Quan Le Sep 26 '17 at 09:26
  • Thanks but how could I override child of block with name checkout.cart.item.renderers which has no name and use default.phtml as template in module-checkout/view/frontend/layout/ path – M Taheri Sep 26 '17 at 12:07