Magento产品添加了个自定义属性Free Shipping(免运费)。要实现当Free shipping属性的值为YES的时候,自动把产品指定到一个叫Free Shipping的目录。
P,前台还可以处理下,使有这属性值的产品添加标识条幅。如图,效果还是挺惹人喜的。
让Magento产品自动添加到指定目录 我的处理办法是,重写“app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php”这个文件。
在_initProductSave方法里的
/**
* Initialize product categories
*/
这个步骤添加额外处理。
完整代码为:
// addEd at: 10:20 2011/7/30 by chen@sjolzy.cn
/*
$productData['free_shipping']: 21 yes 20 no
Free Shipping Specials category id: 31
*/
$ifFreeShipping = (int)$productData['free_shipping']===21?true:false;
$freeShippingCategoryId = 31;
// addEd at: 10:20 2011/7/30 by chen@sjolzy.cn
if (null !== $categoryIds) {
if (empty($categoryIds)) {
$categoryIds = $ifFreeShipping?$freeShippingCategoryId:array();
}else{
$categoryIds = explode(',',$categoryIds);
$categoryIds = $this->freeShippingCategoryIdProcess($freeShippingCategoryId,$ifFreeShipping,$categoryIds);
}
$product->setCategoryIds($categoryIds);
}else{ // addEd at: 10:20 2011/7/30 by chen@sjolzy.cn
$categoryIds = Mage::getResourceSingleton('catalog/product')->getCategoryIds($product);
if($categoryIds){
$categoryIds = is_array($categoryIds)?$categoryIds:array($categoryIds);
$categoryIds = $this->freeShippingCategoryIdProcess($freeShippingCategoryId,$ifFreeShipping,$categoryIds);
$product->setCategoryIds($categoryIds);
}
} // addEd at: 10:20 2011/7/30 by chen@sjolzy.cn
另外添加的freeShippingCategoryIdProcess方法是
protected function freeShippingCategoryIdProcess($freeShippingCategoryId,$ifFreeShipping,$categoryIds){
if(!$ifFreeShipping){
$k = array_search($freeShippingCategoryId,$categoryIds);
if(false!==$k){
unset($categoryIds[$k]);
}
}else{
$categoryIds[] = $freeShippingCategoryId;
}
return implode(',',$categoryIds);
}
经过测试,达到预期效果,产品设置YES后自动添加到free shipping的目录,No了之后再取消。
(责任编辑:最模板) |