Prestashop:循环 $product->save() 方法 [英] Prestashop : loop on $product->save() method
本文介绍了Prestashop:循环 $product->save() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为 Prestashop 创建一个模块,它需要在每次更改产品时重新计算价格.这是我的代码:
I'm creating a module for Prestashop and it need at each change on a product to recalculate the price. This is my code :
public function hookActionProductUpdate($params)
{
if ($this->context->controller->controller_type == 'front')
return ;
$id = $params['id_product'];
$po = new Product($id);
$cats = Product::getProductCategories($po->id);
foreach ($cats as $cat)
{
if ($cat == "49")
{
$ok = 1;
break 1;
}
else
$ok = 0;
}
if ($ok == 0)
return 0;
$po->price_tax_exc = $this->CalculatePrice($po);
var_dump($po->price_tax_exc);
$po->save(); // THE PROBLEM IS HERE
}
问题是当我点击保存时,我永远不会回到管理员的产品列表.当我用 add() 替换 save() 时,这是同一件事,但它会在循环中创建产品.所以 save() 处于循环中,我不知道为什么...
The problem is when i click on save, i will never go back to the product list on the admin. When I replace save() by add() this is the same thing but it create product in a loop. So save() is in a loop and I don't know why...
推荐答案
您至少可以通过两种方式解决此问题:
You can fix this in at least two ways:
1.
protected $price_updated = false;
public function hookActionProductUpdate($params)
{
if ($this->price_updated)
return null;
.........
if ($po->save())
$this->price_updated = true;
}
- 直接在数据库中更新值(它会再次触发你的钩子).我无法在产品表中找到引用
price_tax_exc
的列,我对价格没有太多经验:( 也许wholesale_price
?
- Updating the value directly in the database (it would trigger you hook again). I haven't been able to find column inside product table which refers to
price_tax_exc
, I don't have much experience with prices :( Maybewholesale_price
?
这篇关于Prestashop:循环 $product->save() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文