Changing Shipping Prices Based on User Selection in Magento
You’re on the right track with your approach to creating a custom shipping method and updating prices based on user selection in Magento 1.7.x.x. Let’s break down the issue and find a solution that maintains your changes throughout the checkout process.
Current Issue Analysis
Your current implementation uses an observer to update the shipping price when the shipping method is selected. However, you’re encountering two main problems:
- Price Reset: The price gets reset during the order placement step because Magento recalculates the rates at this stage.
- Incorrect Price Update (from your log): When you set the price directly on the rate object (
$rate->setPrice(10)), it doesn’t reflect in the quote totals.
Proposed Solution
To solve these issues, we’ll need to:
- Update the method that calculates shipping rates.
- Use the appropriate Magento objects to update prices and ensure they persist through checkout.
Here’s a step-by-step approach:
Step 1: Modify the Shipping Method Logic
Instead of using an observer for saveShippingMethod, let’s modify your custom shipping method class (Namespace_ModuleName_Model_Carrier_Pickship). In this class, you can add logic to update rates based on the user’s selection.
public function getRates(Mage_Shipping_Model_Rate_Request $request)
{
// Your existing rate collection or array initialization here
// Assuming you have a method that returns the selected option
$selectedOption = $this->getSelectedOption();
if ($selectedOption === 1) {
$ratePrice = '10.00';
} elseif ($selectedOption === 2) {
$ratePrice = '20.00';
} else {
$ratePrice = $this->getDefaultPrice();
}
// Create and add a new rate object with the updated price
$rate = Mage::getModel('shipping/rate_result_method');
$rate->setMethodTitle($this->_methodTitle);
$rate->setCarrier($this->_code);
$rate->setMethod($this->_code . '_' . $i);
$rate->setPrice($ratePrice);
// Add other required details...
return $rate;
}
Step 2: Persisting Price Through Checkout
To ensure the price persists through checkout, you need to:
- Update the quote’s shipping address with the new rate.
- Update the quote total using
Mage::getSingleton('checkout/session')->start()and$quote->collectTotals().
You can achieve this in your observer for checkout_controller_onepage_save_shipping:
public function saveShippingMethod($observer)
{
$request = $evt->getRequest();
$quote = $evt->getQuote();
// ... (existing code)
if ($pickship['select_element'] == 1) {
$ratePrice = '10.00';
} elseif ($pickship['select_element'] == 2) {
$ratePrice = '20.00';
}
$quote->getShippingAddress()->setShippingMethod($this->_code . '_' . $i);
$quote->getShippingAddress()->setShippingAmount($ratePrice);
Mage::getSingleton('checkout/session')->start();
$quote->collectTotals();
// ... (existing code)
}
Conclusion
By updating your custom shipping method class to include the logic for calculating rates based on user selection and ensuring you update the quote totals correctly, you should be able to maintain the updated shipping prices throughout the checkout process.
Remember to clear your cache after making these changes to ensure they take effect. If you still encounter any issues or need further clarification, feel free to explore other resources on Magento development, such as Laravel Company’s blog.
Happy coding!