您必須在prestashop中創建產品類的覆蓋。要做到這一點,創建一個名爲Product.php覆蓋/班一個新的文件,並把這個代碼在它:
<?php
class Product extends ProductCore
{
// Here we will put every method or property override
}
在這個類中,你會複製/粘貼靜態方法priceCalculation
(它在原有的2567線我的Product.php文件)。完成後,只需添加這些行的方法結束,就在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,並將該值加到價格中。
編輯:如果像西里爾遊客說,你要也算目前的車,得到這個新的代碼(還沒有測試,但應工作):
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存儲在一個常量,配置變量或任何內容中,但不保存在代碼中。我只是爲了這個例子。
我實際上試圖解決這樣的問題。 我所學到的atm最常見的方法是重寫product.php類中的一些價格函數,但我試圖弄清楚哪個函數可以覆蓋。 –
@Nolwennig其實,'else'沒用。 – sitilge
@Fabio首先,它應該(如果它不是常量)是'$ product_id',其次'$ product_id = 44'總是'true'和'{$ price = $ price + 10; }'總是被執行,第三,'else'語句沒用。 * correct *語法會更像'if($ product_id == 44){$ price + = 10}' – sitilge