脚本宝典收集整理的这篇文章主要介绍了php – 在prestashop中定制价格计算,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
if (product_id = 44) { $price = $price + 10; } else { $price = $price }
你有什么想法吗
<?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存储在常量,配置变量或任何东西中,但不保留在代码中.我只是为了这个例子.
以上是脚本宝典为你收集整理的php – 在prestashop中定制价格计算全部内容,希望文章能够帮你解决php – 在prestashop中定制价格计算所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。