I need to create an order with more than 60 products programmatically but it seems like Magento skips some(around 3 or 4) products randomly. I think it's a problem with itemsCollection of _quote but I have no idea what's wrong with my code. On repeated requests it skips different products. I disabled/deleted cache, reindexed everything. Here's a snippet of my code:

$this->_quote   = Mage::getModel( 'sales/quote' );
//assign customer, address code here
foreach($products as $product){
    //add custom options 
    $buyRequest = array(
        'product_id' => $product->getId(),
        'qty' => $req['Quantity']
    $product_request = $this->_getProductRequest( $buy_request );
    $quote_item = $this->_quote->addProduct( $product, $product_request );
    if( is_string( $quote_item ) ) {
        //code never enters this if
        throw new Exception( $quote_item );

$itemsCount = count($this->_quote->getItemsCollection());
//for more than 60? items it seems like skips some products randomly
//continue with order create process

No error or exception message is generated, order gets created with success. Another strange thing is that when I use PHPStorm's debugger and place a breakpoint inside foreach, all products get added to quote without any problem. Is there some sort of asynchronousness or collection caching in quote addProduct proccess?

LE: I'm using magento 1.9



The problem was in my code where I was generating a random string for each product and add it as custom option to differentiate the products in cart. That random string was not unique, so if for any two products that random string was the same, the quantity for the first of products added to cart was increased.

I solved it by adding a counter variable to the random string so that each random string is unique for that cart

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.