Reputation: 3
I have this module that contains a hook that reduces the product price by 2 and I have for example a product with an original price of 22 and becomes 16 also tried some echos and it echos twice so I assumed it gets fired twice
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class HalvePriceOutOfStock extends Module
{
public function __construct()
{
$this->name = 'halvepriceoutofstock';
$this->tab = 'pricing_promotion';
$this->version = '0.1.0';
$this->author = 'Ali Fertah';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Halve Price for Out-of-Stock Products');
$this->description = $this->l('Displays out-of-stock products with half the original price.');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() && $this->registerHook('displayProductPriceBlock');
}
public function uninstall()
{
return parent::uninstall();
}
public function hookDisplayProductPriceBlock($params)
{
$params['product']['price'] = $this->updateProductPrice($params);
$this->context->smarty->assign(array(
'product_price' => $params['product']['price']
));
}
public function updateProductPrice($params)
{
$product = $params['product'];
$rawPrice = trim($product['price']);
if ($product['quantity'] <= 0) {
$cleanedPrice = preg_replace('/[^\d,.]/', '', $rawPrice);
if (strpos($cleanedPrice, ',') !== false && strpos($cleanedPrice, '.') === false) {
$cleanedPrice = str_replace(',', '.', $cleanedPrice);
} elseif (strpos($cleanedPrice, ',') !== false && strpos($cleanedPrice, '.') !== false) {
$cleanedPrice = str_replace(',', '', $cleanedPrice);
}
$priceFloat = (float)$cleanedPrice;
$priceFloat = $priceFloat - 2;
$formattedPrice = number_format($priceFloat, 2, '.', '');
return $formattedPrice;
} else {
return $rawPrice;
}
}
}
I tried to do a variable that checks if it got executed or not but it doesn't execute for the rest of the products
Upvotes: 0
Views: 75
Reputation: 168
The hook you are currently using is only for displaying the price, it will not do anything regarding the price. Try it with this hook: actionProductPriceCalculation
Upvotes: 0