[Skip to main content](#vessel-content)

#   Programmatically change shipping method from cart

##

- **[olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")**
- [![olaf@zuurstof.nl's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile") Topic Author
- [ Offline](/forum/user/47570-olaf-zuurstof-nl)

Less More

- Posts: 9
- Thank you received: 0
-
-

9 years 7 months ago [\#264984](#264984) by [olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")  *[Programmatically change shipping method from cart](/forum/28-shipping-methods/889348-programmatically-change-shipping-method-from-cart#264984 "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!")* was created by *[olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")*

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](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI4LXNoaXBwaW5nLW1ldGhvZHMvODg5MzQ4LXByb2dyYW1tYXRpY2FsbHktY2hhbmdlLXNoaXBwaW5nLW1ldGhvZC1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86286
- Thank you received: 14263
-
-

9 years 7 months ago [\#264989](#264989) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Programmatically change shipping method from cart](/forum/28-shipping-methods/889348-programmatically-change-shipping-method-from-cart#264989 "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!")*

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-&gt;get('reset_cache');
after you change the shipping method selected so that you don't have caching issues.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI4LXNoaXBwaW5nLW1ldGhvZHMvODg5MzQ4LXByb2dyYW1tYXRpY2FsbHktY2hhbmdlLXNoaXBwaW5nLW1ldGhvZC1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")**
- [![olaf@zuurstof.nl's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile") Topic Author
- [ Offline](/forum/user/47570-olaf-zuurstof-nl)

Less More

- Posts: 9
- Thank you received: 0
-
-

9 years 7 months ago [\#265032](#265032) by [olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")  Replied by *[olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")* on topic *[Programmatically change shipping method from cart](/forum/28-shipping-methods/889348-programmatically-change-shipping-method-from-cart#265032 "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!")*

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(&amp;$subject, $config) { parent::__construct($subject, $config); } function onBeforeCartUpdate($cartClass,$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force) { if($cart-&gt;package[volume][value] &gt; 5) { $cart-&gt;cart_shipping_ids = array('1@0'); } $cartClass-&gt;get('reset_cache'); } }

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI4LXNoaXBwaW5nLW1ldGhvZHMvODg5MzQ4LXByb2dyYW1tYXRpY2FsbHktY2hhbmdlLXNoaXBwaW5nLW1ldGhvZC1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")**
- [![Mohamed Thelji's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar6861.png)](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")
- [ Offline](/forum/user/6861-mohamed-thelji)

Less More

- Posts: 12953
- Thank you received: 1778
-
-
-

9 years 7 months ago -  9 years 7 months ago [\#265095](#265095) by [Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")  Replied by *[Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile")* on topic *[Programmatically change shipping method from cart](/forum/28-shipping-methods/889348-programmatically-change-shipping-method-from-cart#265095 "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!")*

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 7 months ago by [Mohamed Thelji](/forum/user/6861-mohamed-thelji "View Mohamed Thelji's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI4LXNoaXBwaW5nLW1ldGhvZHMvODg5MzQ4LXByb2dyYW1tYXRpY2FsbHktY2hhbmdlLXNoaXBwaW5nLW1ldGhvZC1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")**
- [![olaf@zuurstof.nl's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile") Topic Author
- [ Offline](/forum/user/47570-olaf-zuurstof-nl)

Less More

- Posts: 9
- Thank you received: 0
-
-

9 years 7 months ago [\#265155](#265155) by [olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")  Replied by *[olaf@zuurstof.nl](/forum/user/47570-olaf-zuurstof-nl "View olaf@zuurstof.nl's Profile")* on topic *[Programmatically change shipping method from cart](/forum/28-shipping-methods/889348-programmatically-change-shipping-method-from-cart#265155 "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!")*

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-&gt;cart_shipping_ids=='1@0') { if(!isset($cartClass-&gt;total)) { $cartClass-&gt;cart_shipping_ids='2@0'; } } }

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI4LXNoaXBwaW5nLW1ldGhvZHMvODg5MzQ4LXByb2dyYW1tYXRpY2FsbHktY2hhbmdlLXNoaXBwaW5nLW1ldGhvZC1mcm9tLWNhcnQ/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop- Install &amp; Update- Product &amp; Category Display- Content Creation- Sales and Orders- - Orders management- - Reports &amp; Statistics- Customers- Checkout- Payment Methods- Shipping Methods- Import &amp; Export (CSV, XLS…)- Specific Features- - Affilation- - Taxes- - Wishlist- - Vote- - Carousel &amp; Slider- - Mass Actions- - Filters &amp; Search- - Discount &amp; Coupon- - Feature Request- Integrations- Showcase- General Talk about HikaShop- Development- - HikaShop 3.0 Beta- Translation- Commercial Jobs HikaShop Components- HikaMarket- - Market: Feature requests- - Market: How to?- - Market: Bug Report- HikaSerial- - General talk about HikaSerial- - Serial: Bug Report- - Serial: How to?- - Support en Français de HikaSerial (French Support)- - HikaSubscription- HikaAuction- - Auction: Feature requests- - Auction: How to?- - Auction: Bug report Hikashop around the world- Support en Français (French Support)- Russian- - FAQ. Курс выживания с Hikashop. Частые вопросы и о- - Расширения Hikashop- - Наши сайты на Hikashop- 日本語版サポート(Japanese Support)- Thai- German Archives- Bug Report- How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.257 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)

[](#top)
