php – 在prestashop中定制价格计算

我正在使用Prestashop 1.5.x网站,我需要为特定产品添加自定义的价格计算规则.
我的目标是每订单增加10美元,但是PS会按产品数量增加额外的费用,所以如果你订购20个产品,它会要求你200 $而不是10 …
我需要覆盖/classes/Product.PHP中的计算过程,类似于:
if (product_id = 44) { $price = $price + 10; }
else { $price = $price }

你有什么想法吗

您必须在prestashop中创建Product类的覆盖.为此,请在覆盖/类中创建一个名为Product.PHP的新文件,并将此代码放在其中:
<?PHP
class Product extends ProductCore
{
    // Here we will put every method or property override
}

在这个类中,您将复制/粘贴静态方法priceCalculation(在我的原始Product.PHP文件的第2567行).完成后,只需在方法的最后添加这些行,就在self :: $_price [$cache_id] = $price之前; :

if ($id_product == 44 && Context::getContext()->customer->isLogged()) {
        $customer = Context::getContext()->customer;

        $nbTimesBoughtThisProduct = (int) Db::getInstance()->getValue('
            SELECT COUNT(*)
            FROM `' . _DB_PREFIX_ . 'product` p
            JOIN `' . _DB_PREFIX_ . 'order_detail` od
            ON p.`id_product` = od.`product_id`
            JOIN `' . _DB_PREFIX_ . 'orders` o
            ON od.`id_order` = o.`id_order`
            WHERE o.`id_customer` = ' . $customer->id . '
            AND p.`id_product` = ' . $id_product . '
        ');

        $price += $nbTimesBoughtThisProduct * 10;
    }

我没有时间测试这些,但我认为这是做你想要做的方式.

priceCalculation是每次Prestashop需要产品价格的方法.通过将此代码放在此方法的最后,我们修改返回的价格.

代码首先检查客户是否被记录(如果没有,我们不能从他那里获得订单).如果是这样,查询会检索客户过去购买此产品的次数.该数字乘以十,并将该值添加到价格.

编辑:如果,如Cyril Tourist所说,你还要计算当前的购物车,得到这个新的代码(仍然没有测试,但应该工作):

if ($id_product == 44 && Context::getContext()->customer->isLogged()) {
        $customer = Context::getContext()->customer;

        $nbTimesBoughtThisProduct = (int) Db::getInstance()->getValue('
            SELECT COUNT(*)
            FROM `' . _DB_PREFIX_ . 'product` p
            JOIN `' . _DB_PREFIX_ . 'order_detail` od
            ON p.`id_product` = od.`product_id`
            JOIN `' . _DB_PREFIX_ . 'orders` o
            ON od.`id_order` = o.`id_order`
            WHERE o.`id_customer` = ' . $customer->id . '
            AND p.`id_product` = ' . $id_product . '
        ');

        $productsInCart = Context::getContext()->cart->getProducts();

        foreach ($productsInCart as $productInCart) {
            if ($productInCart['id_product'] == 44) {
                $nbTimesBoughtThisProduct++;
            }
        }

        $price += $nbTimesBoughtThisProduct * 10;
    }

此外,我建议您将“44”产品ID存储在常量,配置变量或任何东西中,但不保留在代码中.我只是为了这个例子.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...