久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

magento 1.8 使用 php 將產品添加到購物車

magento 1.8 add product to cart using php(magento 1.8 使用 php 將產品添加到購物車)
本文介紹了magento 1.8 使用 php 將產品添加到購物車的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 Magento 1.7 之前,我能夠使用以下代碼以編程方式將產品添加到購物車:

Up until Magento 1.7 I was able to use the following code to add a product to cart programatically:

require_once '../app/Mage.php'; 
Mage::getSingleton('core/session', array('name' => 'frontend')); 
umask(0);  
Mage::app();  
$session = Mage::getSingleton('customer/session');  
$product = Mage::getModel('catalog/product')->load(99);  // Random product ID

// get cart and add product
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();
$cart->addProduct($product, 1);

// update session
$session->setCartWasUpdated(true);

// save the cart
$cart->save();  

然而,這在 Magento 1.8 中不再起作用.我一整天都在嘗試/搜索為什么會這樣.可悲的是,我還沒有找到有關此問題的任何線索.

However, this does not work anymore in Magento 1.8. I've been trying/searching all day why this is the case. Sadly I haven't found any clue regarding this issue.

可能與1.8的變化有關,這也導致使用URL方法時需要form-key;不過,這是一個瘋狂的猜測.

It may have to do with the changes in 1.8 which also causes the requirement for a form-key when using the URL method; this is a wild guess though.

在使用 Magento 1.8 時,有人知道如何做到這一點嗎?

Anyone any idea or a working example on how to do this when working with Magento 1.8?

推薦答案

重新審視它并立即讓它運行良好.僅供任何人參考,這是我正在使用的代碼(Magento 1.8):

Had a fresh look at it and got it working nicely right away. Just for anyone's reference, here's the code I'm using (Magento 1.8):

// Mage init
require_once '../../app/Mage.php'; 
umask(0);  
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));  

// Get customer session
$session = Mage::getSingleton('customer/session'); 

// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

// Add a product (simple); id:12,  qty: 3 
$cart->addProduct(12, 3);

// Add a product with custom options
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
    'product' => $productInstance->getId(),
    'qty' => 1,
    'options' => array(
        234 => 'A value'  // Custom option with id: 234
    )
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);

// Set shipping method
$quote = $cart->getQuote();
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setShippingMethod('flatrate_flatrate')->save();               

// update session
$session->setCartWasUpdated(true);

// save the cart
$cart->save(); 

這篇關于magento 1.8 使用 php 將產品添加到購物車的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 色综合久久88 | 国产又色又爽又黄又免费 | 中文字幕免费观看 | 国内精品视频 | 人人爱人人草 | 成人免费看片在线观看 | 伊人网在线 | 国产小视频在线播放 | 99久久久精品 | 免费黄色小视频 | 亚洲欧美日本在线 | 丁香久久| 日韩三级久久 | 欧美日韩一区二区三区视频 | 精品国产一区二区三区久久久蜜月 | 天天色天天干天天 | 亚洲天堂一区 | 日韩一级淫片 | 不卡的av网站| 亚洲激情欧美 | 国产精品久久久久久无人区 | 综合伊人| 中文字幕精品视频 | 久草视频在线播放 | 麻豆av在线 | 精品久 | 中文字幕av一区二区 | 超碰在线视屏 | 亚洲精品久久久久 | 国产不卡在线观看 | 欧美日皮视频 | 亚洲天堂免费视频 | 一区二区三区不卡视频 | 无套内谢的新婚少妇国语播放 | 日本黄a三级三级三级 | 日本在线观看一区 | 日韩视频在线观看 | 欧美日韩综合在线 | 黄色精品视频 | 日韩在线一区二区 | 欧美激情国产精品 |