<?php
error_reporting(E_ALL);
set_time_limit(0);
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
/**
* 添加产品所属分类
*
* @param $skustr 产品SKU拼接的字符串
* @param $$categoryId 分类ID
*/
function add_category_product($skustr,$categoryId){
$skuArray = explode('|',$skustr);
foreach($skuArray as $sku){
if(trim($sku)){
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',trim($sku));
if($_product){
$productId = $_product->getId();
//给该产品添加分类
//先查询是否已经存在,存在就不新增
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * from catalog_category_product WHERE category_id=$categoryId AND product_id=$productId";
$results = $read->fetchAll($sql);
if(!$results){
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "insert into catalog_category_product values($categoryId,$productId,1)";
$write->query($sql);
}
}
}
}
}
$skustr = "TL30079 | EP20870 | EP21581 | EP20868 | EP18965 |EP19235 | EP19236 | HB83558 | HB83559 | EP20587 | TL30081 | TL28825 | TL28827 |AT3638RE+ | AT4158+ | AT4284 | AT4085 | ET1126 | AT4079|HW49745NA | TL30122|TL28555 | TL28554 | TL27435 | AT4305 | AT4309 | TL31060 | TL31061 | TL31071 | TL31134 | TL25975-10 | TL11552|AT3957 | AT3956 | AT3715 | AT3372 | AT3373 | TL28534 | AP2032 | HW51122 | HW51121 | HW51120 | HW51119 | HW50080GR | EP20447 | EP19995 | EP20570-110V | EP20571-110V | EP19568-110V | EP20485 | EP21052 | EP20572-110V | TL28822 ";
$categoryId = 89;
add_category_product($skustr,$categoryId);[Magento代码] 关联SKU到指定的分类下面
最新推荐文章于 2024-01-26 13:16:37 发布
本文介绍了一种在Magento中批量为多个产品添加指定分类的方法。通过解析产品SKU字符串,加载对应的产品并检查其是否已存在于目标分类中,如果不存在,则将其添加到该分类下。
5万+

被折叠的 条评论
为什么被折叠?



