`
kirenenko04
  • 浏览: 146450 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

add a product into a category & remove a product from a category

阅读更多
	public function addToCategory($sku,$categoryId)
	{
		$category = Mage::getModel('catalog/category')->load($categoryId);
		$positions = $category->getProductsPosition();
		$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
		$productId = $product->getId();
		$positions[$productId] = $position;
		$category->setPostedProducts($positions);
		try {
			$category->save();
		} catch (Mage_Core_Exception $e) {
			Mage::log('data_invalid' . $e->getMessage());
		}
		
	}
	
	public function removeFromCategory($sku,$categoryId) {
		$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
		$category = Mage::getModel('catalog/category')->load($categoryId);
		$positions = $category->getProductsPosition();
		$productId = $product->getId();
		
		if (!isset($positions[$productId])) {
			Mage::log('product_not_assigned');
		}
		
		unset($positions[$productId]);
		
		$category->setPostedProducts($positions);

		try {
			$category->save();
		} catch (Mage_Core_Exception $e) {
			$this->_fault('data_invalid', $e->getMessage());
		}
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics