Magento使自定义属性'免运费'的产品自动添加到指定目录

Magento免运费

Magento产品添加了个自定义属性Free Shipping(免运费)。要实现当Free shipping属性的值为YES的时候,自动把产品指定到一个叫Free Shipping的目录。

P,前台还可以处理下,使有这属性值的产品添加标识条幅。如图,效果还是挺惹人喜的。

magento免运费属性的产品示例图

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了之后再取消。

Magento首页设置显示指定目录的产品

先查看Magento这种设置指定目录产品到首页
的方法的效果图:

Magento首页显示指定目录产品的效果图

1.在Page的home里添加:{{block type="catalog/product_list" category_id="28" products_count="9" template="catalog/product/home.phtml"}}

注:category_id是指定的目录ID,products_count是要显示的产品数。

2.home.phtml的内容为

<?php
    $_products=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_products->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<h2 class="subtitle"><?php echo $this->__('BMD Exclusives') ?></h2>
<?php $_columnCount =3;$limit=isset($this->products_count)?(int)$this->products_count:6; ?>
    <?php $i=0;foreach ($_products as $_product): ?>
    <?php if ($limit<=$i){ break;} ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135) ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
                <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php echo $this->getPriceHtml($_product, true, '-new') ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if ($_compareUrl = $this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
        </ul>
        <?php endif ?>
    <?php endforeach; ?>
<?php endif; ?>

跟以前Magento首页显示什么什么的一样。。代码备份文