在下面的脚本中,我遍历了一堆产品。对于其中的每一个,我想添加一个自定义选项“Eskestørrelse”。就像第一个产品上的魅力一样,但对于以下产品,以前的产品中的自定义选项会以某种方式保留,产品最终会有多个自定义选项。
例如 - 第10个产品将有10个自定义选项,这些选项是为产品1,2,3,...生成的选项。
我做错了什么?
继承脚本(如果您更喜欢pastie,请参阅http://pastie.org/1243529),自定义选项和产品保存为粗体:
$categoryId = 128;
$storeId = 4;
$cwd = getcwd();
chdir($launchdir);
echo "Entered $launchdir...\n";
require_once('includes/config.php');
require_once('app/Mage.php');
try {
$mageObj = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);;
$category = Mage::getModel('catalog/category')->load($categoryId);
$products = $category->getProductCollection()->addStoreFilter($storeId)->addAttributeToSelect('*');
echo "Found " . count($products) . " products in category #{$categoryId} + store #{$storeId}...\n";
$nodesc = array();
$n = 0;
foreach($products as $product) {
$sku = $product->getSku();
$desc = $product->getDescription();
$shortdesc = $product->getShortDescription();
$price = $product->getPrice();
$matches = array();
if (preg_match('/[Ee]ske med ([0-9]+)/', $desc, $matches)==0) {
$nodesc[] = $product;
}
else {
$product = Mage::getModel('catalog/product')->load($product->getId());
$halfqty = (int)($matches[1] / 2);
$halfpriceExact = ($price / 2) * 1.1;
$halfprice = ceil($halfpriceExact/10) * 10;
$pricediff = round(($halfprice / ($price / 2)) * 100) - 100;
$savepct = round(($halfprice*2 - $price)/($halfprice*2)*100);
echo "{$sku}: quantity ({$matches[1]}, {$halfqty}), price ({$price}, {$halfprice} ({$halfpriceExact}), half +{$pricediff}%), savepct {$savepct}% \n";
$newdesc = preg_replace('/([Ee])ske med [0-9]+/', "$1ske med {$halfqty} eller {$matches[1]} ", $desc);
$newshortdesc = preg_replace('/([Ee])ske med [0-9]+/', "$1ske med {$halfqty} eller {$matches[1]} ", $shortdesc);
$product->setPrice($halfprice);
$product->setDescription($newdesc . "\n\n<b>PS! </b>Du sparer $savepct% ved å kjøpe den største esken ({$matches[1]} stk).");
$product->setShortDescription($newshortdesc);
// figure out options
$newopts = array();
$newopts[] = array(
'title' => 'Eskestørrelse',
'type' => 'drop_down',
'previous_type' => null,
'previous_group' => 'select',
'is_require' => 1,
'is_delete' => null,
'sort_order' => 1,
'values' => array(
array(
'option_type_id' => -1,
'is_delete' => null,
'title' => "Eske med {$matches[1]} stk.",
'price' => ($price - $halfprice),
'price_type' => 'fixed',
'sku' => "-{$matches[1]}",
'sort_order' => '1'
),
array(
'option_type_id' => -1,
'is_delete' => null,
'title' => "Eske med {$halfqty} stk.",
'price' => 0.00,
'price_type' => 'fixed',
'sku' => "-{$halfqty}",
'sort_order' => '2'
)
)
);
if ($product->getOptionsReadonly()) {
echo "READONLY options, cant save...\n";
}
else {
$product->setProductOptions($newopts);
$product->setCanSaveCustomOptions(true);
$product->save();
$n++;
if ($n==2) die('temp stop');
}
}
$options = null;
$product = null;
}
echo "SUMMARY: " . count($products) . " products. " . count($nodesc) . " descriptions with no match. \n";
foreach ($nodesc as $product) {
echo "UNMATCHED: " . $product->getSku() . "\n";
}
}
catch (Exception $e) {
echo "Failed with exception " . $e . "\n";
}
chdir($cwd);
echo "Returned to $cwd...\n";
return;
?>
答案 0 :(得分:16)
经过几个小时之后,我在Magentos模型课程中坚持不懈地想出了这个。似乎产品选项是用单一模式设计的。
无需在第37行重新加载产品:
$product = Mage::getModel('catalog/product')->load($product->getId());
......这只是一个绝望的尝试来处理这个问题。我应该做的是重置Magentos单例Product_Option
实例中设置的多个选项。用下面的行替换第37行可以解决问题:
Mage::getSingleton('catalog/product_option')->unsetOptions(); // forget me and Magento will hate you
希望这可以为别人带来一些挫败感。 ;)