【问题标题】:Custom price calculation in prestashopprestashop 中的自定义价格计算
【发布时间】:2015-03-15 09:35:43
【问题描述】:

我正在开发 Prestashop 1.5.x 网站,我需要为特定产品添加自定义价格计算规则。 我的目标是每个订单增加 10 美元,但 PS 按产品数量增加额外成本,所以如果您订购 20 件产品,它会要求您 200 美元而不是 10 美元... 我需要覆盖 /classes/Product.php 中的计算过程,例如:

if (product_id = 44) { $price = $price + 10; }
else { $price = $price }

你有什么想法吗?

【问题讨论】:

  • 我实际上正在尝试解决这样的问题。我在 atm 学到的是,最常见的方法是覆盖 product.php 类中的一些价格函数,但我试图找出要覆盖的函数。
  • @Nolwennig 实际上,else 没用。
  • @Fabio 首先,它应该(如果它不是常量)是$product_id,其次$product_id = 44 始终是true{ $price = $price + 10; } 总是被执行,第三是else声明没用。 正确的语法会更像if ($product_id == 44) {$price += 10}
  • @sitilge 是的,我删除它

标签: php e-commerce prestashop prestashop-1.5


【解决方案1】:

您必须在 prestashop 中创建 Product 类的覆盖。为此,请在 override/classes 中创建一个名为 Product.php 的新文件并将此代码放入其中:

<?php
class Product extends ProductCore
{
    // Here we will put every method or property override
}

在本课程中,您将复制/粘贴静态方法priceCalculation(对我来说它位于原始 Product.php 文件的第 2567 行)。完成后,只需在方法末尾添加这些行,就在 self::$_prices[$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 需要产品价格时调用的方法。通过将此代码放在该方法的末尾,我们修改了返回的价格。

代码首先检查客户是否已登录(如果他未登录,我们将无法从他那里获得订单)。如果是这样,查询会检索该客户过去购买此产品的次数。这个数字乘以 10,然后价值被添加到价格中。

编辑:如果正如 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 存储在常量、配置变量或任何内容中,但不要将其保存在这样的代码中。我这样做只是为了示例。

【讨论】:

  • 根据我对问题的理解,他希望该特定产品的每个订单静态 10 美元。所以它应该在 Cart 类级别的某个地方“操纵”。像 $totalProductCost = ($productPrice * $productQuantity) + 10$
  • 我编辑了我的答案以考虑添加购物车。谢谢你提醒我这件事。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2018-10-31
相关资源
最近更新 更多