programmatically add discount

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
3 years 4 months ago #335051

-- HikaShop version -- : 4.4.3
-- Joomla version -- : 3.9.27
-- PHP version -- : 7.3.28
-- Browser(s) name and version -- : Chrome 90.0.4430.212

Hi,
we need to create and add "on-the-fly" one or more discounts to one or more products in cart on checkout page
i say "on-the-fly" because we need to create/add discount based on what thirdpary backend app say aboutn the product (if it deserve or not one or more discounts)

do you have any php code that can help us?
thank you

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

  • Posts: 12953
  • Thank you received: 1778
3 years 4 months ago #335064

Hello,

We'll actually need more information about what you exactly want to achieve and when that piece of php code needs to be triggered.

To add a discount using php code, you'll just have to use and SQL request to add a row to the "#_hikashop_discount" table, you can manualy create a discount and then check its structure through the "#_hikashop_discount" table.

Kind regards,
Mohamed.

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
3 years 4 months ago #335069

is there a way to achive this without directly accessing the db but instead using hikashop helpers, class etc.?
some php example code?

how can we associate a discount to a specific product by code?

we use a product and customize it on request with several custom item field, than put it to cart
it's price is altered by "amount" custom item field (from donation plugin) because we use hikashop to sell custom tickets coming from thirdparty software, i create a "dummy" product with price = 1, and than use it to add one o more "copy" of it to cart, customizing it with custom item fields and the price is altered by "amout" custom item field.

in my immagination product inside cart ($cart->cart_products[]) can be setup with a reference to a discount i create ad-hoc.
i prior create a "dummy" discount with no value or 0, and if i need it i take it, alter it's value (percentage or flat) based on what my thirdparty software say, and associate it to my product in cart

can you help me please?

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

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
3 years 4 months ago #335072

Hi,

I would recommend you base yourself on the global cart fee plugin:
www.hikashop.com/marketplace/product/224-global-cart-fee.html
This plugin will add an extra fee to the cart when the cart is being loaded.
So you could do the same but instead of a positive fee, you could add a negative fee (so a discount).

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

  • Posts: 50
  • Thank you received: 1
  • Hikamarket Multivendor Hikashop Business
3 years 4 months ago #335073

thank you for your kind support!

is possibile to have multiple fee in the same cart? because we can have multiple type of discount ("five%", "family", "september-promo") and we want customers to visibily know the kind of discounts he gets!

anyway, we still prefer to use discount system because we want to add discount to product, not to entire cart, because in a cart customer can put several type of products and not every product in cart deserves a discount, and we want customers to know what product deserve a discount and what product do not deserve a discount

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

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
3 years 4 months ago #335075

Hi,

Yes. Using the "additional" mechanism you see in the global cart fee plugin, you can have as many discounts as you want. You just need to give a different key to each discount in the additional array.

Now, if you want to use discounts, you can only have one discount per product. In that case, you can implement the onSelectDiscount(&$product, &$discountsSelected, &$discounts, $zone_id, &$parent) event in a plugin. In $product you'll have the data of the product, in $discountSelected you'll have an array of discounts already valid (based on how you configured the discounts that can potentially apply to the product) and the system will use the one with the smallest key inside the array with the smallest key in there.
So what you can do is create one or several generic discount(s) that by default apply all the time, and then in your code, you can remove them from $discounts if the conditions are not met or modify their amount based on your needs.
Note that doing it like that, it means that the discount will apply directly to each product, even before they are in the cart and you can have one discount per product, so you can have several discounts in the same cart but for different products, basically how discounts normally work in HikaShop.

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

  • Posts: 7
  • Thank you received: 0
3 years 2 months ago #336443

Hi nicolas,

Which trigger I should use if I want to add this discount after adding a product to cart, not on checkout page?

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

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
3 years 2 months ago #336465

Hi,

Discounts are added automatically to the price of the product even before you add it to the cart. You can see the discount on the products listing and the product details page.

And coupons are added automatically to the carts when they are loaded if the "auto load in cart if possible" setting is activated in the coupon. So not only in the checkout but even elsewhere. So you can see the coupon added to the cart in the cart module if the display setting for coupons is activated in the settings page of the cart module in the Joomla modules manager.

So there is actually no way to have a discount/coupon added to the cart on the checkout page and not after adding the product to the cart. It's either both or neither.

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

  • Posts: 7
  • Thank you received: 0
3 years 2 months ago #336504

Thank you nicolas,

Seems I'm a little bit confused about how discounts and coupons are used.

What I want to do is:
After clicking the add to cart button, I want to have multiple copies of the added product with different quantities and prices.

So I created a plugin and implemented the onBeforeCartSave event to add the products, which is done successfully with correct quantities, but I can't find out how to set the prices for each product copy to be different from the product setup price.

Can this be done in the same event or it should be done in another event?

Thanks for your help.

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

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
3 years 2 months ago #336510

Hi,

The price is retrieved automatically from the database when the cart is loaded.
So you can't set the price for each product in the cart when adding the products to it.
The exception would be if you create a custom item field with the column name 'amount' and use the donation plugin so that the value entered in that custom item field overrides the price of the product:
demo.hikashop.com/index.php/en/hikashop/...t-page/donation-page
When you add / modify the products in $cart->cart_products in the onBeforeCartSave of your plugin, you can directly inject the custom item field value using the column name of the custom field.

The following user(s) said Thank You: mabdelaziz

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

  • Posts: 7
  • Thank you received: 0
3 years 1 month ago #336546

Thank you nicolas :)
Worked like a charm!

I'm still willing to know more about the "additional" mechanism you mentioned earlier, so please let me know whether there is a kind of documentation or example that shows how to use it and for which use case.

Best regards

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

  • Posts: 83024
  • Thank you received: 13403
  • MODERATOR
3 years 1 month ago #336549

Hi,

We don't have a documentation for it. However, as I told you previously the www.hikashop.com/marketplace/product/224-global-cart-fee.html global cart fee plugin is a good example of how to use to add an extra fee to the cart.

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

Time to create page: 0.103 seconds
Powered by Kunena Forum