[Magento代码] 关联SKU到指定的分类下面

来源:互联网 发布:云计算部署模式哪三种 编辑:程序博客网 时间:2024/05/22 15:24
<?phperror_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);

0 0
原创粉丝点击