doru52911
2013-01-18 12:04
浏览 52
已采纳

Magento - 将SKU的数组添加到购物车

I'm working on a add product by sku form for a e-commerce website I'm developing. I made the form which basically has 2 fields, "cod" and "qt".

I've passed that to this script (edited):

<?php
    $sku = $_POST['cod'];
    $qty = $_POST['qt'];
    $product = new Mage_Catalog_Model_Product();
    $cart = Mage::getSingleton('checkout/cart');

    $osids = array();

    foreach ($sku as $lol){
        $lolz = Mage::getModel('catalog/product')->loadByAttribute('sku',$lol)->getId();
        array_push($osids, $lolz);

    }
var_dump($osids);

$params = array(
    'qty' => 2,
);
$cart->addProductsByIds($osids, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


?>

The var_dump displays the array correctly, ie. multiple skus when that's the case, but the foreach loop only adds the first sku in the array to the shopping cart.

Any idea why?

I wan't it to add all the sku's in the array.

图片转代码服务由CSDN问答提供 功能建议

我正在为我正在开发的电子商务网站制作一个sku表格的添加产品。 制作了基本上有两个字段“鳕鱼”和“qt”的表格。

我已将其传递给此脚本(已编辑):

 &lt;?php 
 $ sku = $ _POST ['cod']; 
 $ qty = $ _POST ['qt']; 
 $ product = new Mage_Catalog_Model_Product(); 
 $ cart = Mage  :: getSingleton('checkout / cart'); 
 
 $ osids = array(); 
 
 foreach($ sku as $ lol){
 $ lolz = Mage :: getModel('catalog / product'  ) - &gt; loadByAttribute('sku',$ lol) - &gt; getId(); 
 array_push($ osids,$ lolz); 
 
} 
var_dump($ osids); 
 
 $ params =  array(
'qty'=&gt; 2,
); 
 $ cart-&gt; addProductsByIds($ osids,$ params); 
 $ cart-&gt; save(); 
Mage :: getSingleton('  checkout / session') - &gt; setCartWasUpdated(true); 
 
 
?&gt; 
   
 
 

var_dump正确显示数组,即。 多个skus就是这种情况,但foreach循环只将数组中的第一个sku添​​加到购物车。

知道为什么吗?

我不想在数组中添加所有sku。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyi7055 2013-01-18 12:14
    已采纳

    Try the following code :

    <?php
        $sku_list = $_POST['cod'];
        $qty = $_POST['qt'];
        $cart = Mage::getSingleton('checkout/cart');
    
        foreach ($sku_list as $sku){
            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
            $cart->addProduct($product, $qty);
        }
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
    
    打赏 评论

相关推荐 更多相似问题