Add product to cart using a plugin

  • Posts: 168
  • Thank you received: 9
  • Hikamarket Multivendor Hikashop Business
5 years 10 months ago #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...

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

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
5 years 10 months ago #245020

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
But it's actually quite easy to do:
$class = hikashop::get('class.cart');
$class->update($product_id, $quantity);

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

  • Posts: 168
  • Thank you received: 9
  • Hikamarket Multivendor Hikashop Business
5 years 10 months ago #245107

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 or Create an account to join the conversation.

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
5 years 10 months ago #245109

Hi,

Yes, and it's really easy. It's the same function with the quantity being 0 :
$class = hikashop::get('class.cart');
$class->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->update($product_id, 2, true);

The following user(s) said Thank You: Rixters

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

  • Posts: 168
  • Thank you received: 9
  • Hikamarket Multivendor Hikashop Business
5 years 10 months ago #245183

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

Thanks for the help and regards!

The following user(s) said Thank You: nicolas, Siberio87, kyratn

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

  • Posts: 1119
  • Thank you received: 114
5 years 10 months ago #245185

Hi,

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

Regards

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

  • Posts: 29
  • Thank you received: 0
5 years 9 months ago #246528

thank you sir for the plugin!

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

  • Posts: 13
  • Thank you received: 0
2 years 3 weeks ago #317785

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 or Create an account to join the conversation.

  • Posts: 75473
  • Thank you received: 11742
  • MODERATOR
2 years 3 weeks ago #317792

Hi,

With the plugin on our marketplace ( www.hikashop.com/marketplace/product/118...on-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 or Create an account to join the conversation.

Time to create page: 0.094 seconds
Powered by Kunena Forum