Donate discount to charity checkbox, how to change discount amount cart via plugin?

Started by Kuubs, December 04, 2024, 15:18:00 PM

Previous topic - Next topic

Kuubs

Hello,

I'm trying to develop a plugin to render a checkbox, when the checkbox is checked it should change the discount amount and save that amount in a new variable so i can save that when the order is confirmed.

Example. You order a product with 50 euro discount. Now when you check that box, the discount amount should be added back to the total cart amount and the discount amount should be saved in a new variable, donation amount. When the order is confirmed the user pays the full amount and the donation amount is saved in a new table. It's basically a checkbox to "Donate discount to charity". But I'm not sure how to proceed. I have this code right now but its not working at all:
public function plgVmPrepareCartData(VirtueMartCart &$cart)
    {

        $input = $this->app->input;
        $donateDiscount = $input->get('donate_discount', false, 'bool');

        if ($donateDiscount) {

            $discountAmount = $cart->cartData['salesPriceCoupon'] ?? 0;

            if ($discountAmount > 0) {
               
                $cart->cartPrices['salesPrice'] += $discountAmount;
                $cart->cartData['salesPriceCoupon'] = 0; // Remove coupon discount
                $cart->cartData['donationAmount'] = $discountAmount;
            }
        }
    }

Does someone know how I can make this work?