magento按分类批量添加up-sell产品
2012-10-30 12:12
591 查看
require_once 'app/Mage.php'; set_time_limit(0); Mage::app('default'); //获取所有分类 function nodeToArray(Varien_Data_Tree_Node $node) { $result = array(); $result['category_id'] = $node->getId(); $result['parent_id'] = $node->getParentId(); $result['name'] = $node->getName(); $result['is_active'] = $node->getIsActive(); $result['position'] = $node->getPosition(); $result['level'] = $node->getLevel(); $result['children'] = array(); foreach ($node->getChildren() as $child) { $result['children'][] = nodeToArray($child); } return $result; } function load_tree() { $store = 1; $parentId = 2; $tree = Mage::getResourceSingleton('catalog/category_tree') ->load(); $root = $tree->getNodeById($parentId); if ($root && $root->getId() == 1) { $root->setName(Mage::helper('catalog')->__('Root')); } $collection = Mage::getModel('catalog/category')->getCollection() ->setStoreId($store) ->addAttributeToSelect('name') //->addAttributeToSelect('id') ->addAttributeToSelect('is_active'); $tree->addCollectionData($collection, true); return nodeToArray($root); } function getCatList($tree) { $cat_list = array(); foreach ($tree as $item) { $cat = array(); $cat['id'] = $item['category_id']; $cat['is_active'] = $item['is_active']; $product = array(); $category = Mage::getModel('catalog/category')->load($item['category_id']); $products = $category->getProductCollection(); foreach($products as $val) { $product[] = $val->getId(); } $cat['products'] = $product; $cat_list[$cat['id']] = $cat; getCatList($item['children']); } return $cat_list; } $tree = load_tree(); $cat_list = getCatList($tree['children']); //--------------------------------------获取所有分类-------------------------------------// $itemsLimit = 8; $collection = Mage::getResourceModel('catalog/product_collection')->load(); foreach($collection as $item) { $oldCount = count($item->getUpSellProducts()); if($oldCount < $itemsLimit) { //$itemsLimit = $itemsLimit - $oldCount; $cat_arr = array(); $product_id = $item->getId(); $categories = $item->getCategoryCollection(); foreach($categories as $_category) { if($cat_list[$_category['entity_id']]['is_active'] == 1) { $upsells = array_rand($cat_list[$_category['entity_id']]['products'], $itemsLimit+1); $i = $oldCount; $link = array(); foreach($upsells as $up) { $realtedid = $cat_list[$_category['entity_id']]['products'][$up]; if($realtedid != $product_id) { $link[$realtedid] = array('position' => null); } $i++; if($i >= $itemsLimit){break;} } print_r($link);echo $product_id . "<br />"; $item->setUpSellLinkData($link); Mage::dispatchEvent('catalog_product_prepare_save', array('product' => $item)); $item->save(); } } } }
相关文章推荐
- magento related/upsell product 及产品/分类调用
- magento related/upsell product 及产品/分类调用
- magento随机关联同分类下upsell产品
- [Magento代码]-导入excel添加分类,并关联产品到该分类下
- Magento分类中添加产品
- magento分类添加超过1000个产品
- magento产品详细页添加返回分类链接,下一件,上一件产品链接
- Magento 修改添加产品的默认属性值
- sell脚本应用:批量添加(删除)用户
- 解决magento添加产品在前台不显示问题
- Get Realted, Upsell, Cross sell Product collection in magento
- Magento快速添加产品的插件 - Quick Product Adder Client
- 在Magento产品分类页面创建推荐产品(Featured Products)
- magento添加多个产品到购物车(Add multiple products to cart )
- Magento 后台订单详情页Items Ordered列表处,添加跳转到编辑产品的url
- magento 批量添加表前缀测试
- Magento 把所有产品的 Special Price 同步添加给Group Price代码
- Bigcommerce产品分类:给重复的CategoryName链接添加rel="nofllow"标记
- discuz 批量添加主题分类 自动添加主题分类
- magento -- 如何在magento中进行产品的批量上传