【发布时间】:2011-12-13 16:56:12
【问题描述】:
无需直接查询 Magento 数据库。如何根据数量和客户群删除某个产品的所有等级价格?
然后为该产品添加新的等级价格。
示例:
删除具有 SKU:FD001 的产品的所有等级价格
其中客户组 ID 为:4
PHP 5.3 Magento 1.4.2
【问题讨论】:
标签: magento magento-1.4
无需直接查询 Magento 数据库。如何根据数量和客户群删除某个产品的所有等级价格?
然后为该产品添加新的等级价格。
示例:
删除具有 SKU:FD001 的产品的所有等级价格
其中客户组 ID 为:4
PHP 5.3 Magento 1.4.2
【问题讨论】:
标签: magento magento-1.4
您首先必须取消设置等级价格并保存产品,然后添加等级价格并再次保存。
Mage::getModel("catalog/product")->load(123)
->unsTierPrice()
->save()
->setTierPrice(array(
array(
'website_id' => 0,
'all_groups' => 0,
'cust_group' => 4,
'price' => 99.99,
'price_qty' => 1
),
array() // another tier, etc
))
->save();
【讨论】:
Mage::getModel("catalog/product")->load(123) ->unsTierPrice() ->save() ->load() ->setTierPrice(array( array( 'website_id' => 0, 'all_groups' => 0, 'cust_group' => 4, 'price' => 99.99, 'price_qty' => 1 ), array() // another tier, etc )) ->save();
我最终通过使用直接数据库查询解决了这个问题。
我一直在寻找更好的答案。
我的哈克解决方案:
$product = Mage::getModel('catalog/product')->load(9999);
$dbc = Mage::getSingleton('core/resource')->getConnection('core_write');
$dbc->query('DELETE FROM `catalog_product_entity_tier_price` WHERE `entity_id` = ' . intval($product->getId()) . ' AND `customer_group_id` IN(3,4,6,7) AND qty = 1');
$dbc->query(
'INSERT INTO `catalog_product_entity_tier_price` (`entity_id`,`all_groups`,`customer_group_id`,`qty`,`value`,`website_id`)
VALUES ('. intval($product->getId()) . ',0,' . intval($id) . ',1,' . floatval($price) . ',0)'
);
【讨论】:
There's an API available for the product tier price.
或者,您可以使用一些使用 Magento 代码的 PHP 代码来查询符合这些条件的产品列表,然后调整每个产品。 Alan Storm has an article about how Model Collections work (they're the type of object that you would use for this).
基本上删除产品是这样的,我不确定你会如何设置等级价格,但你可以看看the generated phpdoc documentation。我正在选择与 customer_group 4 匹配的每个产品,然后删除每个产品。您必须弄清楚如何根据等级价格过滤掉东西......
<?php
require 'app/Mage.php';
$app = Mage::app('default'); // Mage_Core_Model_App
$store = $app->getStore(); // Mage_Core_Model_Store
$products = Mage::getModel('catalog/product')->getCollection();
// filter out anything that doesnt match the customer group 4
$products->addFieldToFilter('customer_group', '4');
// loop through each product and delete it
for ($products->load() as $product) {
$product->delete();
}
【讨论】:
@omouse 的回答将我们指向 Magento 的 API 以获取层级价格。 [Updated Link for Magento 1.X's Tier Price API] 注意:我正在使用 Magento 1.9.2.2。
在寻找更新产品等级价格的最有效方法(不使用直接 SQL)后,我发现 API 是最快的方法。它仍然很慢,但它比我发现的其他方法要好,建议执行以下操作:
// Works, but is slow
$product = Mage::getModel('catalog/product')->load($productId);
$product->unsTierPrice();
$product->save();
$product->load();
$product->setTierPrice($tiers);
$product->save();
更好的是,我将层数组数组变成了层对象数组,并使用 API 函数更新产品:
// Build the array of tier level objects
foreach ($tierLevels as $tierLevel) {
$tiers[] = (object) array(
'website' => 'all',
'customer_group_id' => 'all',
'qty' => $tierLevel['min_qty'],
'price' => $tierLevel['unit_price']
);
}
// Use the API to do the update
try {
$tierPriceApi = Mage::getSingleton('catalog/product_attribute_tierprice_api_v2');
$tierPriceApi->update($productId, $tiers);
}
catch (Exception $e) {
// Handle the exception
}
【讨论】:
我搜索了一段时间找到了一个解决方案,它不是纯 SQL 并且不需要产品保存,因为我们要更新 50k 产品目录的等级价格,所以保存所有产品肯定不是解决方案。 我想我终于找到了一个好方法。
Mage::getResourceSingleton('catalog/product_attribute_backend_tierprice')
->deletePriceData($product->getId());
$newTierPrice['entity_id'] = $product->getId()
$newTierPrice['all_groups'] = 1;
$newTierPrice['customer_group_id'] = 0;
$newTierPrice['qty'] = 42;
$newTierPrice['value'] = 100;
$newTierPrice['website_id'] = 0;
Mage::getResourceSingleton('catalog/product_attribute_backend_tierprice')
->savePriceData(new Varien_Object($newTierPrice));
【讨论】: