How to override cartaddcontroller in magento 2?How to override cartcontroller in magento 2?How to get rid of Add All To Cart from WishlistHow to Override or Extend protected or parent function in Magento 2 using PluginCart page not Working in magento2Trouble with cart add moduleOverriding add cart moduleMagento 2: How to override newsletter Subscriber modelProceed to checkout redirects to empty cart pageMagento 2: I Want to add multiple product using checkboxMagento 1 with Php 7.2, add to cart error if product have option

Possible to set `foldexpr` using a function reference?

Lock in SQL Server and Oracle

How can I get precisely a certain cubic cm by changing the following factors?

Reverse the word in a string with the same order in javascript

Binary Numbers Magic Trick

Past Perfect Tense

What does "rf" mean in "rfkill"?

What is the range of this combined function?

Is it possible to measure lightning discharges as Nikola Tesla?

Do I have an "anti-research" personality?

Why do computer-science majors learn calculus?

Pressure to defend the relevance of one's area of mathematics

Sci-fi novel series with instant travel between planets through gates. A river runs through the gates

What does YCWCYODFTRFDTY mean?

How to replace the "space symbol" (squat-u) in listings?

What's the metal clinking sound at the end of credits in Avengers: Endgame?

A non-technological, repeating, visible object in the sky, holding its position in the sky for hours

How to determine the actual or "true" resolution of a digital photograph?

Has any spacecraft ever had the ability to directly communicate with civilian air traffic control?

You look catfish vs You look like a catfish

In the time of the mishna, were there Jewish cities without courts?

What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?

A question regarding using the definite article

Is it cheaper to drop cargo drop than to land it?



How to override cartaddcontroller in magento 2?


How to override cartcontroller in magento 2?How to get rid of Add All To Cart from WishlistHow to Override or Extend protected or parent function in Magento 2 using PluginCart page not Working in magento2Trouble with cart add moduleOverriding add cart moduleMagento 2: How to override newsletter Subscriber modelProceed to checkout redirects to empty cart pageMagento 2: I Want to add multiple product using checkboxMagento 1 with Php 7.2, add to cart error if product have option






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I want this function in code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php(this is written in magento 1.9, I want to convert it into magento 2). But I cant add any new function in add controller. Any help will be appreciated.



 public function buyXgetYfree()



$cart = $this->_getCart();
/* check if cart is empty */
if (!$this->_getCart()->getQuote()->getItemsCount())

return;



$buyProductXID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_product_id'));
$buyProductYID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_product_id'));
$buyProductXminQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_required_qty'));
$buyProductXmaxQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_limit_qty'));
$buyProductYDescription = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_description'));

$error="A BuyXGetY Extension cart error was detected!";

try

for($i = 0; $i < count($buyProductXID); $i++)
if (empty($buyProductYDescription[$i]))
$buyProductYDescription[$i]="free gift";

if (empty($buyProductXID[$i]))
$buyProductXID[$i]="0";

if (empty($buyProductYID[$i]))
$buyProductYID[$i]="0";

if (empty($buyProductXminQty[$i]))
$buyProductXminQty[$i]="999";

if (empty($buyProductXmaxQty[$i]))
$buyProductXmaxQty[$i]="0";

if ($buyProductXID[$i] !="0" && $buyProductYID[$i] !="0")
if ($this->isProductYUnique())


$this->buyXgetYfreeCartUpdate((int)$buyProductXID[$i],(int)$buyProductXminQty[$i],(int)$buyProductYID[$i],$buyProductYDescription[$i],(int)$buyProductXmaxQty[$i]);
else
$error = "Error in Buy X configuration - Product Y is not unique across all extension settings.";
throw new Exception($error);
break;





catch (Exception $ex)

$cart->getCheckoutSession()->addError($this->__($error));
$this->sendErrorEmail($error);




di.xml



 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="MagentoCheckoutControllerCartAdd">
<plugin name="buy-x-get-y" type="StwBuyXGetYBuyXGetYPluginControllerCheckoutCartAdd" sortOrder="1" />
</type>
</config>


code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php



 namespace StwBuyXGetYBuyXGetYPluginControllerCheckoutCart;
class Add

public function aroundExecute(MagentoCheckoutControllerCartAdd $add, callable $proceed, OutputInterface $output)

$this->buyXgetYfree();
$returnValue = $proceed();
return $returnValue;

public function buyXgetYfree()

echo "buyXgetYfree"; // this is dummy content











share|improve this question






























    3















    I want this function in code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php(this is written in magento 1.9, I want to convert it into magento 2). But I cant add any new function in add controller. Any help will be appreciated.



     public function buyXgetYfree()



    $cart = $this->_getCart();
    /* check if cart is empty */
    if (!$this->_getCart()->getQuote()->getItemsCount())

    return;



    $buyProductXID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_product_id'));
    $buyProductYID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_product_id'));
    $buyProductXminQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_required_qty'));
    $buyProductXmaxQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_limit_qty'));
    $buyProductYDescription = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_description'));

    $error="A BuyXGetY Extension cart error was detected!";

    try

    for($i = 0; $i < count($buyProductXID); $i++)
    if (empty($buyProductYDescription[$i]))
    $buyProductYDescription[$i]="free gift";

    if (empty($buyProductXID[$i]))
    $buyProductXID[$i]="0";

    if (empty($buyProductYID[$i]))
    $buyProductYID[$i]="0";

    if (empty($buyProductXminQty[$i]))
    $buyProductXminQty[$i]="999";

    if (empty($buyProductXmaxQty[$i]))
    $buyProductXmaxQty[$i]="0";

    if ($buyProductXID[$i] !="0" && $buyProductYID[$i] !="0")
    if ($this->isProductYUnique())


    $this->buyXgetYfreeCartUpdate((int)$buyProductXID[$i],(int)$buyProductXminQty[$i],(int)$buyProductYID[$i],$buyProductYDescription[$i],(int)$buyProductXmaxQty[$i]);
    else
    $error = "Error in Buy X configuration - Product Y is not unique across all extension settings.";
    throw new Exception($error);
    break;





    catch (Exception $ex)

    $cart->getCheckoutSession()->addError($this->__($error));
    $this->sendErrorEmail($error);




    di.xml



     <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="MagentoCheckoutControllerCartAdd">
    <plugin name="buy-x-get-y" type="StwBuyXGetYBuyXGetYPluginControllerCheckoutCartAdd" sortOrder="1" />
    </type>
    </config>


    code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php



     namespace StwBuyXGetYBuyXGetYPluginControllerCheckoutCart;
    class Add

    public function aroundExecute(MagentoCheckoutControllerCartAdd $add, callable $proceed, OutputInterface $output)

    $this->buyXgetYfree();
    $returnValue = $proceed();
    return $returnValue;

    public function buyXgetYfree()

    echo "buyXgetYfree"; // this is dummy content











    share|improve this question


























      3












      3








      3








      I want this function in code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php(this is written in magento 1.9, I want to convert it into magento 2). But I cant add any new function in add controller. Any help will be appreciated.



       public function buyXgetYfree()



      $cart = $this->_getCart();
      /* check if cart is empty */
      if (!$this->_getCart()->getQuote()->getItemsCount())

      return;



      $buyProductXID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_product_id'));
      $buyProductYID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_product_id'));
      $buyProductXminQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_required_qty'));
      $buyProductXmaxQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_limit_qty'));
      $buyProductYDescription = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_description'));

      $error="A BuyXGetY Extension cart error was detected!";

      try

      for($i = 0; $i < count($buyProductXID); $i++)
      if (empty($buyProductYDescription[$i]))
      $buyProductYDescription[$i]="free gift";

      if (empty($buyProductXID[$i]))
      $buyProductXID[$i]="0";

      if (empty($buyProductYID[$i]))
      $buyProductYID[$i]="0";

      if (empty($buyProductXminQty[$i]))
      $buyProductXminQty[$i]="999";

      if (empty($buyProductXmaxQty[$i]))
      $buyProductXmaxQty[$i]="0";

      if ($buyProductXID[$i] !="0" && $buyProductYID[$i] !="0")
      if ($this->isProductYUnique())


      $this->buyXgetYfreeCartUpdate((int)$buyProductXID[$i],(int)$buyProductXminQty[$i],(int)$buyProductYID[$i],$buyProductYDescription[$i],(int)$buyProductXmaxQty[$i]);
      else
      $error = "Error in Buy X configuration - Product Y is not unique across all extension settings.";
      throw new Exception($error);
      break;





      catch (Exception $ex)

      $cart->getCheckoutSession()->addError($this->__($error));
      $this->sendErrorEmail($error);




      di.xml



       <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
      <type name="MagentoCheckoutControllerCartAdd">
      <plugin name="buy-x-get-y" type="StwBuyXGetYBuyXGetYPluginControllerCheckoutCartAdd" sortOrder="1" />
      </type>
      </config>


      code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php



       namespace StwBuyXGetYBuyXGetYPluginControllerCheckoutCart;
      class Add

      public function aroundExecute(MagentoCheckoutControllerCartAdd $add, callable $proceed, OutputInterface $output)

      $this->buyXgetYfree();
      $returnValue = $proceed();
      return $returnValue;

      public function buyXgetYfree()

      echo "buyXgetYfree"; // this is dummy content











      share|improve this question
















      I want this function in code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php(this is written in magento 1.9, I want to convert it into magento 2). But I cant add any new function in add controller. Any help will be appreciated.



       public function buyXgetYfree()



      $cart = $this->_getCart();
      /* check if cart is empty */
      if (!$this->_getCart()->getQuote()->getItemsCount())

      return;



      $buyProductXID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_product_id'));
      $buyProductYID = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_product_id'));
      $buyProductXminQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_required_qty'));
      $buyProductXmaxQty = explode (",",Mage::getStoreConfig('buyxgety_section1/general/productx_limit_qty'));
      $buyProductYDescription = explode (",",Mage::getStoreConfig('buyxgety_section1/general/producty_description'));

      $error="A BuyXGetY Extension cart error was detected!";

      try

      for($i = 0; $i < count($buyProductXID); $i++)
      if (empty($buyProductYDescription[$i]))
      $buyProductYDescription[$i]="free gift";

      if (empty($buyProductXID[$i]))
      $buyProductXID[$i]="0";

      if (empty($buyProductYID[$i]))
      $buyProductYID[$i]="0";

      if (empty($buyProductXminQty[$i]))
      $buyProductXminQty[$i]="999";

      if (empty($buyProductXmaxQty[$i]))
      $buyProductXmaxQty[$i]="0";

      if ($buyProductXID[$i] !="0" && $buyProductYID[$i] !="0")
      if ($this->isProductYUnique())


      $this->buyXgetYfreeCartUpdate((int)$buyProductXID[$i],(int)$buyProductXminQty[$i],(int)$buyProductYID[$i],$buyProductYDescription[$i],(int)$buyProductXmaxQty[$i]);
      else
      $error = "Error in Buy X configuration - Product Y is not unique across all extension settings.";
      throw new Exception($error);
      break;





      catch (Exception $ex)

      $cart->getCheckoutSession()->addError($this->__($error));
      $this->sendErrorEmail($error);




      di.xml



       <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
      <type name="MagentoCheckoutControllerCartAdd">
      <plugin name="buy-x-get-y" type="StwBuyXGetYBuyXGetYPluginControllerCheckoutCartAdd" sortOrder="1" />
      </type>
      </config>


      code StwBuyXGetY BuyXGetY ControllerCheckoutCartAdd.php



       namespace StwBuyXGetYBuyXGetYPluginControllerCheckoutCart;
      class Add

      public function aroundExecute(MagentoCheckoutControllerCartAdd $add, callable $proceed, OutputInterface $output)

      $this->buyXgetYfree();
      $returnValue = $proceed();
      return $returnValue;

      public function buyXgetYfree()

      echo "buyXgetYfree"; // this is dummy content








      magento2 cart overrides controllers addtocart






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 17 '16 at 6:59







      Keerthana

















      asked Aug 17 '16 at 6:32









      KeerthanaKeerthana

      169319




      169319




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Change your plugin controller to following




          class Add

          public function aroundExecute(
          MagentoCheckoutControllerCartAdd $subject,
          Closure $proceed
          )
          $this->buyXgetYfree();
          return $proceed();


          public function buyXgetYfree()

          error_log('buyXgetYfree');




          When you press addtocart button, basically it's not return any 'echo' content. So you can use error_log or magento 2 log system for checking.






          share|improve this answer























          • this solution is not working for me, error log is not displaying.

            – Keerthana
            Aug 22 '16 at 6:09


















          0














          $writer = new ZendLogWriterStream(BP . '/var/log/addToCart.log');
          $logger = new ZendLogLogger();
          $logger->addWriter($writer);
          $logger->debug(__METHOD__);


          Sohel is right, it does not show your echo if you try to add to cart. You can try this one and check your log at var/log/addToCart.log






          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f131716%2fhow-to-override-cart-addcontroller-in-magento-2%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Change your plugin controller to following




            class Add

            public function aroundExecute(
            MagentoCheckoutControllerCartAdd $subject,
            Closure $proceed
            )
            $this->buyXgetYfree();
            return $proceed();


            public function buyXgetYfree()

            error_log('buyXgetYfree');




            When you press addtocart button, basically it's not return any 'echo' content. So you can use error_log or magento 2 log system for checking.






            share|improve this answer























            • this solution is not working for me, error log is not displaying.

              – Keerthana
              Aug 22 '16 at 6:09















            0














            Change your plugin controller to following




            class Add

            public function aroundExecute(
            MagentoCheckoutControllerCartAdd $subject,
            Closure $proceed
            )
            $this->buyXgetYfree();
            return $proceed();


            public function buyXgetYfree()

            error_log('buyXgetYfree');




            When you press addtocart button, basically it's not return any 'echo' content. So you can use error_log or magento 2 log system for checking.






            share|improve this answer























            • this solution is not working for me, error log is not displaying.

              – Keerthana
              Aug 22 '16 at 6:09













            0












            0








            0







            Change your plugin controller to following




            class Add

            public function aroundExecute(
            MagentoCheckoutControllerCartAdd $subject,
            Closure $proceed
            )
            $this->buyXgetYfree();
            return $proceed();


            public function buyXgetYfree()

            error_log('buyXgetYfree');




            When you press addtocart button, basically it's not return any 'echo' content. So you can use error_log or magento 2 log system for checking.






            share|improve this answer













            Change your plugin controller to following




            class Add

            public function aroundExecute(
            MagentoCheckoutControllerCartAdd $subject,
            Closure $proceed
            )
            $this->buyXgetYfree();
            return $proceed();


            public function buyXgetYfree()

            error_log('buyXgetYfree');




            When you press addtocart button, basically it's not return any 'echo' content. So you can use error_log or magento 2 log system for checking.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 17 '16 at 13:21









            Sohel RanaSohel Rana

            23.7k34461




            23.7k34461












            • this solution is not working for me, error log is not displaying.

              – Keerthana
              Aug 22 '16 at 6:09

















            • this solution is not working for me, error log is not displaying.

              – Keerthana
              Aug 22 '16 at 6:09
















            this solution is not working for me, error log is not displaying.

            – Keerthana
            Aug 22 '16 at 6:09





            this solution is not working for me, error log is not displaying.

            – Keerthana
            Aug 22 '16 at 6:09













            0














            $writer = new ZendLogWriterStream(BP . '/var/log/addToCart.log');
            $logger = new ZendLogLogger();
            $logger->addWriter($writer);
            $logger->debug(__METHOD__);


            Sohel is right, it does not show your echo if you try to add to cart. You can try this one and check your log at var/log/addToCart.log






            share|improve this answer



























              0














              $writer = new ZendLogWriterStream(BP . '/var/log/addToCart.log');
              $logger = new ZendLogLogger();
              $logger->addWriter($writer);
              $logger->debug(__METHOD__);


              Sohel is right, it does not show your echo if you try to add to cart. You can try this one and check your log at var/log/addToCart.log






              share|improve this answer

























                0












                0








                0







                $writer = new ZendLogWriterStream(BP . '/var/log/addToCart.log');
                $logger = new ZendLogLogger();
                $logger->addWriter($writer);
                $logger->debug(__METHOD__);


                Sohel is right, it does not show your echo if you try to add to cart. You can try this one and check your log at var/log/addToCart.log






                share|improve this answer













                $writer = new ZendLogWriterStream(BP . '/var/log/addToCart.log');
                $logger = new ZendLogLogger();
                $logger->addWriter($writer);
                $logger->debug(__METHOD__);


                Sohel is right, it does not show your echo if you try to add to cart. You can try this one and check your log at var/log/addToCart.log







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 18 '18 at 6:23









                Adrian PaulAdrian Paul

                145




                145



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f131716%2fhow-to-override-cart-addcontroller-in-magento-2%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form