how to change/update shipping price after shipping method choose - php

I am trying to create a custom (pickship) shipping method for magento 1.7.x.x following the instructions in this this blog after creating the shipping module. I would like to be able to change shipping cost based on the user's choice of shipping option.
Preparation:
1.) Created a form block for custom (pickship) shipping method
2.) Create a store name (can be any text) dropdown/select element with three values: store1, store2, store3.
3.) The default shipping price is set via the shipping configuration at $1.00 (just for testing purposes).
Current Status:
1.) After the required process, when I reach the selection of shipping method in the single-page checkout, I see a select element with three options and the shipping price set to the default price.
Target:
I would like the shipping cost to be updated to $10.00 if the user selects the first option, $20.00 if the user selects the second option.
My attempt:
After a few hours of googling, I made some changes in the observer model:
NameSpace_ModuleName_Model_Observer extends Varien_Object {
public function saveShippingMethod($observer) {
$request = $evt->getRequest();
$quote = $evt->getQuote();
$pickship = $request->getParam('shipping_pickship', false);
$rates = $quote->getShippingAddress()->getShippingRatesCollection();
$rate = $rates->getFirstItem();
if($pickship['select_element'] == 1) {
$rate->setPrice(10);
} elseif($pickship['select_element'] == 2) {
$rate->setPrice(20);
}
//Mage::log($rate->getPrice());
}
Question:
Am I using the right observer/method for this task? It only works for one step – once I click 'Continue' to place the order, it resets to the default price. My way of setting the delivery price may be wrong. Can you put me on the right track or suggest a solution?

If you want to code your tailored solution, you can find a nice tutorial here: Custom shipping method in Magento
Basically you should not use observers, instead you should define in your module a custom carrier with two shipping methods.
In your model, use function getAllowedMethods() to declare your shipping methods, then in function collectRates() give the rate for each shipping method.

If your main goal is to provide multiple shipping methods with different pricing, and can trust a third party module, you might try the free MatrixRate from WebShopApps
http://www.magentocommerce.com/magento-connect/webshopapps-matrixrate-1-multiple-table-rates-extension.html
There are other modules for this, but this is which I tried, and it works as I expected.

Related

Magento 2 Filter Shipping Methods based on Product Attribute

I am new to Magento 2. Anybody knows of a way to disable a shipping method based on an attribute withing the products in the cart. Let's say we wanna enable store pickup only for certain products.
On the onepage checkout these are updated with a post /rest/en/V1/guest-carts/SESSION_ID/estimate-shipping-methods but tried the module-checkout and module-quote and still can't find where this code is so i can extend it.
Would be helpful if somebody has been thru this before.
Thanks
Probably more than one way to accomplish this, but my method of choice was to create a plugin for \Magento\Shipping\Model\Shipping, specifically the collectRates() function. Granted our requirements were more specific than yours (at bottom).
Basic logic flow is...
collectRates() (Unmodified function in \Magento\Shipping\Model\Shipping, Collects rates for all shipping methods)
afterCollectRates() (plugin)
At this point all shipping methods have been called and rates stored in our $request object.
You can determine products that are in the cart via $request->getAllItems()
NOTE: Child/Parent products are separate items, and depending on your store config, one or the other may not have the custom attribute you want to look at.
You can see all shipping methods/rates via $request->getResult()->getAllRates()
I did not find a core function to remove a rate, my workaround was to...
Unset all data in rate to remove
After all rates are modified, use a foreach() loop to store them in a tempArray (with some logic to not add if cost==0, etc.)
Now flush and reset all existing rates via $request->getResult()->reset()
Finally, add the rates from your tempArray back in
Depending on how you calculate rates, you may also want to extend the various shipping methods so you can bypass them entirely when certain products are in the cart (probably not for your use case, but for anyone trying to disable UPS/FedEx/etc. rates)
As mentioned, our requirements were more extensive and we also had a beforeCollectRates() function which actually created the product array and some other logic (we had to restrict various shipping methods, add handling for specific products, and use dimensional logic to create a list of shipping boxes to send to UPS/FedEx, etc. for the actual CollectRates() part.)

Magento IWD Onepage checkout - set default shipping method

by default magento ( and this module ) show the shipping methods available only after the shipping form is filled..
but I need to show the methods on page load, before shipping infos.
Is this possible? How?
I have try so many code, but i can't implement this.
Last one, but i don't know where to put this.
$shippingAddress = $this->getQuote()->getShippingAddress();
$shippingAddress->setCountryId('UK')->setShippingMethod('matrixrate_matrixrate')-save();
I don't think it's possible to get all the shipping methods before filling in the shipping details.
In theory the shipping methods depend on the shipping details.
So even if you all the shipping methods you will get wrong prices.
And this is bad for business.
If the customer sees for example at first: Some shipping method: 5$, then he fills in his shipping address and sees: Some shipping method: 15$ there is a big chance he will stop the checkout.
But I wrote this answer so I can recommend you not to use the IWD checkout extension. Here is why.
To summarize what I answered for that question:
the extension sends your data to their servers.
if their server is offline, your checkout will probably stop working.

customize shipping method magento

I am trying to change the view of shipping method in checkout page, this is what I have now:
I want to remove radio button and show free shipping if its free, otherwise I want to show flat rate, its decided by the total purchase amount.
But I am not able to find the page where I can change this. I found some files related to shipping:
/app/design/frontend/base/default/template/checkout/onepage/shipping.phtml
/app/design/frontend/base/default/template/checkout/onepage/shipping_method.phtml
but shipping method content is not coming from these files.
The available methods are rendered in /app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml
Look around line 34:
foreach ($_shippingRateGroups as $code => $_rates):
You can add your condition below that line to show free shipping or flat rate.
Edit:
Alternatively, you can override Mage_Checkout_Block_Onepage_Shipping_Method_Available::getShippingRates() method so that shipping method of your choice is displayed.

Retrieve shipping price via webservice in Magento

I have created my own shipping module, but problem is that collectRates function is called when user comes to shipping method step and then these prices are just shown to user. But in my case I have to use webservice to get price, and there are some options user can choose in shipping method step screen, which will affect shipping price. Price itself is calculated on other server.
So how I can set shipping price via ajax in shipping method step?
I found working solution myself. I call shipping.save() JavaScript function to update shipping methods page, then collectRates (php) function in my own shipping module is called and everything works correctly.
shipping.save();

magento checkout: change shipping price

Scenario:
Change shipping price using variable parsed by design during checkout.
I need to change the final shipping price using some variables sent by phtml checkout.
Eg.
Shipping method 1: cost 33$
Shipping method 2: cost 23$
Shipping method 3: cost 10$
special packaging checkbox (y/n) + 10$ (set it as yes for this example)
assicuration:
I've inserted manually this checkbox, just in design but I need to increment final price and add a simple label that include this on shipment
total should be
shipment total 20$
In order to do this, you will need to write your own Shipping Module. There is a wiki article here with instructions, otherwise there are many good blog posts on the process. I would recommend that you use one of the existing Magento shipping modules (e.g. Mage_Shipping_Model_Carrier_Flatrate) as an example and then just extend the collectRates method to test for the value of your variable.

Categories