3

I wanted to add my own button in the Shopping Cart as I tried to show you in the screenshot.

enter image description here

But I didn't find a tutorial that worked for me. Do you have any idea how I can do this? (I want to make my own extension)

(EDIT) This is what I have done so far:

In the registration.php file:

<?php
  \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_ModuleName',
    __DIR__
 );

In the module.xml file:

<?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="Vendor_ModuleName" setup_version="1.0.0"></module>
</config>

In the Button.php file:

<?php
namespace Vendor\ModuleName\Block;
class Button extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

}

In the checkout_cart_index.xml file:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="cart.summary">
                <block class="Vendor\Module\Block\Button" after="-" name="custom_button" template="Vendor_ModuleName::button.phtml" />
        </referenceContainer>
</page>

In the button.phtml file:

<button>Your Button</button>

I also ran all commands you have told me.

7
  • Are you using a custom theme? Commented Aug 7, 2019 at 12:42
  • No I am not using a custom Theme. I actually did almost nothing in my Magento 2 Website. The only thing I made was my first own Module to learn how to make Modules in Magento. Commented Aug 7, 2019 at 12:44
  • check my answer if u getting any issue let me know
    – Waqar Ali
    Commented Aug 7, 2019 at 12:56
  • now in checkout_cart_index add ModuleName instead of Module in block class
    – Waqar Ali
    Commented Aug 7, 2019 at 13:56
  • well, I am stupid, i will try if it works Commented Aug 7, 2019 at 13:58

2 Answers 2

8

Create a Custom Module as Follows

Vendor\ModuleName\registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_ModuleName',
    __DIR__
);

then

Vendor\ModuleName\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="Vendor_ModuleName" setup_version="1.0.0">
       <sequence>
            <module name="Magento_Checkout"/>
       </sequence>
    </module>
</config>

After that run following Command

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
chmod -R 777 var/ pub/ generated/

Add this is Your

Vendor\ModuleName\view\frontend\layout\checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="cart.summary">
                <block class="Vendor\Module\Block\Button" after="-" name="custom_button" template="Vendor_ModuleName::button.phtml" />
        </referenceContainer>
</page>

Now in

Vendor\ModuleName\Block\Button.php

namespace Vendor\ModuleName\Block;

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

}

then in

Vendor\ModuleName\view\frontend\templates\button.phtml

<button>Your Button</button>
10
  • This is not working, sorry. Any other ideas? Commented Aug 7, 2019 at 12:57
  • is should work. have u create a module??
    – Waqar Ali
    Commented Aug 7, 2019 at 12:58
  • Yes, but now the cart page is empty, any idea what could be the problem? Commented Aug 7, 2019 at 13:05
  • I fixed the page, but i cant see the button, I created Commented Aug 7, 2019 at 13:28
  • check answer now
    – Waqar Ali
    Commented Aug 7, 2019 at 13:33
0

Waqar Ali's answer is almost correct. The registration.php file is missing:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_ModuleName',
__DIR__
);

and the Vendor/ModuleName/Block/Button.php file needs to define namespace:

namespace Vendor\ModuleName\Block;

Don't to forget:

bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush
4
  • I did this and now the page works, but i dont see any button Commented Aug 7, 2019 at 13:27
  • Try checkout.cart.container instead cart.summary . Don't to forget flush cache. Commented Aug 7, 2019 at 14:01
  • Waqar Ali solved it for me, but thanks Commented Aug 7, 2019 at 14:02
  • I stay happy to know :) Commented Aug 7, 2019 at 14:04

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.