Magento产品层价格在产品图像更新时删除
我创建了一个脚本以编程方式更新我的产品图片,但我的脚本删除了Magento产品层价格在产品图像更新时删除,magento,Magento,我创建了一个脚本以编程方式更新我的产品图片,但我的脚本删除了$product->save()上的所有tier_价格 以下是我的bilder更新脚本: foreach ($productCollection as $product) { $formatted_sku = $product->getSku(); $config = $product->getMediaConfig(); // JPG files verification $jpg_
$product->save()上的所有tier_价格代码>
以下是我的bilder更新脚本:
foreach ($productCollection as $product) {
$formatted_sku = $product->getSku();
$config = $product->getMediaConfig();
// JPG files verification
$jpg_file = $images_folder.$formatted_sku.".".$extension[0];
if (file_exists($jpg_file) ) {
$fileurl = $config->getMediaUrl($jpg_file);
$product->addImageToMediaGallery($jpg_file, $visibility, false, false);
$product->save();
}
}
我如何避免更新我的tier_价格
非常感谢。您是如何创建的$productCollection
?可能产品中没有填充所需的数据(tier_prices),因此save()
persist product不包含该数据。尝试使用addAttributeToSelect()
添加一些要选择的属性。如何创建该$productCollection
?可能产品中没有填充所需的数据(tier_prices),因此save()
persist product不包含该数据。尝试使用addAttributeToSelect()添加一些要选择的属性,我遇到了相同的问题。我最终以一种非常奇怪的方式解决了这个问题,但它确实奏效了。您只需创建一个“假”tierprice:
$tierPrices = array(
'website_id' => 0,
'cust_group' => 2,
'price_qty' => 3,
'price' => 10
);
(请注意,没有[])
然后添加它(实际上它不会添加任何内容),但您需要这样做:
$product->setTierPrice($tierPrices);
最后保存产品:
$product->save();
它将保存您的产品,而无需删除旧的分层价格。希望能有帮助 我遇到了同样的问题。我最终以一种非常奇怪的方式解决了这个问题,但它确实奏效了。您只需创建一个“假”tierprice:
$tierPrices = array(
'website_id' => 0,
'cust_group' => 2,
'price_qty' => 3,
'price' => 10
);
(请注意,没有[])
然后添加它(实际上它不会添加任何内容),但您需要这样做:
$product->setTierPrice($tierPrices);
最后保存产品:
$product->save();
它将保存您的产品,而无需删除旧的分层价格。希望能有帮助 对于那些仍然遇到这个问题的人,有一个简单的修复方法可以尝试。看起来tierprice数据不是由产品的默认getModel读取的。要解决这个问题,只需调用产品的getTierPrice方法来加载它
$tp=$product->getTierPrice();
你不需要做任何其他事情,只需加载它。然后,当您保存产品时,将保存分层定价数据。对于仍遇到此问题的用户,有一个简单的修复方法可以尝试。看起来tierprice数据不是由产品的默认getModel读取的。要解决这个问题,只需调用产品的getTierPrice方法来加载它
$tp=$product->getTierPrice();
你不需要做任何其他事情,只需加载它。然后,当您保存产品时,将保存分层定价数据。本文中其他人的评论帮助我找到了一个有效的解决方案。对我来说,只需将层价格设置为false,就可以防止其被覆盖/修改
$product->setTierPrice(false);
我个人更喜欢这个选项而不是其他一些解决方案,因为它干净、简单,不设置假值,而且有效。在这些方面,我更希望GregC提供的解决方案能够工作,因为它只是加载层价格,但在我的测试中,这并没有像预期的那样工作-层价格仍然被删除
下面是OP中代码的修改版本
foreach ($productCollection as $product) {
$formatted_sku = $product->getSku();
$config = $product->getMediaConfig();
// JPG files verification
$jpg_file = $images_folder.$formatted_sku.".".$extension[0];
if (file_exists($jpg_file) ) {
$fileurl = $config->getMediaUrl($jpg_file);
$product->addImageToMediaGallery($jpg_file, $visibility, false, false);
$product->setTierPrice(false); // set tier price to false to prevent it from being overwritten
$product->save();
}
}
这段代码经过测试并与MagentoEE1.14.12.0一起使用。其他人在这篇文章中的评论帮助我找到了一个有效的解决方案。对我来说,只需将层价格设置为false,就可以防止其被覆盖/修改
$product->setTierPrice(false);
我个人更喜欢这个选项而不是其他一些解决方案,因为它干净、简单,不设置假值,而且有效。在这些方面,我更希望GregC提供的解决方案能够工作,因为它只是加载层价格,但在我的测试中,这并没有像预期的那样工作-层价格仍然被删除
下面是OP中代码的修改版本
foreach ($productCollection as $product) {
$formatted_sku = $product->getSku();
$config = $product->getMediaConfig();
// JPG files verification
$jpg_file = $images_folder.$formatted_sku.".".$extension[0];
if (file_exists($jpg_file) ) {
$fileurl = $config->getMediaUrl($jpg_file);
$product->addImageToMediaGallery($jpg_file, $visibility, false, false);
$product->setTierPrice(false); // set tier price to false to prevent it from being overwritten
$product->save();
}
}
这段代码在我开始使用的时候已经与MagentoEE1.14.12.0一起测试和使用:$productCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
然后我添加了->addAttributeToFilter
来测试这样的产品:`$productCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('SKU','SA80/80');`但是我得到的结果总是一样的。也许你需要这样做Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('SKU',SA80/80')代码>我仍然需要heeeeelp!!:(Bizboss您最终解决了这个问题吗?我已经在500个产品上运行了一个脚本,并丢失了层价格。在开始时,我使用了:$productCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*'))
然后我添加了->addAttributeToFilter
来只测试一个这样的产品:`$productCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('SKU','SA80/80');`但是我总是得到相同的结果。也许你需要这样做Mage::getModel('catalog/product'))->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('SKU',SA80/80');
我仍然需要HeeeeElp!!:(Bizboss你最终解决了这个问题吗?我已经在500个产品上运行了一个脚本,并且丢失了层价格。谢谢,我发现要更新一个产品层,你可以使用:$product->setTierPrice();$product->save();$product->SETTIERPICE($tier);$product->save();谢谢,我发现要更新产品层,您可以使用:$product->SETTIERPICE();$product->save();$product->SETTIERPICE($tier);$product->save();