Skip to main content

Programmatically change shipping method from cart

More
9 years 6 months ago #264984 by olaf@zuurstof.nl
I have several shipping methods with restrictions based on total volume and 1 shipping method for any volume (pick-up at warehouse).

When starting a cart, none of the restrictions are met, so the 'pick-up' method is selected.

Now, when the cart volume reaches the first restriction I would like to re-select the shipping method based on the order (so the paid shipment is selected instedad of the pick-up method). I would like to keep the 'pick-up' method usable as well so users can select that one once in the checkout process.

How can I do that using PHP from the cart?

Thnx for your help!

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #264989 by nicolas
Hi,

Ideally, you can implement the triggers onBeforeCartSave and/or onAfterCartSave in a plugin of the group "hikashop" in order to set the cart_shipping_ids of the cart.
I would recommend calling :
$class = hikashop_get('class.cart');
$class->get('reset_cache');
after you change the shipping method selected so that you don't have caching issues.

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #265032 by olaf@zuurstof.nl
Hi Nicolas,

Can you help me out? I'new to plgins for Hikashop and objects/methods are not entirely clear to me.... This is the code I came up with, but I'm sure this is not the right way...
Code:
class plgHikashopHrtg_change_shipping extends JPlugin { function __construct(&$subject, $config) { parent::__construct($subject, $config); } function onBeforeCartUpdate($cartClass,$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force) { if($cart->package[volume][value] > 5) { $cart->cart_shipping_ids = array('1@0'); } $cartClass->get('reset_cache'); } }

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago - 9 years 6 months ago #265095 by Mohamed Thelji
Hello,

The code that you used should actually do the job, but can you also use the "onAfterCartSave" function and test it again ?

Thank you.
Last edit: 9 years 6 months ago by Mohamed Thelji.

Please Log in or Create an account to join the conversation.

More
9 years 6 months ago #265155 by olaf@zuurstof.nl
I was using the wrong trigger and I had to use the cartClass object, it seems that 'onBeforeCartSave' does work.

Only problem is that I do not have the total volume in that function so I force the cart to always try the default shipment. If restrictions are not met the cart falls back to the 'pick-up' shipment method. That seems to work fine....

For those interested:
Code:
function onBeforeCartSave($cartClass,$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force) { if($cartClass->cart_shipping_ids=='1@0') { if(!isset($cartClass->total)) { $cartClass->cart_shipping_ids='2@0'; } } }

Please Log in or Create an account to join the conversation.

Time to create page: 0.157 seconds
Powered by Kunena Forum