gpt4 book ai didi

php - prestashop 中的自定义价格计算

转载 作者:行者123 更新时间:2023-12-03 01:33:05 26 4
gpt4 key购买 nike

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

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

你有什么想法吗?

最佳答案

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

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

在本类(class)中,您将复制/粘贴静态方法 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 需要产品价格时调用的方法。通过将此代码放在该方法的末尾,我们可以修改返回的价格。

代码首先检查客户是否已登录(如果没有登录,我们就无法从他那里获取订单)。如果是这样,查询将检索该客户过去购买该产品的次数。该数字乘以十,并将该值添加到价格中。

编辑:如果,正如西里尔游客所说,您还想计算当前的购物车,请获取这个新代码(尚未测试,但应该可以工作):

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

关于php - prestashop 中的自定义价格计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27989752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com