Programatically add product to basket - Error: The product could not be found












0















Hi guys I have a small code snippet and am trying to add a product to basket in Magento via PHP.



Whatever I try (I've searched for a lot of snippets) I just get "The product could not be found." error. I have dialled my code back to a very short and simple snippet to try and get this working.



Can anyone spot what might be causing this?



require_once 'app/Mage.php';
umask(0);
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));

$cart = Mage::getSingleton('checkout/cart');
$sku="prodsku";
$qty=1;
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
var_dump($product);
$cart->addProduct($product, $qty);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


I am using a var_dump on the product to make sure it is finding it and if I remove the line:



$cart->addProduct($product, $qty);


It then dumps the product information and it all looks fine.



SOLVED: This single product was not physically ADDED to the website under Product > Websites, I also have to change to load by ID rather than SKU as it was throwing up stock errors.










share|improve this question
















bumped to the homepage by Community 7 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    0















    Hi guys I have a small code snippet and am trying to add a product to basket in Magento via PHP.



    Whatever I try (I've searched for a lot of snippets) I just get "The product could not be found." error. I have dialled my code back to a very short and simple snippet to try and get this working.



    Can anyone spot what might be causing this?



    require_once 'app/Mage.php';
    umask(0);
    Mage::init('default');
    Mage::getSingleton('core/session', array('name' => 'frontend'));

    $cart = Mage::getSingleton('checkout/cart');
    $sku="prodsku";
    $qty=1;
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    var_dump($product);
    $cart->addProduct($product, $qty);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


    I am using a var_dump on the product to make sure it is finding it and if I remove the line:



    $cart->addProduct($product, $qty);


    It then dumps the product information and it all looks fine.



    SOLVED: This single product was not physically ADDED to the website under Product > Websites, I also have to change to load by ID rather than SKU as it was throwing up stock errors.










    share|improve this question
















    bumped to the homepage by Community 7 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      0








      Hi guys I have a small code snippet and am trying to add a product to basket in Magento via PHP.



      Whatever I try (I've searched for a lot of snippets) I just get "The product could not be found." error. I have dialled my code back to a very short and simple snippet to try and get this working.



      Can anyone spot what might be causing this?



      require_once 'app/Mage.php';
      umask(0);
      Mage::init('default');
      Mage::getSingleton('core/session', array('name' => 'frontend'));

      $cart = Mage::getSingleton('checkout/cart');
      $sku="prodsku";
      $qty=1;
      $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
      var_dump($product);
      $cart->addProduct($product, $qty);
      $cart->save();
      Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


      I am using a var_dump on the product to make sure it is finding it and if I remove the line:



      $cart->addProduct($product, $qty);


      It then dumps the product information and it all looks fine.



      SOLVED: This single product was not physically ADDED to the website under Product > Websites, I also have to change to load by ID rather than SKU as it was throwing up stock errors.










      share|improve this question
















      Hi guys I have a small code snippet and am trying to add a product to basket in Magento via PHP.



      Whatever I try (I've searched for a lot of snippets) I just get "The product could not be found." error. I have dialled my code back to a very short and simple snippet to try and get this working.



      Can anyone spot what might be causing this?



      require_once 'app/Mage.php';
      umask(0);
      Mage::init('default');
      Mage::getSingleton('core/session', array('name' => 'frontend'));

      $cart = Mage::getSingleton('checkout/cart');
      $sku="prodsku";
      $qty=1;
      $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
      var_dump($product);
      $cart->addProduct($product, $qty);
      $cart->save();
      Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


      I am using a var_dump on the product to make sure it is finding it and if I remove the line:



      $cart->addProduct($product, $qty);


      It then dumps the product information and it all looks fine.



      SOLVED: This single product was not physically ADDED to the website under Product > Websites, I also have to change to load by ID rather than SKU as it was throwing up stock errors.







      magento-1.9 php cart shopping-cart






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 2 '18 at 10:57









      Teja Bhagavan Kollepara

      2,94841847




      2,94841847










      asked Feb 17 '17 at 12:11









      KiwisTasteGoodKiwisTasteGood

      482429




      482429





      bumped to the homepage by Community 7 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 7 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can try bellow Code:



          Mage::init('default');
          Mage::getSingleton('core/session', array('name' => 'frontend'));
          // Get customer session
          $session = Mage::getSingleton('customer/session');
          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();
          $productid=1;
          $qty=1;
          // Add a product with custom options
          $productInstance = Mage::getModel('catalog/product')->load($productid);
          $param = array(
          'product' => $productInstance->getId(),
          'qty' => $qty,
          );
          $request = new Varien_Object();
          $request->setData($param);
          $cart->addProduct($productInstance, $request);

          // update session
          $session->setCartWasUpdated(true);

          // save the cart
          $cart->save();





          share|improve this answer
























          • Same error unfortunately.

            – KiwisTasteGood
            Feb 17 '17 at 12:22











          • check if the product is_in_stock?

            – Prasanta Hatui
            Feb 17 '17 at 12:25











          • ["is_in_stock"]=> NULL - I have manage stock set to no

            – KiwisTasteGood
            Feb 17 '17 at 12:29











          • what is quantity?

            – Prasanta Hatui
            Feb 17 '17 at 12:33



















          0














          use below code to add product to cart



             try
          {

          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();

          $qty=1;

          $productId='10'; //product id

          $product = Mage::getModel('catalog/product')->load($productId);
          $formKey = Mage::getSingleton('core/session')->getFormKey();

          if($product->getSku()!='')
          {
          $paramater = array(
          'product' => $productId,
          'qty' => $qty,
          'form_key' => $formKey
          );

          $cart->addProduct($product, $paramater);
          $cart->save();
          }
          else
          {
          Mage::log('product not vailable');
          }
          $cart->save();
          Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
          }catch (Exception $e)
          {
          Mage::log($e->getMessage());
          }





          share|improve this answer


























          • Same error as before.

            – KiwisTasteGood
            Feb 17 '17 at 12:23











          • i edit my answer you check

            – Jigs Parmar
            Feb 17 '17 at 12:28











          • No error but doesn't add to cart either

            – KiwisTasteGood
            Feb 17 '17 at 12:31











          • any log print in log file??

            – Jigs Parmar
            Feb 17 '17 at 12:35











          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%2f160493%2fprogramatically-add-product-to-basket-error-the-product-could-not-be-found%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














          You can try bellow Code:



          Mage::init('default');
          Mage::getSingleton('core/session', array('name' => 'frontend'));
          // Get customer session
          $session = Mage::getSingleton('customer/session');
          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();
          $productid=1;
          $qty=1;
          // Add a product with custom options
          $productInstance = Mage::getModel('catalog/product')->load($productid);
          $param = array(
          'product' => $productInstance->getId(),
          'qty' => $qty,
          );
          $request = new Varien_Object();
          $request->setData($param);
          $cart->addProduct($productInstance, $request);

          // update session
          $session->setCartWasUpdated(true);

          // save the cart
          $cart->save();





          share|improve this answer
























          • Same error unfortunately.

            – KiwisTasteGood
            Feb 17 '17 at 12:22











          • check if the product is_in_stock?

            – Prasanta Hatui
            Feb 17 '17 at 12:25











          • ["is_in_stock"]=> NULL - I have manage stock set to no

            – KiwisTasteGood
            Feb 17 '17 at 12:29











          • what is quantity?

            – Prasanta Hatui
            Feb 17 '17 at 12:33
















          0














          You can try bellow Code:



          Mage::init('default');
          Mage::getSingleton('core/session', array('name' => 'frontend'));
          // Get customer session
          $session = Mage::getSingleton('customer/session');
          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();
          $productid=1;
          $qty=1;
          // Add a product with custom options
          $productInstance = Mage::getModel('catalog/product')->load($productid);
          $param = array(
          'product' => $productInstance->getId(),
          'qty' => $qty,
          );
          $request = new Varien_Object();
          $request->setData($param);
          $cart->addProduct($productInstance, $request);

          // update session
          $session->setCartWasUpdated(true);

          // save the cart
          $cart->save();





          share|improve this answer
























          • Same error unfortunately.

            – KiwisTasteGood
            Feb 17 '17 at 12:22











          • check if the product is_in_stock?

            – Prasanta Hatui
            Feb 17 '17 at 12:25











          • ["is_in_stock"]=> NULL - I have manage stock set to no

            – KiwisTasteGood
            Feb 17 '17 at 12:29











          • what is quantity?

            – Prasanta Hatui
            Feb 17 '17 at 12:33














          0












          0








          0







          You can try bellow Code:



          Mage::init('default');
          Mage::getSingleton('core/session', array('name' => 'frontend'));
          // Get customer session
          $session = Mage::getSingleton('customer/session');
          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();
          $productid=1;
          $qty=1;
          // Add a product with custom options
          $productInstance = Mage::getModel('catalog/product')->load($productid);
          $param = array(
          'product' => $productInstance->getId(),
          'qty' => $qty,
          );
          $request = new Varien_Object();
          $request->setData($param);
          $cart->addProduct($productInstance, $request);

          // update session
          $session->setCartWasUpdated(true);

          // save the cart
          $cart->save();





          share|improve this answer













          You can try bellow Code:



          Mage::init('default');
          Mage::getSingleton('core/session', array('name' => 'frontend'));
          // Get customer session
          $session = Mage::getSingleton('customer/session');
          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();
          $productid=1;
          $qty=1;
          // Add a product with custom options
          $productInstance = Mage::getModel('catalog/product')->load($productid);
          $param = array(
          'product' => $productInstance->getId(),
          'qty' => $qty,
          );
          $request = new Varien_Object();
          $request->setData($param);
          $cart->addProduct($productInstance, $request);

          // update session
          $session->setCartWasUpdated(true);

          // save the cart
          $cart->save();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 17 '17 at 12:19









          Prasanta HatuiPrasanta Hatui

          1,4321414




          1,4321414













          • Same error unfortunately.

            – KiwisTasteGood
            Feb 17 '17 at 12:22











          • check if the product is_in_stock?

            – Prasanta Hatui
            Feb 17 '17 at 12:25











          • ["is_in_stock"]=> NULL - I have manage stock set to no

            – KiwisTasteGood
            Feb 17 '17 at 12:29











          • what is quantity?

            – Prasanta Hatui
            Feb 17 '17 at 12:33



















          • Same error unfortunately.

            – KiwisTasteGood
            Feb 17 '17 at 12:22











          • check if the product is_in_stock?

            – Prasanta Hatui
            Feb 17 '17 at 12:25











          • ["is_in_stock"]=> NULL - I have manage stock set to no

            – KiwisTasteGood
            Feb 17 '17 at 12:29











          • what is quantity?

            – Prasanta Hatui
            Feb 17 '17 at 12:33

















          Same error unfortunately.

          – KiwisTasteGood
          Feb 17 '17 at 12:22





          Same error unfortunately.

          – KiwisTasteGood
          Feb 17 '17 at 12:22













          check if the product is_in_stock?

          – Prasanta Hatui
          Feb 17 '17 at 12:25





          check if the product is_in_stock?

          – Prasanta Hatui
          Feb 17 '17 at 12:25













          ["is_in_stock"]=> NULL - I have manage stock set to no

          – KiwisTasteGood
          Feb 17 '17 at 12:29





          ["is_in_stock"]=> NULL - I have manage stock set to no

          – KiwisTasteGood
          Feb 17 '17 at 12:29













          what is quantity?

          – Prasanta Hatui
          Feb 17 '17 at 12:33





          what is quantity?

          – Prasanta Hatui
          Feb 17 '17 at 12:33













          0














          use below code to add product to cart



             try
          {

          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();

          $qty=1;

          $productId='10'; //product id

          $product = Mage::getModel('catalog/product')->load($productId);
          $formKey = Mage::getSingleton('core/session')->getFormKey();

          if($product->getSku()!='')
          {
          $paramater = array(
          'product' => $productId,
          'qty' => $qty,
          'form_key' => $formKey
          );

          $cart->addProduct($product, $paramater);
          $cart->save();
          }
          else
          {
          Mage::log('product not vailable');
          }
          $cart->save();
          Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
          }catch (Exception $e)
          {
          Mage::log($e->getMessage());
          }





          share|improve this answer


























          • Same error as before.

            – KiwisTasteGood
            Feb 17 '17 at 12:23











          • i edit my answer you check

            – Jigs Parmar
            Feb 17 '17 at 12:28











          • No error but doesn't add to cart either

            – KiwisTasteGood
            Feb 17 '17 at 12:31











          • any log print in log file??

            – Jigs Parmar
            Feb 17 '17 at 12:35
















          0














          use below code to add product to cart



             try
          {

          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();

          $qty=1;

          $productId='10'; //product id

          $product = Mage::getModel('catalog/product')->load($productId);
          $formKey = Mage::getSingleton('core/session')->getFormKey();

          if($product->getSku()!='')
          {
          $paramater = array(
          'product' => $productId,
          'qty' => $qty,
          'form_key' => $formKey
          );

          $cart->addProduct($product, $paramater);
          $cart->save();
          }
          else
          {
          Mage::log('product not vailable');
          }
          $cart->save();
          Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
          }catch (Exception $e)
          {
          Mage::log($e->getMessage());
          }





          share|improve this answer


























          • Same error as before.

            – KiwisTasteGood
            Feb 17 '17 at 12:23











          • i edit my answer you check

            – Jigs Parmar
            Feb 17 '17 at 12:28











          • No error but doesn't add to cart either

            – KiwisTasteGood
            Feb 17 '17 at 12:31











          • any log print in log file??

            – Jigs Parmar
            Feb 17 '17 at 12:35














          0












          0








          0







          use below code to add product to cart



             try
          {

          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();

          $qty=1;

          $productId='10'; //product id

          $product = Mage::getModel('catalog/product')->load($productId);
          $formKey = Mage::getSingleton('core/session')->getFormKey();

          if($product->getSku()!='')
          {
          $paramater = array(
          'product' => $productId,
          'qty' => $qty,
          'form_key' => $formKey
          );

          $cart->addProduct($product, $paramater);
          $cart->save();
          }
          else
          {
          Mage::log('product not vailable');
          }
          $cart->save();
          Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
          }catch (Exception $e)
          {
          Mage::log($e->getMessage());
          }





          share|improve this answer















          use below code to add product to cart



             try
          {

          $cart = Mage::getSingleton('checkout/cart');
          $cart->init();

          $qty=1;

          $productId='10'; //product id

          $product = Mage::getModel('catalog/product')->load($productId);
          $formKey = Mage::getSingleton('core/session')->getFormKey();

          if($product->getSku()!='')
          {
          $paramater = array(
          'product' => $productId,
          'qty' => $qty,
          'form_key' => $formKey
          );

          $cart->addProduct($product, $paramater);
          $cart->save();
          }
          else
          {
          Mage::log('product not vailable');
          }
          $cart->save();
          Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
          }catch (Exception $e)
          {
          Mage::log($e->getMessage());
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 17 '17 at 12:27

























          answered Feb 17 '17 at 12:18









          Jigs ParmarJigs Parmar

          1,112423




          1,112423













          • Same error as before.

            – KiwisTasteGood
            Feb 17 '17 at 12:23











          • i edit my answer you check

            – Jigs Parmar
            Feb 17 '17 at 12:28











          • No error but doesn't add to cart either

            – KiwisTasteGood
            Feb 17 '17 at 12:31











          • any log print in log file??

            – Jigs Parmar
            Feb 17 '17 at 12:35



















          • Same error as before.

            – KiwisTasteGood
            Feb 17 '17 at 12:23











          • i edit my answer you check

            – Jigs Parmar
            Feb 17 '17 at 12:28











          • No error but doesn't add to cart either

            – KiwisTasteGood
            Feb 17 '17 at 12:31











          • any log print in log file??

            – Jigs Parmar
            Feb 17 '17 at 12:35

















          Same error as before.

          – KiwisTasteGood
          Feb 17 '17 at 12:23





          Same error as before.

          – KiwisTasteGood
          Feb 17 '17 at 12:23













          i edit my answer you check

          – Jigs Parmar
          Feb 17 '17 at 12:28





          i edit my answer you check

          – Jigs Parmar
          Feb 17 '17 at 12:28













          No error but doesn't add to cart either

          – KiwisTasteGood
          Feb 17 '17 at 12:31





          No error but doesn't add to cart either

          – KiwisTasteGood
          Feb 17 '17 at 12:31













          any log print in log file??

          – Jigs Parmar
          Feb 17 '17 at 12:35





          any log print in log file??

          – Jigs Parmar
          Feb 17 '17 at 12:35


















          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%2f160493%2fprogramatically-add-product-to-basket-error-the-product-could-not-be-found%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

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto