Hello
I'm creating a guest order programmatically and I would like to add a product with custom price. Instead the product is added with the current price from catalog. I found several suggested solutions making use of setOriginalCustomPrice(), setCustomPrice() and perhaps setIsSuperMode(true). Here's the code I have so far
$quote = Mage::getModel('sales/quote')->setStoreId($store->getId()); $quote->setCurrency($order->AdjustmentAmount->currencyID); $quote->setCheckoutMethod('guest'); $quote->setCustomerId(null); $quote->setCustomerEmail($customer_email); $quote->setCustomerIsGuest(true); $quote->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID); $product = Mage::getModel('catalog/product') ->load($product_id); $buyInfo = array( 'qty' => 1, ); $quote->addProduct($product, new Varien_Object($buyInfo)) ->setOriginalCustomPrice($custom_price) ->setCustomPrice($custom_price) ->setIsSuperMode(true) ->save(); $quote->save();
which causes SQL error
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`myshop_dev`.`sales_flat_quote_item`, CONSTRAINT `FK_SALES_FLAT_QUOTE_ITEM_QUOTE_ID_SALES_FLAT_QUOTE_ENTITY_ID` FOREIGN KEY (`quote_id`) REFERENCES `sales_flat_quote` (`entity_id`) ON DELETE C)' in /var/www/myshop/public/lib/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /var/www/myshop/public/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /var/www/myshop/public/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) #2 /var/www/myshop/public/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array) #3 /var/www/myshop/public/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array) #4 /var/www/myshop/public/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `sa...', Array) #5 /var/www/ in /var/www/myshop/public/lib/Zend/Db/Statement/Pdo.php on line 234
Any idea what needs to be changed to avoid the error and create the order with custom product price? Thanks in advance.
Solved! Go to Solution.
Rick711 wrote: Saving the quote before setting the custom price on quoted item will create the order but with original price, not custom price.
My fault, the changed approach actually worked as expected. I needed to save the quote after having added the product. Then setting the custom price is successful by saving the quote item.
I tried a slightly different approach with the following code
$quote->addProduct($product, new Varien_Object($buyInfo)); $quote->save(); // without saving the quote here I get error $quoteItem = $quote->getItemByProduct($product); $quoteItem->setOriginalCustomPrice($custom_price); $quoteItem->setCustomPrice($custom_price); $quoteItem->setIsSuperMode(true); $quoteItem->save();
Without saving the quote before trying to set the custom price I still get the same SQL error.
Saving the quote before setting the custom price on quoted item will create the order but with original price, not custom price.
When and how could I set the custom price for a product/quote item?
Rick711 wrote: Saving the quote before setting the custom price on quoted item will create the order but with original price, not custom price.
My fault, the changed approach actually worked as expected. I needed to save the quote after having added the product. Then setting the custom price is successful by saving the quote item.