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

#   Add product to cart using a plugin

##

- **[Rixters](/forum/user/28762-rixters "View Rixters's Profile")**
- [![Rixters's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar28762.jpg)](/forum/user/28762-rixters "View Rixters's Profile") Topic Author
- [ Offline](/forum/user/28762-rixters)

Less More

- Posts: 209
- Thank you received: 14
-
-
-
- [](http://www.r2h.nl)
-

10 years 4 months ago [\#245011](#245011) by [Rixters](/forum/user/28762-rixters "View Rixters's Profile")  *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#245011 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")* was created by *[Rixters](/forum/user/28762-rixters "View Rixters's Profile")*

-- HikaShop version -- : 2.6.3
-- Joomla version -- : 3.5.1
-- PHP version -- : 7.0.9
-- Browser(s) name and version -- : FF

Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?

Hope you can get me started...

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50Lzg4NTQ2Ny1hZGQtcHJvZHVjdC10by1jYXJ0LXVzaW5nLWEtcGx1Z2luP3RtcGw9bWFya2Rvd24=) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Away](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

10 years 4 months ago [\#245020](#245020) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#245020 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Hi,

On this thread, we talk about adding a product to the cart with either a redirect, a form submit, or code:
[ www.hikashop.com/forum/2-general-talk-ab...-product-fields.html](http://www.hikashop.com/forum/2-general-talk-about-hikashop/16216-add-to-cart-link-and-custom-product-fields.html)
But it's actually quite easy to do:
$class = hikashop::get('class.cart');
$class-&gt;update($product_id, $quantity);

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

- **[Rixters](/forum/user/28762-rixters "View Rixters's Profile")**
- [![Rixters's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar28762.jpg)](/forum/user/28762-rixters "View Rixters's Profile") Topic Author
- [ Offline](/forum/user/28762-rixters)

Less More

- Posts: 209
- Thank you received: 14
-
-
-
- [](http://www.r2h.nl)
-

10 years 4 months ago [\#245107](#245107) by [Rixters](/forum/user/28762-rixters "View Rixters's Profile")  Replied by *[Rixters](/forum/user/28762-rixters "View Rixters's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#245107 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Great! That worked!

Just to be complete. Can I also remove a product from the cart or change the price using this class?

Thanks for the help!

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50Lzg4NTQ2Ny1hZGQtcHJvZHVjdC10by1jYXJ0LXVzaW5nLWEtcGx1Z2luP3RtcGw9bWFya2Rvd24=) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Away](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

10 years 4 months ago [\#245109](#245109) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#245109 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Hi,

Yes, and it's really easy. It's the same function with the quantity being 0 :
$class = hikashop::get('class.cart');
$class-&gt;update($product_id, 0);
Also note that the quantity is not added by default, it replaces the quantity of the product in the cart.
If you want to add a quantity of 2 to the quantity already in the cart, you need to do like that:
$class = hikashop::get('class.cart');
$class-&gt;update($product_id, 2, true);

The following user(s) said Thank You: [Rixters](/forum/user/28762-rixters "View Rixters's Profile")

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

- **[Rixters](/forum/user/28762-rixters "View Rixters's Profile")**
- [![Rixters's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar28762.jpg)](/forum/user/28762-rixters "View Rixters's Profile") Topic Author
- [ Offline](/forum/user/28762-rixters)

Less More

- Posts: 209
- Thank you received: 14
-
-
-
- [](http://www.r2h.nl)
-

10 years 4 months ago [\#245183](#245183) by [Rixters](/forum/user/28762-rixters "View Rixters's Profile")  Replied by *[Rixters](/forum/user/28762-rixters "View Rixters's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#245183 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Hi Nicolas,
With your help I created a plugin that add a free product to the cart depending on the total cart price. [ github.com/RickR2H/hikashop-free-product](https://github.com/RickR2H/hikashop-free-product)

Thanks for the help and regards!

The following user(s) said Thank You: [nicolas](/forum/user/63-nicolas "View nicolas's Profile"), [Siberio87](/forum/user/37259-siberio87 "View Siberio87's Profile"), [kyratn](/forum/user/38342-kyratn "View kyratn's Profile")

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

- **[kyratn](/forum/user/38342-kyratn "View kyratn's Profile")**
- [![kyratn's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/gallery/redswirl.jpg)](/forum/user/38342-kyratn "View kyratn's Profile")
- [ Offline](/forum/user/38342-kyratn)

Less More

- Posts: 1119
- Thank you received: 114
-
- [](http://kyra.lt)
-

10 years 4 months ago [\#245185](#245185) by [kyratn](/forum/user/38342-kyratn "View kyratn's Profile")  Replied by *[kyratn](/forum/user/38342-kyratn "View kyratn's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#245185 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Hi,

Thanks for the share. It can be very useful...

Regards

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

- **[Siberio87](/forum/user/37259-siberio87 "View Siberio87's Profile")**
- [![Siberio87's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/37259-siberio87 "View Siberio87's Profile")
- [ Offline](/forum/user/37259-siberio87)

Less More

- Posts: 29
- Thank you received: 0
-
-

10 years 3 months ago [\#246528](#246528) by [Siberio87](/forum/user/37259-siberio87 "View Siberio87's Profile")  Replied by *[Siberio87](/forum/user/37259-siberio87 "View Siberio87's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#246528 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

thank you sir for the plugin!

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

- **[ynnadje](/forum/user/13472-ynnadje "View ynnadje's Profile")**
- [![ynnadje's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/13472-ynnadje "View ynnadje's Profile")
- [ Offline](/forum/user/13472-ynnadje)

Less More

- Posts: 13
- Thank you received: 0
-
-

6 years 6 months ago [\#317785](#317785) by [ynnadje](/forum/user/13472-ynnadje "View ynnadje's Profile")  Replied by *[ynnadje](/forum/user/13472-ynnadje "View ynnadje's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#317785 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Thank you Nicholas,

This is a very usefull plugin. Could I add multiple products depending on the cart-total.

example
If you buy for 30€, you receive free product B
If you buy voor 100€ you receive product A + B
When buying for 500€, you receive products A + B + C

...

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50Lzg4NTQ2Ny1hZGQtcHJvZHVjdC10by1jYXJ0LXVzaW5nLWEtcGx1Z2luP3RtcGw9bWFya2Rvd24=) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Away](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

6 years 6 months ago [\#317792](#317792) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#317792 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Hi,

With the plugin on our marketplace ( [ www.hikashop.com/marketplace/product/118...on-free-product.html](https://www.hikashop.com/marketplace/product/118-coupon-free-product.html) ) you can do:
If you buy for 30€, you receive free product B
If you buy for 100€ you receive product A
When buying for 500€, you receive products C

But you can't have it add several products at once. That would require some development in the plugin to support it.

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

- **[bansi](/forum/user/70166-bansi "View bansi's Profile")**
- [![bansi's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/70166-bansi "View bansi's Profile")
- [ Offline](/forum/user/70166-bansi)

Less More

- Posts: 1
- Thank you received: 0
-
-

4 years 59 minutes ago [\#346503](#346503) by [bansi](/forum/user/70166-bansi "View bansi's Profile")  Replied by *[bansi](/forum/user/70166-bansi "View bansi's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#346503 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

how to fetch the product id for all different products?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50Lzg4NTQ2Ny1hZGQtcHJvZHVjdC10by1jYXJ0LXVzaW5nLWEtcGx1Z2luP3RtcGw9bWFya2Rvd24=) 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?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Away](/forum/user/63-nicolas)

Less More

- Posts: 86541
- Thank you received: 14304
-
-

3 years 11 months ago [\#346549](#346549) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Add product to cart using a plugin](/forum/31-development/885467-add-product-to-cart-using-a-plugin#346549 "-- HikaShop version -- : 2.6.3 -- Joomla version -- : 3.5.1 -- PHP version -- : 7.0.9 -- Browser(s) name and version -- : FF  Because Hikashop doesn't provide a functionality to add a product for free when a certain cart amount is met, I'm trying to build a plugin who does that for me. Could you provide a code sample how to add a product to the cart when a plugin event is fired? can I also then reset the the price to 0?  Hope you can get me started...")*

Hi,

Where do you want to do that ? What information of the products you have at hand ?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50Lzg4NTQ2Ny1hZGQtcHJvZHVjdC10by1jYXJ0LXVzaW5nLWEtcGx1Z2luP3RtcGw9bWFya2Rvd24=) 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.074 seconds

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