【问题标题】:Trucating magento cart and then adding product to cart gives 500 error截断 magento 购物车然后将产品添加到购物车会出现 500 错误
【发布时间】:2018-04-13 03:01:51
【问题描述】:

在这里,我们首先截断了我们的 magento 购物车,

$cart = Mage::getModel('checkout/cart');                
  $cart->truncate()->save(); // remove all active items in cart page
  $cart->init();


  Mage::getSingleton('checkout/session')->clear();

在此之后,我们尝试添加产品

$cart = Mage::getSingleton('checkout/cart'); 
   $cart->init();
   $cart->addProduct($id,$qty);

它给出 500 内部服务器错误

注意:只有当我们应用添加产品数量到最大值时才会出错。

例如。可用数量100 在我的情况下$qty=100

注意:我们的代码运行良好,但只是第一次,其余时间它给出了这个错误。

【问题讨论】:

    标签: php magento cart


    【解决方案1】:

    找到答案了,

    我已经找到答案了,

     $session = Mage::getSingleton('checkout/session');
        $session->getQuote()->delete();
        $session->clear();
    
        $cart = Mage::getModel('checkout/cart');
        $cart->setQuote($session->getQuote());
    
        $product = Mage::getModel('catalog/product') -> load($product_id);
        if ($product) {
            if ($product -> isSaleable()) {
                $cart -> addProduct($product, array('qty' => $qty));
            }
        }
    
        // $session->setCartWasUpdated(true); // Not really necessary - Vicary
        $cart->save();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      相关资源
      最近更新 更多