1

In my custom module I have to check some conditions and have to disable "Proceed to Checkout" button from the Cart page.

I know it's coming from

magento\vendor\magento\module-checkout\view\frontend\templates\onepage\link.phtml

I can override this but I have to also override block for this. Is there any other way to achieve this?

  • What exactly do you want to achieve here? – Anshu Mishra Aug 22 '18 at 10:39
0

In your custom module or if you have a design template create view -> frontend -> templates -> onepage -> link.phtml and inside just put:

<?php

?>
<?php if ($block->isPossibleOnepageCheckout()):?>

<?php endif?>

This should just not create the button.

0

You can try following code, here I have rewritten the template file in my custom module. app/code/Anshu/Custom/registration.php

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

app/code/Anshu/Custom/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="Anshu_Custom" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Checkout" />
        </sequence>
    </module>
</config>

app/code/Anshu/Custom/view/frontend/layout/checkout_cart_index.xml

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="checkout.cart.methods.onepage.bottom" template="Anshu_Custom::onepage/link.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/Anshu/Custom/view/frontend/templates/onepage/link.phtml

<?php
// @codingStandardsIgnoreFile
?>
<?php if ($block->isPossibleOnepageCheckout()):?>
    <button type="button"
            data-role="proceed-to-checkout"
            title="<?= /* @escapeNotVerified */ __('Proceed to Checkout') ?>"
            data-mage-init='{"Magento_Checkout/js/proceed-to-checkout":{"checkoutUrl":"<?= /* @escapeNotVerified */ $block->getCheckoutUrl() ?>"}}'
            class="action primary checkout<?= ($block->isDisabled()) ? ' disabled' : '' ?>"
            <?php if ($block->isDisabled()):?>disabled="disabled"<?php endif; ?>>
        <span><?= /* @escapeNotVerified */ __('Proceed to Checkout') ?></span>
    </button>
<?php endif?>

I have just copied the code from original core link.phtml file, you can modify it according to your requirement.

  • I am doing exact same thing that you mentioned above, added a code in custom block to check customer group data and disable button based on that check. In my case it disabled the button for all the customers including Guest. I tried cacheable=false in layout xml and it didn't fix it either. What kind of caching issue is it since the button is disabled even after bin/magento cache:flush? – cnu Apr 4 at 17:02
  • @cnu The code is just for over ridding the template file, you need to apply the condition according to your requirement. – Anshu Mishra Apr 4 at 17:08
  • Right, I have the condition and it's working but the button disappears even when data is not there for the condition, so trying to understand if it's a caching or some other problem. – cnu Apr 4 at 18:17
  • @cnu Doesn't seems to be cache issue, I am assuming it will be a condition issue. Kindly check your condition. – Anshu Mishra Apr 5 at 4:50
  • found the issue. It was a typo in one of the variable names in custom Block, hence the button was missing for every customer. – cnu Apr 5 at 14:19
-1

Override the helper function canOnepageCheckout. To achieve this you need to override the following class

Magento\Checkout\Helper\Data

Create your own module and add the following code to your di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Helper\Data" type="Vendor\Module\Helper\Data" />
</config>

And update your logic to the below function:

public function canOnepageCheckout()
{
    return (bool)$this->scopeConfig->getValue('checkout/options/onepage_checkout_enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}

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.