Jump to content

negative value for cart rule


Recommended Posts

PrestaShop 1.6.1.5

I would like to enter a negative value in a cart rule. Will be handy to add a fee.

When i enter a negative value the cart rule there is a message that says that the value must be greater than zero.

So in AdminCartRulesController.php i commented arround line 220

//if ((int)Tools::getValue('reduction_amount') < 0) {
            //    $this->errors[] = Tools::displayError('Reduction amount cannot be lower than zero.');
            //}

and in AdminGroupsController.php i changed

protected function validateDiscount($reduction)
    {
        if (!Validate::isPrice($reduction) || $reduction > 100 || $reduction < 0) {		 					
            return true;
        } else {
            return true;
        }
    }

to

protected function validateDiscount($reduction)
    {
				if ((!Validate::isPrice($reduction) && $reduction > 100) || (!Validate::isNegativePrice($reduction) && $reduction > -100)) {
            return true;
        } else {
            return true;
        }
    }

 

Now there is a negative value in the database for the reduction_amount in the ps_cart_rule table but the cart rule is now not visible in the cart. So i think there is somewhere another thing i need to change.

 

Anyone any idea?

 

Link to comment
Share on other sites

in CartRule.php i changed the if

// Discount (¤)
            if ((float)$this->reduction_amount > 0) {
							 	
                $prorata = 1;
                if (!is_null($package) && count($all_products)) {
                    $total_products = $context->cart->getOrderTotal($use_tax, Cart::ONLY_PRODUCTS);
                    if ($total_products) {
                        $prorata = $context->cart->getOrderTotal($use_tax, Cart::ONLY_PRODUCTS, $package['products']) / $total_products;
                    }
                }

to


            if ((float)$this->reduction_amount != 0) {
							 	
               

now the negative reduction value is visible in the cart but the value is not calcullated.

 

Link to comment
Share on other sites

  • 2 weeks later...

It's working here for me now.

After i saw the post https://www.prestashop.com/forums/topic/241062-negative-cart-rulesvoucher/      ) 

i also added an extra field in the database and i changed the public function getOrderTotal in the cart.php.
Also i did the modifications i mentioned above.

 

So

- start with the modification in AdminCartRulesController.php
- do the change in AdminGroupsController.php
- do the change in CartRule.php.

- Create in the table "ps_cart_rule" an extra field with the name "negative_value" (boollean, standard false)
- add a cart rule with a negative value.
- change the value for "negative_value" in the database to 1 (=true)

- modify cart.php like here below

 

change the public function getOrderTotal in cart.php

Also check first if you have an override on the cart.php. If so you need to change the public function getOrderTotal in the override cart.php. 

change

if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount > 0) {
                    $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), $compute_precision);
 }

to

if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount != 0)
				{
					if ($cart_rule['obj']->negative_value == true)
					{
						$order_total_discount -= Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), 2);
					}
					else
					{
						$order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), 2);
					}				
					
				}

			} 

 

Now it should work.

It's working for me so now i can add for example an extra cost if a customer selects one kind of delivery. You can use all the options for the cart rules.
I'm also able to give a discount and to add a fee. A discount of 30€ and a fee of 50€ is resulting in a fee of 20€.

I now it's tricky because i modified the voucher function but i'm not using this. I also need to adapt the text because i use the voucher as a fee, it's better to change the "total discount value" text to something else for example total discount/fee.

 

Hope someone else can use this.

 

 

 

 

Link to comment
Share on other sites

  • 2 years later...

Thanks a lot for listing all the files to edit to make negative cart rules work.

In new versions of prestashop you must change this too:

/src/Adapter/Presenter/Cart/CartPresenter.php

Change

    private function cartVoucherHasAmountReduction($cartVoucher)
    {
        return isset($cartVoucher['reduction_amount']) && $cartVoucher['reduction_amount'] > 0;
    }

With
 

    private function cartVoucherHasAmountReduction($cartVoucher)
    {
        return isset($cartVoucher['reduction_amount']);
    }


And if you use % discount too, in the same file,

change

    private function cartVoucherHasPercentReduction($cartVoucher)
    {
        return isset($cartVoucher['reduction_percent'])
            && $cartVoucher['reduction_percent'] > 0
            && $cartVoucher['reduction_amount'] == '0.00';
    }

with
 

    private function cartVoucherHasPercentReduction($cartVoucher)
    {
        return isset($cartVoucher['reduction_percent'])
            && $cartVoucher['reduction_amount'] == '0.00';
    }



Hope this can help someone else.

Edited by Pandal (see edit history)
Link to comment
Share on other sites

  • 3 months later...

Do you can share solution for add these mods in PS 1.7.8.3 ?

 

Following all Martino00 steps, in cart.php file of last step is not exist the code:

if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount > 0) {
                    $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), $compute_precision);
 }

I also I dont know if I added correctly the new field from here there are not the option to choose "standard false", i created the field as BOOLEAN not null

thanks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...