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 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,