Solving Custom Total Row Prices in Magento B2B Shopping Cart
In developing a B2B webshop using Magento, you may face challenges with customizing the total row price displayed in the shopping cart. The core issue arises when the API returns only the total discounted price for multiple items, and you need to display this discount accurately.
Current Solution: Overriding Quote Item Model
The developer has already made significant progress by overriding the Mage_Sales_Model_Quote_Item model using a configuration XML file:
<global>
<models>
<sales>
<rewrite>
<quote_item>Frigg_Import_Model_QuoteItem</quote_item>
</rewrite>
</sales>
</models>
</global>
And then overriding the calcRowTotal() method:
class Frigg_Import_Model_QuoteItem extends Mage_Sales_Model_Quote_Item
{
// Code logic for setting and calculating custom row total price
}
Event Observer: Handling Custom Pricing
The developer has also implemented an event observer to handle the custom pricing logic. The setPriceForItem() method is triggered by the checkout_cart_product_add_after event, ensuring that the price is updated whenever a product is added to the cart:
public function setPriceForItem(Varien_Event_Observer $observer)
{
// Code logic for calling API and setting custom row total price
}
The Issue: Persistent Custom Prices
While this approach works when adding items to the basket, it fails to persist the custom prices across page refreshes or navigation to the shopping cart page. The original prices are restored because Magento recalculates the totals during these events.
Solution: Updating Quote Item Totals
To resolve this issue and ensure that the custom row total prices are persisted:
-
Update the quote item after setting the custom price: In your observer method, make sure to update the quote item’s totals immediately after setting the custom row total price. This can be done by calling
$item->save();or usingMage::getSingleton('checkout/session')->setQuote($quote);. -
Implement a custom block for displaying prices: Create a custom block class that extends
Mage_Core_Block_Template. Override the template file and use your observer method to set the custom row total price before rendering the view. -
Ensure the custom prices are retrieved during data preparation: In the cart block, make sure you retrieve the quote item’s custom row total price when preparing the data for display. You can do this by accessing
$item->getRowTotal(). -
Update the quote item totals in a controller action: If needed, create an observer or a custom controller action that updates the quote item totals whenever required, such as after applying coupons or during checkout.
Best Practices and Further Optimization
To optimize your solution:
-
Avoid modifying core files directly; use Magento’s built-in mechanisms like rewrite models and event observers to ensure compatibility with future updates.
-
Use a module for organizing your customizations and keeping them separate from the core codebase.
-
Implement proper error handling and logging when calling external APIs or updating quote items.
-
Regularly test your customizations in different scenarios, such as changing quantities, applying discounts, and navigating between pages.
Conclusion
In summary, to set custom total row prices in a Magento B2B shopping cart:
- Override the
Mage_Sales_Model_Quote_Itemmodel. - Implement an event observer for handling custom pricing logic.
- Update quote item totals immediately after setting custom prices.
- Ensure custom prices are retrieved during data preparation for display.
By following these steps and understanding Magento’s core processes, you can successfully customize the shopping cart experience to meet your B2B webshop requirements.
For more advanced Magento customizations and best practices, visit Laravel Company.