2

I've been trying to overwrite the getBackUrl method in the Cart Add controller of the checkout. With a preference I managed without an issue but plugins are a bit more of a challenge.

Here's what I have so far.

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Controller\Cart\Add">
        <plugin name="[namespace]_[module]_checkout_controller_cart_add_before" type="[Namespace]\[Module]\Controller\Checkout\Cart\Plugin" sortOrder="1" />
    </type>
</config>

Controller/Checkout/Cart/Plugin.php

namespace [Namespace]\[Module]\Controller\Checkout\Cart;

class Plugin
{

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_config;

    /**
     * @var \Magento\Framework\Url
     */
    protected $_url;

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Framework\Url $url
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        \Magento\Framework\Url $url
    ) {
        $this->_config = $config;
        $this->_url = $url;
    }

    /**
     * Get resolved back url, rewritten to return checkout URL instead of cart url
     *
     * @param \Magento\Checkout\Controller\Cart\Add $subject
     * @return string
     */
    protected function beforeGetBackUrl(\Magento\Checkout\Controller\Cart\Add $subject)
    {
        return $this->_url->getUrl('some/custom/url');
    }
}

Whatever I try I can't get it to reach the beforeGetBackUrl method.

1

It is possible to plug in to the public non-static methods only: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html#plugin-limit
It is impossible to plug in to the protected, private, static methods.
Also it is impossible to plug in to the public __construct method (constructor).
https://github.com/magento/magento2/issues/2265 https://github.com/magento/magento2/issues/2367 https://github.com/magento/magento2/issues/2202#issuecomment-151599087

| improve this answer | |
  • Wow, how did I not catch that. So in this case I should use a preference? – Sander Mangel Nov 22 '15 at 14:00
  • Preference is evil because you break all other extensions who will try to set a preference for the same class: github.com/magento/magento2/issues/2443 – Mage2.PRO Nov 22 '15 at 14:04
  • Which I understand but in this case I don't see another option if I want to rewrite that method – Sander Mangel Nov 22 '15 at 14:04
  • You need to choose carefully another method to plug in, or handle an event, or report the issue in the official tracker: github.com/magento/magento2/issues/2265#issuecomment-154910716 – Mage2.PRO Nov 22 '15 at 14:05
  • already went for execute :) kinda weird tho, there is a perfect option but is way to limited and an 'evil' option which they shouldn't even have provided imo. Anyway, thanks for the help! – Sander Mangel Nov 22 '15 at 14:07
2

You cannot plugginize getBackUrl, but you can getUrl.

For this you need:

  1. Create virtual type myUrlBulder from \Magento\Framework\Url
  2. Use it as argument for \Magento\Checkout\Controller\Cart\Add
  3. Declare plugin for myUrlBulder virtual type
| improve this answer | |
  • Awesome word: plugginize! So I ended up plugginizing the execute method, sounds easier but I'll try out your method as training – Sander Mangel Nov 22 '15 at 15:50
  • @KAndy:- please see my post where i added virtual type approach but not working please review my code and correct me where i missed – Pradeep Kumar Nov 24 '15 at 6:48
  • @KAndy:- can you post the code how to do it in virtual type i create one more question magento.stackexchange.com/questions/91353/… – Pradeep Kumar Nov 24 '15 at 10:05
0

Can I pluginize execute method or any solution to over ride the execute method of Add.php in cart controllers.

Here is the link of thread I already created few days before but no solution

    Overriding Magento 2.3 Checkout Cart Control Using Plugin using around but not working 

   Overriding add cart controller in magento 2 

Any solution any reference which can work execute will be appreciated

Thanks

| improve this answer | |

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.