Create a specific calculate discount (one shot) depending a previoinvoice credit

  • Posts: 7
  • Thank you received: 0
  • Hikashop Business
2 weeks 2 days ago #364878

-- HikaShop version -- : 5.1.1
-- Joomla version -- : 5
-- PHP version -- : 8
-- Browser(s) name and version -- : Chrome

Hi,
I would like to create programatically in my plugin a system to give a calculate discount on checkout for a customer who has an "avoir (french in text) like a invoice credit" from another gift - i tried to make that in my my plugin

function onAfterCartProductsLoad(&$cart)
    { if ($avoir > 0) {
                // Créer un objet de remise
                $discount = new stdClass();
                file_put_contents("logBob.txt", "--- onAfterCartProductsLoad --- discount" .PHP_EOL, FILE_APPEND);
                $discount->discount_id = 0; // 0 pour indiquer qu'il s'agit d'une remise à la volée

                $discount->discount_value = -$avoir; // Remise négative pour réduire le total
                $discount->discount_type = 'cart'; // Type "cart" pour le total du panier
                $discount->discount_name = "Avoir client";
                file_put_contents("logBob.txt", "--- onAfterCartProductsLoad --- discount name" .PHP_EOL, FILE_APPEND);
                // Ajouter la remise au panier
                if (!isset($cart->cart_discounts)) {
                    $cart->cart_discounts = [];
                }
                $cart->cart_discounts[] = $discount;
                file_put_contents("logBob.txt", "--- onAfterCartProductsLoad --- discount name".print_r($cart->cart_discounts,true).PHP_EOL, FILE_APPEND);
                // Mettre à jour le total du panier
                $cart->full_total->prices[0]->price_value -= $avoir;
                $cart->full_total->prices[0]->price_value_with_tax -= $avoir;
                // Forcer la mise à jour des totaux des produits et du panier
                foreach ($cart->cart_products as &$product) {
                    if (!isset($product->total)) {
                        continue;
                    }

                    // Ajuster les totaux pour refléter la remise
                    $product->total->prices[0]->price_value -= $avoir / count($cart->cart_products);
                    $product->total->prices[0]->price_value_with_tax -= $avoir / count($cart->cart_products);
                }
            }
...
but when i click pay button - the cart is empty as i cancelled
I dont understand or i don't know why it's not easy to create a discount on subtotal or total
easily
Could you help me Please ?

Last edit: 2 weeks 2 days ago by nicolas.

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

  • Posts: 83022
  • Thank you received: 13403
  • MODERATOR
2 weeks 2 days ago #364880

Hi,

First, your $avoir variable pops out from nowhere at the beginning of the onAfterCartProductsLoad method.

Second, I'm not sure what you mean by "when i click pay button - the cart is empty as i cancelled" ? Or how is it linked to custom plugin ?

Also, the "discount_type" with the value "cart" doesn't exist in HikaShop. Why not use the normal "discount" type ? Using a non standard type might lead to some piece of code of HikaShop not properly taking into account your discount.

Finally, I would actually recommend not calculating the total yourself.
There are two ways I would recommend to do it:
- you could implement instead the "onAfterLoadProductPriceDiscount" event of HikaShop in order to inject your discount when HikaShop is loading the discounts applicable to a product. You would have to check in $rows if the objects have the cart_product_quantity value not empty to know whether HikaShop is asking for the discounts of the products of a cart or not. And $rows would contain all the products in the cart so you would still be able to divide the credit note (avoir) on the number of products in the cart.
- Now, adding the discount to each product in the cart seems clunky to me. I would rather recommend adding an extra fee between the subtotal and the total of the cart, with the appropriate label. This can be done with the "additional fee" mechanism of the cart system.
A relatively simple example of this is the global cart fee plugin which can be used to add extra fixed fees to carts:
www.hikashop.com/marketplace/product/224-global-cart-fee.html
A better version of this is the cart fees plugin we have on our marketplace:
www.hikashop.com/marketplace/product/279-cart-fee.html
It can add fees (or discounts if the calculated value is negative) on the fly with a dynamically calculated value and with a dynamically processed condition. In fact, if your credit note is stored in a custom field of the table "user" you could directly use this plugin to add the credit note to the cart of the user with zero development.

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

Time to create page: 0.056 seconds
Powered by Kunena Forum