I've created code necessary to add a custom item to every cart, and all seems to be ALMOST working as intended. The code gets the items in the cart, and formulates XML that is sent over to a 3rd party in a request, and parses the response back to my code into an array. The response array is then added to the cart as a unique item with a specific, preordained SKU.
The only trouble that I'm facing is that when I do this, it doesn't seem to update the subtotal in the cart. For example:
I put in an order in the cart for one item costing 421ドル. This one item is put into an XML statement that is sent over to the 3rd party to get a certain price for a "bond" that is based on the cost of all items in the cart. The response sends the cost of the bond back as 12ドル.63 and the subtotal of the two items in the cart is 433ドル.63 as intended. Then, after playing with the quantity of the 421ドル item, I'm starting to see some odd things...
I change the number of the item to 5, and click "UPDATE CART". I get a return value of 63ドル.15 on the bond, but the subtotal shows that I'm adding 2,105ドル (the price of 5 items) with 12ドル.63 (the price of the original bond) for a total of 2,117ドル.63. I click "UPDATE CART" again, and it then shows the total of 2,168ドル.15.
This happens every time I change the quantity. I get the previous "bond" price added to the current subtotal, and it takes me two clicks of "UPDATE CART" to get the price caught up.
So, I was thinking if there was a way to add the code I've created BEFORE it runs anything else, I'd be able to close that gap. Am I doing this correctly? What is the best way to add my code previous to any other code that has been put in the XML to call forth this page?
EDIT
To explain further, this is the code block I'm using:
$cartHelper = Mage::helper('checkout/cart');
if ($buySAFEcost > 0) {
$product = Mage::getModel('catalog/product');
$bs_cart = Mage::getModel('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$bs_my_product_id = $product->getIdBySku($bs_sku);
$bs_my_product = $product->load($bs_my_product_id);
$params = array( 'qty' => 1, 'price' => $buySAFEcost );
$bs_cart->addProduct($bs_my_product,$params);
$bs_cart->save();
Mage::app()->setCurrentStore(0); // ADMIN_STORE_ID
$bs_my_product->setPrice($buySAFEcost);
$bs_my_product->save();
// Redo the totals?
$cartModel->getQuote()->getBillingAddress();
$cartModel->getQuote()->getShippingAddress()->setCollectShippingRates(true);
$cartModel->getQuote()->collectTotals();
$cartModel->getQuote()->save();
// $cartModel->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();
Mage::app()->setCurrentStore(1); // DISTRO_STORE_ID
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}
I feel like I'm killing an ant with a sledgehammer here, but the subtotals remain constantly one step behind my actions. Am I putting this in the wrong section of code, perhaps?
-
I'm also willing to say that if there is a way to reset the shopping cart subtotal after this process is performed, I'd also be interested in how to learn this.Vinnie Saletto– Vinnie Saletto2014年05月01日 19:55:31 +00:00Commented May 1, 2014 at 19:55
1 Answer 1
In this situation the best thing you need to do is to create a new subtotal for calculate this items after it to be added to your cart.
When you add a new total to your module, you can change the calculation of the quote based on the quote address object passed to your subtotal class. This way you can make changes every time the cart or quote object be saved and you will always have the subtotals updated as you want.
Basically you add a few nodes to your config.xml
<config>
<global>
<sales>
<quote>
<totals>
<my_new_total>
<class>mymodule/my_total_class</class>
<after>subtotal</after>
</my_new_total>
</totals>
</quote>
</sales>
</global>
</config>
In your total class you need to extend Mage_Sales_Model_Quote_Address_Total_Abstract and implement two public methods:
public function collect(Mage_Sales_Model_Quote_Address $address)
public function fetch(Mage_Sales_Model_Quote_Address $address)
Based on the $address object you can calculate all the values you need to.
Take this two links as example to create your new total:
http://turnkeye.com/blog/magento-development-add-total-row-checkout/ http://excellencemagentoblog.com/magento-add-fee-discount-order-total
I would write you the base code to do that but I'm a little busy here.
I hope I helped you.
Tiago Sampaio
-
Thank you, Tiago. I will give this a try and let you know how it goes here!Vinnie Saletto– Vinnie Saletto2014年05月02日 18:45:21 +00:00Commented May 2, 2014 at 18:45
-
Hi @VinnieSaletto. Did it work for you?Tiago Sampaio– Tiago Sampaio2014年05月12日 18:49:56 +00:00Commented May 12, 2014 at 18:49
-
Yes, it did. My apologies... I'll upvote and mark!Vinnie Saletto– Vinnie Saletto2014年05月13日 19:09:56 +00:00Commented May 13, 2014 at 19:09
-
Or at least mark... I don't have the rep to upvote!Vinnie Saletto– Vinnie Saletto2014年05月13日 19:10:17 +00:00Commented May 13, 2014 at 19:10