0

I am overriding default.phtml and remove.phtml here by using core block but i need to use my custom block also.how do i achieve?

<referenceBlock name="checkout.cart.item.renderers">
  <block class="Magento\Checkout\Block\Cart\Item\Renderer" name="checkout.cart.item.renderers.default" as="default" template="Custom_Module::cart/item/default.phtml">
       <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.default.actions" as="actions">
             <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Custom_Module::cart/item/renderer/actions/remove.phtml"/>
          </block>
     </block>        
</referenceBlock>

like this

 <referenceBlock name="checkout.cart.item.renderers">
  <block class="Magento\Checkout\Block\Cart\Item\Renderer" name="checkout.cart.item.renderers.default" as="default" template="Custom_Module::cart/item/default.phtml">
       <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.default.actions" as="actions">
             <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Custom_Module::cart/item/renderer/actions/remove.phtml"/>
             **<block class="Custom\Module\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.custom.actions.remove" template="Custom_Module::cart/item/renderer/actions/remove.phtml"/>**
          </block>
     </block>        
</referenceBlock>

basically i wanted to use 2 block classes for single remove.phtml file.

Thanks.

1
  • 1
    I think you should use viewModel instead of second block. Commented Nov 5, 2019 at 12:42

3 Answers 3

1

If you extend the original class "Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" on your custom class, you'll be able to use both of them.

use Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove;

class MyCustomClass extends Remove {

  /**
   * @var Cart
   */
  protected $cartHelper;

  /**
   * @param Template\Context $context
   * @param Cart $cartHelper
   * @param array $data
   * @codeCoverageIgnore
   */

  public function __construct(
    Template\Context $context,
    Cart $cartHelper,
    array $data = []
  ) {
    $this->cartHelper = $cartHelper;
    parent::__construct($context, $data);
  }

  //Your custom methods
} 

If you still need to use more classes, use constructor-injection and add them to your custom class.

0

Maybe, you can create one custom class and you use two class depencity injection in the class constructor.

for example;

<?php

namespace Magento\Checkout\Block\Cart\Item\Renderer\Actions;

class MyRemove extends Remove {
    protected $remove;
    protected $customRemove;

    public function __construct(
        Magento\Checkout\Block\Cart\Item\Renderer\Actions $remove,
        Custom\Module\Block\Cart\Item\Renderer\Actions\Remove $customRemove;
    ) {
        $this->remove = $remove;
        $this->customRemove = $customRemove;
    }


}
0

Finally i got the solution for these kind of problems. so we can use view_model for appending other class without effecting existing one. see the example here.

<referenceBlock name="checkout.cart.item.renderers.custom.actions.remove">
        <arguments>
            <argument name="view_model" xsi:type="object">Custom\Module\ViewModel\Cart\Items\Remove</argument>
        </arguments>
 </referenceBlock>

i put this into my layout file checkout_cart_index.xml and created the Remove.php on Custom\Module\ViewModel\Cart\Items this location

Hope it will help to someone

Happy Coding !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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