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.

  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染