Jump to content

Create special product price via php


soduno

Recommended Posts

I am creating my own module, so far it works great.

However I am looking for a function to set a special price(with date range) for a specific/allready added product via PHP.

Also a function to delete that special price just set. Anyone knows if such functionalities exsists in prestashop?

Off course its possible to do it manually in the product section, but I want to be able to set it via Php  :)

 

Best, Simon

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

Thanks for your suggestion(herve25). In the meantime I found out the same thing via admin product controller  :)

But. A new issue accured. When I insert a special(specific) price via PHP, it deletes the allready added special prices for the given product. Any Idear why?

 

I use following code:

$specificPrice = new SpecificPrice();
$specificPrice->id_product = $featuredProductId;
$specificPrice->id_product_attribute = (int)$id_product_attribute;
$specificPrice->id_shop = 1;
$specificPrice->id_currency = 0;
$specificPrice->id_country = 20;
$specificPrice->id_group = 0;
$specificPrice->id_customer = 0;
$specificPrice->price = '-1';
$specificPrice->from_quantity = 1;
$specificPrice->reduction = $discountPrice;
$specificPrice->reduction_type = 'amount';
$specificPrice->reduction_tax = 1;
$specificPrice->from = $countDownTo;
$specificPrice->to = $expire;
$specificPrice->add();
$addedPriceId = $specificPrice->id;
Edited by soduno (see edit history)
  • Like 1
  • Thanks 2
Link to comment
Share on other sites

  • 4 years later...

It is working for me in PS 1.6.1.20

Making this $product_ids array as follows:

$product_ids = array(
            array('product_id' => '8', 'incl_tax' => 1, 'price' => 9, 'from_quantity' => 1, 'reduction_price' => '10', 'reduction_percent' => 2, 'reduction_from' => '0000-00-00 00:00:00', 'reduction_to' => '0000-00-00 00:00:00'),
            array('product_id' => '2', 'from_quantity' => 1, 'reduction_price' => '8', 'reduction_percent' => 2, 'reduction_from' => '0000-00-00 00:00:00', 'reduction_to' => '0000-00-00 00:00:00'),
            array('product_id' => '18', 'from_quantity' => 5, 'reduction_price' => '5', 'reduction_percent' => 2, 'reduction_from' => '0000-00-00 00:00:00', 'reduction_to' => '0000-00-00 00:00:00'),
            array('product_id' => '2', 'from_quantity' => 5, 'reduction_price' => '3', 'reduction_percent' => 2, 'reduction_from' => '0000-00-00 00:00:00', 'reduction_to' => '0000-00-00 00:00:00'),
            array('product_id' => '8', 'from_quantity' => 40,  'reduction_percent' => 40, 'reduction_from' => '0000-00-00 00:00:00', 'reduction_to' => '0000-00-00 00:00:00')
        );

 

Call below method by looping `product_id`

foreach ($product_ids as $row) {
  $pid = (int)$row['product_id'];


  $result = array();
  if ((isset($row['reduction_price']) && $row['reduction_price'] > 0) ||
  (isset($row['reduction_percent']) && $row['reduction_percent'] > 0)) {
  $result['item'] = $this->addSpecificPrice($pid, $row);
  $updated++;
  } else {
  $result['error'] = $pid;
  $fail++;
  }
}
public function addSpecificPrice($pid, $info)
    {
        try {
            $id_shop = $this->getDefaultPSShop();
            $specific_price = SpecificPrice::getSpecificPrice((int)$pid, $id_shop, 0, 0, 0, (int)$info['from_quantity'], 0, 0, 0, 0);

            if (is_array($specific_price) && isset($specific_price['id_specific_price'])) {
                $specific_price = new SpecificPrice((int)$specific_price['id_specific_price']);
            } else {
                $specific_price = new SpecificPrice();
            }

            $specific_price->id_product = (int)$pid;
            $specific_price->id_specific_price_rule = 0;
            $specific_price->id_shop = $id_shop;
            $specific_price->id_currency = 0;
            $specific_price->id_country = 0;
            $specific_price->id_group = 0;
            $specific_price->price = (isset($info['price']) && $info['price']) ? $info['price'] : -1;
            $specific_price->id_customer = 0;

            $specific_price->from_quantity = (isset($info['from_quantity']) && $info['from_quantity']) ? $info['from_quantity'] : 1;

            if ($specific_price->price > 1) {
                $specific_price->reduction = 0;
                $specific_price->reduction_type ='amount';
            } else {
                $specific_price->reduction = (isset($info['reduction_price']) && $info['reduction_price']) ? (float)str_replace(',', '.', $info['reduction_price']) : $info['reduction_percent'] / 100;
                $specific_price->reduction_type = 'percentage';
            }
            $specific_price->reduction_tax = (isset($info['incl_tax']) && $info['incl_tax']) ? 1 : 0;

            //$specific_price->reduction_type = (isset($info['reduction_price']) && $info['reduction_price']) ? 'amount' : 'percentage';

            $specific_price->from = (isset($info['reduction_from']) && Validate::isDate($info['reduction_from'])) ? $info['reduction_from'] : '0000-00-00 00:00:00';
            $specific_price->to = (isset($info['reduction_to']) && Validate::isDate($info['reduction_to'])) ? $info['reduction_to'] : '0000-00-00 00:00:00';

            if (!$specific_price->save()) {
                return Tools::displayError('An error occurred while updating the specific price.');
            }
            return Tools::displayError('Specific Price has been added/updated.');
        } catch (\Exception $e) {
            return Tools::displayError('An error occurred while updating the specific price.');
        }
    }

 

  • Like 1
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...