You are here:  » Use Filters to change price


Use Filters to change price

Submitted by sanniep on Sun, 2015-03-29 21:13 in

Hi,

How can I increase the price of a product using a filter? I want to add a percentage or amount to the price. I think I can't use the filters that are available now?

Regards, Sander

Submitted by support on Mon, 2015-03-30 08:21

Hi Sander,

Sure - here's the code for a Price Adjust filter - simply add to end of your includes/filter.php file, and then add a Price Adjust filter either per feed or as a Global Filter as required. Enter the price, and choose from either amount (currency symbol) or % increase...

  /*************************************************/
  /* priceAdjust */
  /*************************************************/
  $filter_names["priceAdjust"] = "Price Adjust";
  function filter_priceAdjustConfigure($filter_data)
  {
    global $config_currencyHTML;
    widget_textBox("Amount","amount",TRUE,$filter_data["amount"],"",3);
    widget_selectArray("","type",TRUE,$filter_data["type"],array("#"=>$config_currencyHTML,"%"=>"%"));
  }
  function filter_priceAdjustValidate($filter_data)
  {
    if ($filter_data["amount"]=="") widget_errorSet("amount","required field");
  }
  function filter_priceAdjustExec($filter_data,$text)
  {
    $text = tapestry_decimalise($text);
    if ($filter_data["type"]=="#")
    {
      $text = ($text + $filter_data["amount"]);
    }
    else
    {
      $text = ($text / 100) * (100 + $filter_data["amount"]);
    }
    $text = tapestry_decimalise($text);
    return $text;
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sanniep on Mon, 2015-03-30 09:44

Hi David,

Thanks! It does exactly what I asked for. But I formulated my request a bit too simple. It would be perfect if I could set a condition to the price adjustment. More a if..then.. expression. For example:

If Brand = "Apple" then Price +10% or If Price is < 10 then +5. So instead of applying the logic to the entire feed I would like to be able to filter on a field or set a condition.

I could split the feed per Brand and then increase the Price for the Brands that apply but this wouldn't help me if I want to set a Price condition.

Is it possible to add this logic to the Price Adjust filter above?

Sander

Submitted by support on Mon, 2015-03-30 10:27

Hi Sander,

If you want to change prices in different ways depending on condition it's probably best to hard code it into the import record handler, but that's straight forward to do. Based on your above examples, in includes/tapestry.php look for the following code around line 333:

  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);

...and REPLACE with:

  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);
  if ($importRecord["brand"] == "Apple")
  {
    $importRecord["price"] = ($importRecord["price"]/100)*(100+10);
  }
  elseif($importRecord["price"] < 10)
  {
    $importRecord["price"] = $importRecord["price"] + 5;
  }
  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);

Cheers,
David.
--
PriceTapestry.com

Submitted by sanniep on Mon, 2015-03-30 12:25

Hi David,

Thanks! This would solve the problem if the conditions are the same. But I want to apply these conditions on each feed and on multiple brands or price levels. This will result in a bunch of conditions to manage. It would be great to add this to the filter configuration. Like the Price adjust filter you made but then with something like:

Price Adjust (Field based)
If (choose field) : [BRAND]
Then amount: % or EUR

Price Adjust (Price based)
If PRICE is: then: EUR
Then amount: % or EUR

Would it be possible to create these 2 Price Adjust filters?

Submitted by support on Mon, 2015-03-30 14:11

Hello Sander,

Have a go with the following for adjustment by field match, or by price. Before implementing you will need to make a small change to includes/admin.php to make $importRecord global. In that file, look for the following code at line 140:

    global $filter_record;

...and REPLACE with:

    global $filter_record;
    global $importRecord;

New code to add to includes/filter.php:

  /*************************************************/
  /* priceAdjustByField */
  /*************************************************/
  $filter_names["priceAdjustByField"] = "Price Adjust by Field";
  function filter_priceAdjustByFieldConfigure($filter_data)
  {
    global $config_currencyHTML;
    global $config_fieldSet;
    widget_selectArray("Field","field",TRUE,$filter_data["field"],$config_fieldSet);
    widget_textBox("Field Value","value",TRUE,$filter_data["value"],"",3);
    widget_textBox("Adjustment","amount",TRUE,$filter_data["amount"],"",3);
    widget_selectArray("","type",TRUE,$filter_data["type"],array("#"=>$config_currencyHTML,"%"=>"%"));
  }
  function filter_priceAdjustByFieldValidate($filter_data)
  {
    if ($filter_data["amount"]=="") widget_errorSet("amount","required field");
  }
  function filter_priceAdjustByFieldExec($filter_data,$text)
  {
    global $importRecord;
    if ($importRecord[$filter_data["field"]]!=$filter_data["value"]) return $text;
    $text = tapestry_decimalise($text);
    if ($filter_data["type"]=="#")
    {
      $text = ($text + $filter_data["amount"]);
    }
    else
    {
      $text = ($text / 100) * (100 + $filter_data["amount"]);
    }
    $text = tapestry_decimalise($text);
    return $text;
  }
  /*************************************************/
  /* priceAdjustByPrice */
  /*************************************************/
  $filter_names["priceAdjustByPrice"] = "Price Adjust by Price";
  function filter_priceAdjustByPriceConfigure($filter_data)
  {
    global $config_currencyHTML;
    global $config_fieldSet;
    widget_textBox("If Price Less Than","price",TRUE,$filter_data["price"],"",3);
    widget_textBox("Adjustment","amount",TRUE,$filter_data["amount"],"",3);
    widget_selectArray("","type",TRUE,$filter_data["type"],array("#"=>$config_currencyHTML,"%"=>"%"));
  }
  function filter_priceAdjustByPriceValidate($filter_data)
  {
    if ($filter_data["price"]=="") widget_errorSet("price","required field");
    if ($filter_data["amount"]=="") widget_errorSet("amount","required field");
  }
  function filter_priceAdjustByPriceExec($filter_data,$text)
  {
    global $importRecord;
    $price = tapestry_decimalise($importRecord["price"]);
    if ($price >= $filter_data["price"]) return $text;
    $text = tapestry_decimalise($text);
    if ($filter_data["type"]=="#")
    {
      $text = ($text + $filter_data["amount"]);
    }
    else
    {
      $text = ($text / 100) * (100 + $filter_data["amount"]);
    }
    $text = tapestry_decimalise($text);
    return $text;
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by sanniep on Mon, 2015-03-30 18:13

David, it's perfect! Thanks! The only thing left is a nice round function. Because of the adding a amount or percentage the prices become less visually attractive.. Is there a way to round the prices to the nearest x.xxx,95 or something configurable?

Anyhow, thanks a lot for your effort up until this point!

Submitted by support on Tue, 2015-03-31 07:47

Hi Sander,

At the end of the exec function of each filter you'll see this line:

  $text = tapestry_decimalise($text);

To round to to the nearest whole unit of currency, REPLACE with:

  $text = tapestry_decimalise(intval($text));

Or to to round up (or down slightly) to .95 have a go with:

  $text = tapestry_decimalise(floor($text)+0.95);

Cheers,
David.
--
PriceTapestry.com

Submitted by sanniep on Tue, 2015-03-31 17:34

Awesome! Thanks!