prestashop 8.1.7 模块钩子被多次触发

问题描述 投票:0回答:1

我有这个模块,其中包含一个可以将产品价格降低 2 的钩子,例如,我有一个产品原始价格是 22,变成 16 也尝试了一些回声,它回声了两次,所以我假设它被解雇了两次

<?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;
        }
    }
}

我尝试做一个变量来检查它是否被执行,但它不会对其余产品执行

prestashop prestashop-1.6 prestashop-1.7 prestashop-modules prestashop-8
1个回答
0
投票

您当前使用的钩子仅用于显示价格,它不会对价格进行任何操作。 用这个钩子试试:actionProductPriceCalculation

© www.soinside.com 2019 - 2024. All rights reserved.