magento更新scope为website的product attribute

有时候产品的属性为website,无法直接修改产品属性使其在不同的Website呈现不同的值.可以参考如下代码:

首先需要设admin store, Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

第二步,设置product的scope, 

 $website = Mage::app()->getWebsite($website);
$store = $website->getDefaultStore(); 
$product->setStoreId($store->getStoreId());

参考下面的代码:

We must set the admin store first, and then set the scope of the specific product later ($product->setStoreId($store->getStoreId());) You may also notice lines like this: $product->setSpecialToDateIsFormated(true);. We need these to pass in a date as a string and tell Magento that it is indeed a date and should be stored that way.

public function updateSpecialPriceByWebsite($price, $specialFromDate, $specialToDate, $sku, $website)
    {
        try {
            $specialFromDate = date('Y-m-d', strtotime($specialFromDate));
            $specialToDate = date('Y-m-d', strtotime($specialToDate));
            $website = Mage::app()->getWebsite($website);
            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
            $store = $website->getDefaultStore();
            $product = Mage::getModel('catalog/product');
            $productId = $product->getIdBySku($sku);
            $product->load($productId);
            if ($product && $product->getId()) {
                $product->setStoreId($store->getStoreId());
                $product->setSpecialFromDate($specialFromDate);
                $product->setSpecialFromDateIsFormated(true);
                $product->setSpecialToDate($specialToDate);
                $product->setSpecialToDateIsFormated(true);
                $product->setSpecialPrice(strval($price));
                $product->save();
                return true;
            }
        } catch (Exception $e) {
            Mage::logException($e);
            return false;
        }

        return true;
    }

转载至: http://www.demacmedia.com/ecommerce/mini-tutorial-updating-products-within-website-scope/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值