2016-12-27 16 views
0

アイテムIDを使用してカートアイテムを更新するには?私は多くのことを探索したが、成功できなかった、ここに私のコードでいる -Magento - アイテムIDでアイテムを更新する

require_once '../app/Mage.php'; 
Mage::app('default'); 

$qty = $_REQUEST['quantity']; 
$item_id = (int) $_REQUEST['item_id']; 
$cart = Mage::getSingleton('checkout/cart'); 
$quoteItem = $cart->getQuote()->getItemById($item_id); 

/*$quoteItem = Mage::getModel('sales/quote_item')->getCollection() 
    ->addFieldToFilter('item_id', array('in' => array($item_id)));*/ 
print_r($quoteItem); 
if (!$quoteItem) { 
    Mage::throwException('Quote item is not found.'); 
} 

if ($qty == 0) { 
    $cart->removeItem($id); 
} else { 
    $quoteItem->setQty($qty)->save(); 
} 
$cart->save(); 

私はしかし、同じコードがwebsite.Pleaseヘルプで正常に動作している、APIでそれを実装しています!

答えて

3

私は以下のコードを使用してカートアイテムを更新しました。私はそれを行うためにオブザーバーを使用しています。これを試してみてください。

$quote = $observer->getEvent()->getQuote(); 
    var_dump($quote->getId()); 

    $quote = Mage::getSingleton('checkout/session')->getQuote(); 
    //var_dump($quote->getId()); 
    $cartItems = $quote->getAllItems(); 
    //$storeId = Mage::app()->getStore()->getId(); 
    foreach ($cartItems as $item) { 
     //echo $item->getSku(); 
     $item->set__($value); // desired quote field from database 
     $item->save(); 
    } 
    $quote->save(); 
+0

ウェブサービスでコードを使用していますが、ここでは「セッション」が有効でしょうか? –

+0

致命的なエラー:行13の/home/yxyshareoflovest/public_html/web-services/test.phpの非オブジェクト上のメンバー関数getEvent()を呼び出す –

+0

@SachinVairagi以下の行を削除します。 $ quote = $ observer-> getEvent() - > getQuote(); var_dump($ quote-> getId()); これはオブザーバでのみ機能します。 –

関連する問題