0

I'm trying to add a configurable product to the cart, but while it's not throwing any Exceptions, the cart is still empty.

I've used this code before without problems, so I'm not sure if it's something to do with the version of Magento I'm using.

The code I'm using is:

   $post = $this->getRequest()->getPost();
   $session = Mage::getSingleton('customer/session'); 

   $attr = array_keys($post['sa']);

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

    foreach ($post['sa'][$attr[0]] as $optId){
        try {
            if (abs($post['qty'][$optId]) > 0){
                $product = Mage::getModel('catalog/product')->load($post['product']);
                $this->getRequest()->setParam('product',$post['product']);
                $this->getRequest()->setParam('super_attribute',array(
                        $attr[0] => $optId
                    ));
                $options = array(
                    "product"=>$post['product'], 
                    "super_attribute"=>array(
                        $attr[0] => $optId
                    ),                    
                    "qty"=>$post['qty'][$optId]
                ); 


                $opts = new Varien_Object();
                $opts->setData($options);

                var_dump($opts);

                $cart->addProduct($product, $opts);
                $cart->save();

            }
        } catch (Exception $e){
            var_dump($e);
        }


    }

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

$pdts = $cart->getAllVisibleItems();

    var_dump($pdts);
    die("??");

So I'd expect to get a cart with 6 items in it (3 products, configurable + simple), however I'm getting null instead - as you can see from the below, which also shows the $opts object I'm trying to pass:

    object(Varien_Object)[507]
      protected '_data' => 
        array (size=3)
          'product' => string '86' (length=2)
          'super_attribute' => 
            array (size=1)
              179 => string '20' (length=2)
          'qty' => string '1' (length=1)
      protected '_hasDataChanges' => boolean true
      protected '_origData' => null
      protected '_idFieldName' => null
      protected '_isDeleted' => boolean false
      protected '_oldFieldsMap' => 
        array (size=0)
          empty
      protected '_syncFieldsMap' => 
        array (size=0)
          empty
    object(Varien_Object)[663]
      protected '_data' => 
        array (size=3)
          'product' => string '86' (length=2)
          'super_attribute' => 
            array (size=1)
              179 => string '19' (length=2)
          'qty' => string '2' (length=1)
      protected '_hasDataChanges' => boolean true
      protected '_origData' => null
      protected '_idFieldName' => null
      protected '_isDeleted' => boolean false
      protected '_oldFieldsMap' => 
        array (size=0)
          empty
      protected '_syncFieldsMap' => 
        array (size=0)
          empty
    object(Varien_Object)[678]
      protected '_data' => 
        array (size=3)
          'product' => string '86' (length=2)
          'super_attribute' => 
            array (size=1)
              179 => string '17' (length=2)
          'qty' => string '3' (length=1)
      protected '_hasDataChanges' => boolean true
      protected '_origData' => null
      protected '_idFieldName' => null
      protected '_isDeleted' => boolean false
      protected '_oldFieldsMap' => 
        array (size=0)
          empty
      protected '_syncFieldsMap' => 
        array (size=0)
          empty
    null
    ??

Any help would be apprciated!

1

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $simpleProduct = Mage::getModel('catalog/product'); try { $simpleProduct // ->setStoreId(1) //you can set data in store scope ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array ->setAttributeSetId(20) //ID of a attribute set named 'default' ->setTypeId('simple') //product type ->setCreatedAt(strtotime('now')) //product creation time // ->setUpdatedAt(strtotime('now')) //product update time ->setSku('simple99y') //SKU ->setName('test simple product99') //product name ->setWeight(4.0000) ->setStatus(1) //product status (1 - enabled, 2 - disabled) ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping) ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility ->setManufacturer(28) //manufacturer id ->setColor(24) ->setNewsFromDate('06/26/2014') //product set as new from ->setNewsToDate('06/30/2014') //product set as new to ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code) ->setPrice(11.22) //price in form 11.22 ->setCost(22.33) //price in form 11.22 ->setSpecialPrice(00.44) //special price in form 11.22 ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY) ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY) ->setMsrpEnabled(1) //enable MAP ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config) ->setMsrp(99.99) //Manufacturer's Suggested Retail Price ->setMetaTitle('test meta title 2') ->setMetaKeyword('test meta keyword 2') ->setMetaDescription('test meta description 2') ->setDescription('This is a long description') ->setShortDescription('This is a short description') ->setMediaGallery(array('images' => array(), 'values' => array())) //media gallery initialization ->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart 'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => 999 //qty ) ) ->setCategoryIds(array(3, 10)); //assign product to categories $simpleProduct->save(); } catch (Exception $e) { Mage::log($e->getMessage()); echo $e->getMessage(); }

|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.