soduno Posted November 1, 2015 Share Posted November 1, 2015 (edited) 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 November 1, 2015 by soduno (see edit history) Link to comment Share on other sites More sharing options...
herve25 Posted November 2, 2015 Share Posted November 2, 2015 Hello,You can use the class SpecificPrice where everything you wanted is managed :-)https://github.com/PrestaShop/PrestaShop/blob/develop/classes/SpecificPrice.php Link to comment Share on other sites More sharing options...
soduno Posted November 3, 2015 Author Share Posted November 3, 2015 (edited) 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 November 3, 2015 by soduno (see edit history) 1 2 Link to comment Share on other sites More sharing options...
herve25 Posted November 3, 2015 Share Posted November 3, 2015 Your code seems good.That's a weird behavior, which version of prestashop do you use ? Link to comment Share on other sites More sharing options...
soduno Posted November 4, 2015 Author Share Posted November 4, 2015 (edited) Your code seems good. That's a weird behavior, which version of prestashop do you use ? Very weird indeed. It's the latest Prestashop version. It does the same on another test version, that is an older 1.6 version :/ Edited November 4, 2015 by soduno (see edit history) Link to comment Share on other sites More sharing options...
sandipchandela Posted April 19, 2020 Share Posted April 19, 2020 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.'); } } 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now