Skip to content

Instantly share code, notes, and snippets.

@willboudle
Created January 13, 2014 01:16
Show Gist options
  • Select an option

  • Save willboudle/8393139 to your computer and use it in GitHub Desktop.

Select an option

Save willboudle/8393139 to your computer and use it in GitHub Desktop.
Add Products to category by sku programmatically
<?php
// USAGE http://www.site.com/updategroupprice.php?price=14.95&sku=SKU123X
require_once '../app/Mage.php';
umask(0);
//Mage::app('admin');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$sku = $_GET['sku'];
$id = $_GET['id'];
$categoryId = $_GET['catid'];
// get my products
$productcollection = Mage::getResourceModel('catalog/product_collection');
$productcollection->addAttributeToFilter('sku', array('like' => $sku.'%'));
$productcollection->addAttributeToFilter("visibility", array("eq" => 4));
//Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($productcollection);
foreach ($productcollection as $product){
$product->setCategoryIds(array($categoryId));
$product->save();
echo $product->getSku()." has been added to Category ID: ". $categoryId ."<br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment