Jump to content

Adding atribute as a percentage increase


Recommended Posts

Hi.

I'm looking at using PS as a new solution for the next stage in development of my canvas framing business.

Currently I have numerous categories for every size canvas that I can make. Everything from 20cm x20cm up to 200cm x 200cm (and all permutations inbetween).

Quite a few customers have remarked that this method can be confusing and too many clicks - this implies I'm losing a lot of sales. So I'd like to simplify the system.

As each size is essentially the same product I'd like to have one product, but allow the customer to specify the size (width x height) which would change the cost accordingly (I have tables of each sizes' cost). Something like what they have done here would be fantastic: http://www.woodeos.com/product.php?id_product=15 (though that looks like some heavy JS coding with Away3D)

In addition, each canvas can come in two variants - primed/unprimed and 38mm/50mm depth.

These I'd like to have as +% increase attributes rather than fixed +£ increases because the cost of them increases as the bars get longer/surface area increases.

Is such a thing possible with PS? (the JS solution obviously is, though may be an overly elegant solution to a fairly simple problem - and it uses PS as a back end.)

Thanks for your input.

Link to comment
Share on other sites

I think this could be quite tricky. I suggest looking at lines 1266-1267 of classes/Product.php (in Prestashop v1.2.5):

if (isset($result['attribute_price']))
   $price += $attribute_price;



This code adds the attribute to the price. Try changing it to the following:

if (isset($result['attribute_price']))
   $price *= $attribute_price;



If you enter the attribute price as 0.9, this code should reduce the price by 10% instead of adding $0.90 to the price. You may also need to edit the last section of the updateDisplay() function in js/product.js in your theme's directory for the price to work.

Link to comment
Share on other sites

  • 1 month later...
I think this could be quite tricky. I suggest looking at lines 1266-1267 of classes/Product.php (in Prestashop v1.2.5):

if (isset($result['attribute_price']))
   $price += $attribute_price;



This code adds the attribute to the price. Try changing it to the following:

if (isset($result['attribute_price']))
   $price *= $attribute_price;



If you enter the attribute price as 0.9, this code should reduce the price by 10% instead of adding $0.90 to the price. You may also need to edit the last section of the updateDisplay() function in js/product.js in your theme's directory for the price to work.[/quote



I there Rocky, can you please tell what to change in product.js, i want to have attributes with increase of price by percentage, thanks a lot

Link to comment
Share on other sites

Now that I look at it, I think you need to modify admin/tabs/AdminAttributeGenerator.php instead of product.js so that it multiplies instead of adding. Change line 26 from:

$price += floatval($_POST['price_impact'][intval($attr)]);



to:

$price *= floatval($_POST['price_impact'][intval($attr)]);

Link to comment
Share on other sites

Now that I look at it, I think you need to modify admin/tabs/AdminAttributeGenerator.php instead of product.js so that it multiplies instead of adding. Change line 26 from:

$price += floatval($_POST['price_impact'][intval($attr)]);



to:

$price *= floatval($_POST['price_impact'][intval($attr)]);




Thanks friend, but it still not working, maybe some other files to change? Thanks in advance
Link to comment
Share on other sites

  • 4 weeks later...

I´m making my investements about this post ;-), the code is written, now is time to make tests ;-).


root/classes/Product.php

if (isset($result['attribute_price']))

 if ($attribute_price >0)  {

       $price += $attribute_price;

   } else {

       $price *= $attribute_price;

   }



root/admin/tabs/AdminAttributeGenerator.php

if ($_POST['price_impact']>0) {  

   $price += floatval($_POST['price_impact'][intval($attr)])

} else {

   $price *= floatval($_POST['price_impact'][intval($attr)])            

}



My idea is to make an hybrid solution..., if you put the atribute higher than 0, use the base method, but if you put an atribute percent like: 0.12, you use a percentage increase.

What do you think about?, do you like it? ;-)

Link to comment
Share on other sites

Sure, I would appreciate it if you could post a full solution to this "attribute as percentage increase" problem. I have someone asking me for a solution to this problem right now, but I don't feel like hacking PrestaShop at the moment.

Link to comment
Share on other sites

Another anotation:

root/js/attributesBack.js

if (price_impact < 0)
   {
       getE('attribute_price_impact').options[getE('attribute_price_impact').selectedIndex].value = -1;
       getE('attribute_price_impact').selectedIndex = 2;
   }
   else if (!price_impact)
   {
       getE('attribute_price_impact').options[getE('attribute_price_impact').selectedIndex].value = 0;
       getE('attribute_price_impact').selectedIndex = 0;
   }
   else if (price_impact > 0)
   {
       getE('attribute_price_impact').options[getE('attribute_price_impact').selectedIndex].value = 1;
       getE('attribute_price_impact').selectedIndex = 1;
   }
   if (weight_impact < 0)
   {
       getE('attribute_weight_impact').options[getE('attribute_weight_impact').selectedIndex].value = -1;
       getE('attribute_weight_impact').selectedIndex = 2;
   }



I think, there´s the key ;-)

Link to comment
Share on other sites

  • 2 weeks later...

I know this topic is old now, but I found it really useful in accomplishing the same task - applying price impacts using multipliers instead of addition. My site has many items which can be bought singly or in bulk packs, and I needed to multiply the original price by a modifier instead of adding to it incrementally.

I followed the above suggestions but could not get a complete solution. Here's all the changes I ended up making:

In admin/tabs/AdminProducts.php, I slightly changed the user interface to reflect the new behaviour. I changed

  '.$this->l('of').'  '.($currency->format == 1 ? $currency->sign.' ' : '').'


to

 x 



In admin/tabs/AdminAttributeGenerator.php, I changed

$price += floatval($_POST['price_impact'][intval($attr)]);


to

$price *= floatval($_POST['price_impact'][intval($attr)]);



In classes/Product.php I changed

        $price += $attribute_price;


to

        $price *= $attribute_price;



and after doing this I thought I'd done it. But when viewing my product in the front end, the correct multiplied price would show for a few seconds, and then it would change back to the old added price when the javascript loaded. Took me a while to find where this was happening but eventually I changed themes//js/product.js

            var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction);



to

        var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);



And now it all seems to be working well. Hope this helps someone else.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Hi, first of all thanks for solution.

I tried the Jobatkin solution for pplying price impacts using multipliers but I have a problem.

It runs very fine in the products that have atributtes with price impact, but in products without atributtes or without atributtes with price impact the price showed is "0.00".

This is a proble because I have a lots of products without atributtes.

Can anyone help me to fix this.


P.D. Sorry for my English.

Link to comment
Share on other sites

Hi I try a modification to fix the problem with products without price impact. But don't works

A need a little help because i noob in all this

In classes/Product.php I changed

$price *= $attribute_price; 



to

       if (($attribute_price > 0) && ($attribute_price <= 1))    
       $price *= $attribute_price;
       else
       $price += $attribute_price;






And i changed in themes//js/product.js

var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);



to

       if (attribut_price_tmp > 0 && (attribut_price_tmp <=1))    
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction);



Anyone have a suggestion to do it better.

Link to comment
Share on other sites

  • 2 months later...

Hello All,
I have coped very well so far with the installations using prestashop, sure beats OsComm!
this being my first post as i have came up blank on this one.

I have followed the following posts
http://www.prestashop.com/forums/viewthread/78364/
and this one,
i used the following mods to try to make this work:

Change: admin/tabs/AdminProducts.php,


  '.$this->l('of').'  '.($currency->format == 1 ? $currency->sign.' ' : '').' 


to

 x  



Change: admin/tabs/AdminAttributeGenerator.php,

$price *= floatval($_POST['price_impact'][intval($attr)]);



to:

if (($attribute_price >= -1) && ($attribute_price < 0))
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           elseif (($attribute_price > 0) && ($attribute_price <= 1 ))
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           else
           $price += floatval($_POST['price_impact'][intval($attr)]); 



Change: classes/Product.php

$price += $attribute_price; 



to

if (($attribute_price >= -1) && ($attribute_price < 0))
       $price *= $attribute_price;
       elseif (($attribute_price > 0) && ($attribute_price <= 1 ))
       $price *= $attribute_price;
       else
       $price += $attribute_price; 



Change: themes//js/product.js

var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction); 



to;

if (attribut_price_tmp >= -1 && (attribut_price_tmp < 0))                 var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);         else if (attribut_price_tmp > 0 && (attribut_price_tmp <= 1))             var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);         else             var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction); 



My problem seems to be this.
The attributes when input as a 0.1 will calculate a percentage of 10%
but it is not adding this to the base price, instead it is just showing the percentage value
eg. £100 base price + 10% should show £110
instead it is displaying £10

any attribute with a 1> will add the value as normal
eg. £100 base price + £25 shows £125

So the only problem is, is that the percentage increment is not adding itself to the base price,
can any one see where there may be a fix from the above code?
or if i have missed anything out from the two posts.
As I mentioned, i followed the two posts noted, and put together the above from all the chops and changes from the posts.
I know this post is a little old, but I hope others out there are still reading this.
Thanks in advance to all contributors

Baz.

Link to comment
Share on other sites

Hi,
The finally of this modifications is reduce the base price to a percentage of itself.
If you want to reduce the base price, for example a discount of 15%, (base price = 100€ atribute price = 100€ - 15% = 85€) the price impact have to be 0.85 (1 - 0.15).

Otherwise if you want to increase the price in a percentage you have to modifiy the code erasing the conditions that contains <=1 or >= -1:

In AdminAttributeGenerator.php,
Change

if (($attribute_price >= -1) && ($attribute_price < 0))
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           elseif (($attribute_price > 0) && ($attribute_price <= 1 ))
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           else
           $price += floatval($_POST['price_impact'][intval($attr)]); 


To

if ($attribute_price < 0)
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           elseif ($attribute_price > 0)
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           else
           $price += floatval($_POST['price_impact'][intval($attr)]); 



In Product.php
Change

if (($attribute_price >= -1) && ($attribute_price < 0))
       $price *= $attribute_price;
       elseif (($attribute_price > 0) && ($attribute_price <= 1 ))
       $price *= $attribute_price;
       else
       $price += $attribute_price; 


To

if ($attribute_price < 0)
       $price *= $attribute_price;
       elseif ($attribute_price > 0)
       $price *= $attribute_price;
       else
       $price += $attribute_price; 



And in product.js
Change

if (attribut_price_tmp >= -1 && (attribut_price_tmp < 0))    
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else if (attribut_price_tmp > 0 && (attribut_price_tmp <= 1))
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction); 


to

if (attribut_price_tmp < 0)    
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else if (attribut_price_tmp > 0)
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction); 



Now you can increase the price for example a 15% putting 1.15 in the input of price impact.

I hope this help you.

In fact this fix code is redundant now, i do it fast but i think it works, if don't works tellme and i'll take a look slowly.

Link to comment
Share on other sites

Hi,
How you surely always are going to use positive numbers you can erase the condition <0.

In AdminAttributeGenerator.php,
Change

if ($attribute_price < 0)
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           elseif ($attribute_price > 0)
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           else
           $price += floatval($_POST['price_impact'][intval($attr)]); 


to

if ($attribute_price > 0)
           $price *= floatval($_POST['price_impact'][intval($attr)]);
           else
           $price += floatval($_POST['price_impact'][intval($attr)]); 


In Product.php
Change

if ($attribute_price < 0)
       $price *= $attribute_price;
       elseif ($attribute_price > 0)
       $price *= $attribute_price;
       else
       $price += $attribute_price; 


to

if ($attribute_price > 0)
       $price *= $attribute_price;
       else
       $price += $attribute_price; 



And in product.js
Change

if (attribut_price_tmp < 0)    
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else if (attribut_price_tmp > 0)
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction); 


to

if (attribut_price_tmp > 0)
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) * productPriceWithoutReduction);
       else
           var productPriceWithoutReduction2 = (ps_round(attribut_price_tmp * currencyRate) + productPriceWithoutReduction); 



This is only a fix that solves specific needs, it is so far to become a module. I take a look at the last RC version and is still not possible to create attributes that change the price in a percentage.

So. we will have to keep on waiting.

Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

Hi,
first of all I apologize for take so long to post a replay, but i've been a little busy these days.
I am testing prestashop 1.4.2.5 now, and a tried to make these modifications work on it doing the following changes to the originall files of prestashop 1.4.2.5.

In admin/tabs/AdminAttributeGenerator.php,
Change

           $price += (float)($_POST['price_impact_'.(int)($attr)]);
           $weight += (float)($_POST['weight_impact'][(int)($attr)]);



to

           if (($attribute_price > 0))
           $price *= (float)($_POST['price_impact'][(int)($attr)]);
           else
           $price += (float)($_POST['price_impact'][(int)($attr)]);

           $weight += (float)($_POST['weight_impact'][(int)($attr)]);



In admin/tabs/AdminProducts.php,
Change

  '.$this->l('of').'  '.($currency->format % 2 != 0 ? $currency->sign.' ' : '').'



to

 x 



In classes/Product.php
Change

       if ($id_product_attribute !== false) // If you want the default combination, please use NULL value instead
           $price += $attribute_price;



to

       if (($attribute_price > 0))
               $price *= $attribute_price;
       else
               $price += $attribute_price;



so far i think it is all correct, i test a little and all seems to work fine. But i have problems to get the correct display price in the product page. I did the following.

in themes//js/product.js
Change

       var taxExclPrice = (specific_price ? (specific_currency ? specific_price : specific_price * currencyRate) : productPriceTaxExcluded) + combination_add_price * currencyRate;



to

if (selectedCombination['price'] = 0 )
       var taxExclPrice = (specific_price ? (specific_currency ? specific_price : specific_price * currencyRate) : productPriceTaxExcluded) + (combination_add_price * currencyRate);
               else
       var taxExclPrice = (specific_price ? (specific_currency ? specific_price : specific_price * currencyRate) : productPriceTaxExcluded) * combination_add_price * currencyRate;



Whit this change the price displayed is correct when the attribute has a price impact different from zero.
But when the price impact is equal to 0 the price displayed is 0.00, if you add to cart, in blockcart the price is correct.
Now to get this work well, if you do not want that the attribute has an impact on the price, you have to define the price impact is equal to 1.

I think it is better that if you don't to modify the price in an attribute ther price impact should be 0 but i can't get this.
I am doing something wrong but i don't know what it is.
If anyone that knows a little more than me about this code can take a look and give us any suggestions we would be so grateful.

Best regards.

Link to comment
Share on other sites

  • 1 year later...
  • 2 months later...
  • 4 weeks later...
  • 2 months later...
  • 1 year later...
  • 11 months later...
  • 2 months later...
  • 1 month later...

Code for PS 1.6.1.2

 

change (or add override) Product.php

            // If you want the default combination, please use NULL value instead
            if ($id_product_attribute !== false) {
                $price += $attribute_price;
            }

to

            // If you want the default combination, please use NULL value instead
            if ($id_product_attribute !== false) {
        if ($attribute_price==0 || $attribute_price >2) {
                $price += $attribute_price;
        } else {
                $price *= $attribute_price;
        }
            }

change Product.js (in template directory)

	// Apply combination price impact (only if there is no specific price)
	// 0 by default, +x if price is inscreased, -x if price is decreased
	basePriceWithoutTax = basePriceWithoutTax + +combination.price;
	basePriceWithTax = basePriceWithTax + +combination.price * (taxRate/100 + 1);

to

	// Apply combination price impact (only if there is no specific price)
	// 0 by default, +x if price is inscreased, -x if price is decreased
      if (combination.price==0 || combination.price >2) {
	basePriceWithoutTax = basePriceWithoutTax + +combination.price;
	basePriceWithTax = basePriceWithTax + +combination.price * (taxRate/100 + 1);
	} else {
	basePriceWithoutTax = basePriceWithoutTax * combination.price;
	basePriceWithTax = basePriceWithTax * combination.price * (taxRate/100 + 1);	
	}

Codes placed above don't work with specific price (in the same time with the same attribute)

 

You have to use "increase" impact in range: 0.00<x<2.00

where  i.e 0.90 is 90% and 1.10 is 110% of original price

 

if u want to have wider range like 300% You have to change only number 2 to 3 in codes above.

Link to comment
Share on other sites

  • 3 months later...
  • 5 months later...

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...