Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento php以自定义价格添加到购物车_Php_Jquery_Ajax_Magento_Module - Fatal编程技术网

Magento php以自定义价格添加到购物车

Magento php以自定义价格添加到购物车,php,jquery,ajax,magento,module,Php,Jquery,Ajax,Magento,Module,我有以下问题: 我在前端通过jQuery进行计算,并希望将产品添加到购物车中,价格在前端计算 我已经用AjaxController编写了一个自定义模块来实现添加到购物车部分,但我知道设置自定义价格很困难 我的脚本如下所示: $_prod = json_decode(Mage::app()->getRequest()->getPost('zapfsaeule_product')); $id = 347; // there is only this one bundle pro

我有以下问题: 我在前端通过jQuery进行计算,并希望将产品添加到购物车中,价格在前端计算

我已经用AjaxController编写了一个自定义模块来实现添加到购物车部分,但我知道设置自定义价格很困难

我的脚本如下所示:

$_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');
$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);
这是添加产品的代码。 为了设定价格,我尝试了stackexchange上本线程中提到的方法:

但它不起作用。我想这里观察到的事件不会发生在我的案例中,因为我编写了一个自定义脚本

但问题仍然存在,如果观察者方法可行,我如何将计算出的价格传递给观察者

我希望你能理解这个问题,并能帮助我解决它

提前谢谢

致以最良好的祝愿,
Martin

如果参数不起作用,请从$cart->addProduct获取返回的商品,并在保存购物车之前更改商品的价格

$item = $cart->addProduct(...);
$item->setCustomPrice(...); {or whatever price attribute you like to set}
$cart->save();
阅读Mage_Checkout_Model_Cart::addProduct,似乎没有一种方法可以通过参数设置商品的价格。相反,您需要添加产品,然后获取结果项并设置其价格:

$cart->addProduct($product, $params)
    ->save();

// grab the corresponding item
$item = $cart->getQuote()->getItemByProduct($product);

// set its custom price
$item->setOriginalCustomPrice($customPrice)
    ->save();

我还没有时间尝试一下,但这应该是正确的想法。确保使用SetOriginalCustomComprice设置原始\u自定义\u价格字段,而不是其他价格之一。其他价格在总计过程中重新计算。

这来得很晚,但我只是偶然发现了这一点

我让它像这样工作:

$_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');
$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);

不要在前端计算,因为所有用户都会自己计算,因为他们想知道,但我们需要承担风险。问题如下:我们有不同的瓶子,灌装量不同,客户应该选择瓶子和他想要的液体。因此,我们需要根据所选瓶子的灌装量来计算液体的价格。而是将数量和液体种类发送到服务器并在那里计算价格。好的,但我该如何设置产品的价格$_product->setPrice Order setFinalPrice无效您需要将其添加到参数中。addProduct函数使用产品和参数创建一个Item对象。如果您试图更改产品型号的价格,您将在商店而不是购物车上更改产品的价格。查找addProduct功能。那么,哪个部分不工作?您是否看到数据库中的custom_original_price字段see table sales_flat_quote_item?抱歉,我写错了列名。根据我的回答,这实际上是原价。此字段存在,并且具有我在其中设置的价格。但购物车仍然显示计算出的产品价格。我应该提醒你,这是我正在使用的捆绑产品。再次感谢!嗯,真奇怪。原始\自定义\价格应覆盖购物车项目的价格,无论其代表的是哪种类型的产品请参见Mage \销售\模型\报价\项目\摘要::calcRowTotal和Mage \销售\模型\报价\项目\摘要::getBaseCalculationPriceOriginal。如果我有什么想法,我会发回,但与此同时,试着深入研究定价逻辑,看看你能发现什么。将一些Mage::log语句放入上述方法中,以及Mage\u Sales\u Model\u Quote\u Address\u Total\u Subtotal::\u initItem,以及其他一些语句。谢谢!我会这样做,并让你知道结果。你真的帮了我很多