Add product to cart using a plugin

  • Posts: 170
  • Thank you received: 9
  • Hikashop Business
6 years 4 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: 77249
  • Thank you received: 12165
  • MODERATOR
6 years 4 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: 170
  • Thank you received: 9
  • Hikashop Business
6 years 4 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: 77249
  • Thank you received: 12165
  • MODERATOR
6 years 4 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: 170
  • Thank you received: 9
  • Hikashop Business
6 years 4 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
6 years 4 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
6 years 4 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 7 months 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: 77249
  • Thank you received: 12165
  • MODERATOR
2 years 7 months 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.

  • Posts: 1
  • Thank you received: 0
3 weeks 1 day ago #346503

how to fetch the product id for all different products?

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

  • Posts: 77249
  • Thank you received: 12165
  • MODERATOR
3 weeks 7 hours ago #346549

Hi,

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

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

Time to create page: 0.119 seconds
Powered by Kunena Forum