使用magento可以通过添加产品自定义属性为产品添加“免运费”属性,然后通过一定处理将免运费商品输出到特定目录。 在从事magento工作中,公司要求添加一个免运费的控制选项,来开关此产品的免运费状态。今天在网上找到一段代码,经过我的整理,能正常使用,也方便了那些可怜的编辑人员。我找到的内容是这样的: Magento产品添加了个自定义属性Free Shipping(免运费)。要实现当Free shipping属性的值为YES的时候,自动把产品指定到一个叫Free Shipping的目录。
前台的list.phtml还可以处理下,使有这属性值的产品添加标识条幅。如图,效果还是挺惹人喜的。
为了以后升级不影响功能,我的处理办法是,重写app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php这个文件。 在_initProductSave方法里找到下面这个位置 /** * Initialize product categories */ 在这里添加额外处理。 完整代码为:
//add by hicoogle ,date 2012-07-27 添加免运费产品数据据处理
$ifFreeShipping = $productData['is_freeshipping']==169 ? true : false; //此处的169就是属性的option值的ID
$freeShippingCategoryId = 211; //此处的211 就是分类的ID号,根据自己站的情况设置。
/**
* Initialize product categories
*/
$categoryIds = $this->getRequest()->getPost('category_ids');
if (null !== $categoryIds) {
if (empty($categoryIds)) {
$categoryIds = $ifFreeShipping ? $freeShippingCategoryId : array();
}else{
$categoryIds = explode(',',$categoryIds);
$categoryIds = $this->freeShippingCategoryIdProcess($freeShippingCategoryId,$ifFreeShipping,$categoryIds);
}
$product->setCategoryIds($categoryIds);
}else{
$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);
}
}
//end by hicoogle,date 2012-07-27 添加免运费产品数据据处理
另外再增加一个方法freeShippingCategoryIdProcess
/*
* add by hehailin ,date 2012-07-27
* 返回免运费产品的目录
*/
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);
}
//end by hehailin ,date 2012-07-27
经过测试,达到预期效果,产品设置YES后自动添加到free shipping的目录,设置No之后再取消。 代码完成后,一定要记得在magento index索引一下,否则看不到效果的。 (责任编辑:最模板) |



中英双语全静态生成企业
人气:677
黑色衣服鞋子外贸商城|
人气:755
ecshop黑红色耳机电子外贸
人气:938
ecshop仿淘宝母婴商城模板
人气:533
zencart仿focalprice外贸英文
人气:4029
Fastest外贸多用途商城mag
人气:221