简体   繁体   中英

Magento php add to cart with Custom price

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.

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.

My script looks like this:

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

That's the code for adding the product. For setting the price I tried the approach as mentioned in this thread on stackexchange: https://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.

Thanks in advance!

Best regards, Martin

Reading through Mage_Checkout_Model_Cart::addProduct() , there doesn't appear to be a way to set the item's price from the parameters. Instead, you'll need to add the product, then grab the resulting item, and set its price:

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

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

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

Haven't had the time to try this out, but it should be the right idea. Make sure that you set the original_custom_price field (using setOriginalCustomPrice() ), not one of the other prices. The other prices are recalculated during the totals process.

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);

If the params don't work, fetch the returned item from $cart->addProduct and change the price on the item before saving the cart.

$item = $cart->addProduct(...);
$item->setCustomPrice(...); {or whatever price attribute you like to set}
$cart->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM