2015-01-16 68 views
12

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

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

你有什么想法吗?

+1

我实际上试图解决这样的问题。 我所学到的atm最常见的方法是重写product.php类中的一些价格函数,但我试图弄清楚哪个函数可以覆盖。 –

+2

@Nolwennig其实,'else'没用。 – sitilge

+1

@Fabio首先,它应该(如果它不是常量)是'$ product_id',其次'$ product_id = 44'总是'true'和'{$ price = $ price + 10; }'总是被执行,第三,'else'语句没用。 * correct *语法会更像'if($ product_id == 44){$ price + = 10}' – sitilge

回答

10

您必须在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存储在一个常量,配置变量或任何内容中,但不保存在代码中。我只是为了这个例子。

+2

从我对这个问题的理解中,他想要一个静态的10 $订购该特定产品。所以应该在购物车级别的某处“操纵”它。就像$ totalProductCost =($ productPrice * $ productQuantity)+ 10 $ –

+1

我编辑了我的答案,以考虑添加购物车。谢谢你提醒我这件事。 –

+0

谢谢你 – quardas