您必须在prestashop中创建Product类的覆盖.为此,请在覆盖/类中创建一个名为Product.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存储在常量,配置变量或任何东西中,但不保留在代码中.我只是为了这个例子.