Magento的PHP添加到购物车与自定义价格 [英] Magento php add to cart with Custom price
问题描述
我有以下问题: 我在前台通过jQuery进行计算,并希望将产品添加到与前端计算价格的车。
I have the following issue: I make calculations via jQuery in the frontend and want to add a product to the cart with the price calculated in the frontend.
我已经写了一个自定义模块与AjaxController实现增加车的一部分,但我知道有设置自定义价格的斗争。
I already wrote a custom module with an AjaxController to achieve the adding to cart part, but I know struggle with setting the custom price.
我的脚本是这样的:
$_prod = json_decode(Mage::app()->getRequest()->getPost('zapfsaeule_product'));
$id = 347; // there is only this one bundle product added to the cart viar this scipt, so a static id is enough.
$params = array(
'product' => $id,
'related_product' => null,
'bundle_option' => array(
6 => 17, // static options for testing purpouses
5 => 13), //
'qty' => 1 // static qty for testing as well
);
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load($id);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$this->getResponse()->setBody('true'); // setting response to true, because its an ajax request.
$this->getResponse()->setHeader('Content-type', 'text/plain');
这就是code添加的产品。 对于设定价格,我想尽了办法中提到的关于stackexchange这个线程: <一href="http://magento.stackexchange.com/questions/4318/dynamically-calculated-prices-save-before-add-to-cart">http://magento.stackexchange.com/questions/4318/dynamically-calculated-prices-save-before-add-to-cart
That's the code for adding the product. For setting the price I tried the approach as mentioned in this thread on stackexchange: http://magento.stackexchange.com/questions/4318/dynamically-calculated-prices-save-before-add-to-cart
但没有奏效。我想在这里观察到的事件不发生在我的情况,因为我写了一个自定义脚本。
But it didn't work. I guess the event observed here doesn't occur in my case, because I wrote a custom script.
但后来还是会有问题,如果观察者方法是有效的,我将如何通过计算价格观察者?
But then there still would be the problem, IF the observer approach would work, how would I pass the calculated price to the observer?
我希望你明白的问题,可以帮助我解决这个问题。
I hope you understand the problem and can help me solve it.
在此先感谢!
最好的问候, 马丁
推荐答案
这涉及pretty的晚了,但我只是偶然发现了这一点。
This comes pretty late, but I just stumbled upon this.
我得到它的工作是这样的:
I got it to work like this:
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, 1); // 1 = qty. Pass your qty and params.
$item = $cart->getQuote()->getItemByProduct($product);
$item->setCustomPrice(0); // or some other value
$item->setOriginalCustomPrice(0); // or some other value
$item->getProduct()->setIsSuperMode(true); // this is crucial
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
这篇关于Magento的PHP添加到购物车与自定义价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!