首页 文章

更新产品的所有Tier价格

提问于
浏览
3

无需直接查询Magento数据库 . 如何根据数量和客户群体删除特定产品的所有等级价格?

然后为此产品添加新的层级价格 .

例:
使用SKU:FD001删除产品的所有层级价格
客户组ID为:4

PHP 5.3 Magento 1.4.2

4 回答

  • 0

    您首先必须取消设置层级价格并保存产品,然后添加层级价格并再次保存 .

    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();
    
  • 3

    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 . 我必须弄清楚如何根据等级价格筛选出来的东西......

    <?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();
    }
    
  • 1

    我最终通过使用直接数据库查询来解决这个问题 .

    一如既往,我正在寻找更好的答案 .

    我的Hacky解决方案:

    $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)'
        );
    
  • 2

    @omouse 's answer pointed us to 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
    }
    

相关问题