加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在prestashop中定制价格计算

发布时间:2020-12-13 13:56:59 所属栏目:PHP教程 来源:网络整理
导读:我正在使用Prestashop 1.5.x网站,我需要为特定产品添加自定义的价格计算规则. 我的目标是每订单增加10美元,但是PS会按产品数量增加额外的费用,所以如果你订购20个产品,它会要求你200 $而不是10 … 我需要覆盖/classes/Product.php中的计算过程,类似于: if (
我正在使用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存储在常量,配置变量或任何东西中,但不保留在代码中.我只是为了这个例子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读