Aug 222012
 
error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once( 'app/Mage.php');
echo '<pre>';
$app = Mage::app();

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
 
	$readresult = $write->query("SELECT * FROM table limit 1 ");
	$row = $readresult->fetch();
	$SKU = $row['SKU']; 
	if($SKU=='')
	{
		echo 'no Record';
		exit;
	}
	
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);

echo "Product SKU : ".$SKU."<br>";

if ($product) 
{
	if ($product->getId()) 
	{
		/*$product->setCategoryIds(array(393));
			$product->save();
			exit;*/
		echo "Product ID : ".$product->getId()."<br>";
		echo "Product Name : ".$product->getName()."<br>";
		$categoryIds = $product->getCategoryIds();
		if (is_array($categoryIds) and count($categoryIds) > 0) 
		{
			$theMaster = array();
			print_r($categoryIds);
			$theMaster = array_merge($theMaster,$categoryIds);
			foreach($categoryIds as $curProductCatID  )
			{
				
				echo "Product Category ID : ".$curProductCatID."<br>";
				$theCategory = Mage::getModel('catalog/category')->load($curProductCatID);
			
				if($theCategory->getParentId())
				{
					$cat_array= array($curProductCatID);
					$cat_array = getParentTopCategory($theCategory,$cat_array);
					$final_array= array_reverse($cat_array);
					print_r($final_array);
					$theMaster = array_merge($theMaster,$final_array);
					
				}
				else
				{
					echo 'Parent category  not found<br>';
				}
			}
			echo "master array :";
			print_r($theMaster);
			echo '<br>';
			$tmpA = array();
			
			$theMaster = array_unique ($theMaster);
			foreach($theMaster as $item)
			{
				$tmpA[] = $item;
			}
			echo "master unique array :";
			print_r( $tmpA);
			echo '<br>';
			$product->setCategoryIds($tmpA);
			$product->save();
		}
	}
	else
	{
		echo 'Product not found';
	}
} 

function  getParentTopCategory($category,$cat_array)
{
	echo $category->getName()."<br>";
	if($category->getLevel() == 2)
	{
		$cat_array[]=$category->getParentId();
		return $cat_array;
	}
	else
	{
		
		$parentCategory = Mage::getModel('catalog/category')->load($category->getParentId());
		if($category->getParentId())
		{
			$cat_array[]=$category->getParentId();
			return getParentTopCategory($parentCategory,$cat_array);
		}
		else
		{
			return $cat_array;
		}
	}
}
Aug 212012
 
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Load Up Magento Core
define('MAGENTO', realpath('/'));

require_once(MAGENTO . 'app/Mage.php');

$app = Mage::app();

$categories = Mage::getModel('catalog/category')
 ->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('is_anchor', 0)
 ->addAttributeToFilter('entity_id', array("gt" => 1))
 ->setOrder('entity_id')
 ;

foreach($categories as $category) {
 echo $category->getId() . "\t" . $category->getName() . "\n";
 $category->setIsAnchor(1);
 $category->save();
}
Jul 202012
 
require_once 'app/Mage.php';
Mage::app('default'); // Default or your store view name.

//get a new category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().

//if update
if ($id) {
  $category->load($id);
}

$general['name'] = "My Category";
$general['path'] = "1/2/23"; // catalog path
$general['description'] = "Great My Category";
$general['meta_title'] = "My Category"; //Page title
$general['meta_keywords'] = "My , Category";
$general['meta_description'] = "Some description to be found by meta search robots. 2";
$general['landing_page'] = ""; //has to be created in advance, here comes id
$general['display_mode'] = "PRODUCTS_AND_PAGE"; //static block and the products are shown on the page
$general['is_active'] = 1;
$general['is_anchor'] = 0;
$general['page_layout'] = 'two_columns_left';

//$general['url_key'] = "cars";//url to be used for this category's page by magento.
//$general['image'] = "cars.jpg";


$category->addData($general);

try {
    $category->save();
    echo "Success! Id: ".$category->getId();
}
catch (Exception $e){
    echo $e->getMessage();
}
Jun 292012
 
$write = Mage::getSingleton('core/resource')->getConnection('core_write');

$readresult = $write->query("insert into tablename values ('a','b','c')");
// or
$readresult = $write->query("select * from tablename ");

while ($row = $readresult->fetch() ) 
{
	$categoryIds[]=$row['id'];
}

Jun 222012
 

Below code add product with image and custom field data.

<?php
	error_reporting(E_ALL | E_STRICT);
	$mageFilename = 'app/Mage.php';
	require_once $mageFilename;
	$app = Mage::app('default'); 
	
	ini_set('display_errors', 1);


	$api = new Mage_Catalog_Model_Product_Api();
	
	$attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
	$attribute_sets = $attribute_api->items();
	
	$productData = array(); 
	$productData['website_ids'] = array(1); 
	$productData['categories'] = array(23);

	$productData['status'] = 1;
	
	$productData['name'] = utf8_encode('nimit');
	$productData['description'] = utf8_encode('nimit desc');
	$productData['short_description'] = utf8_encode('nimit short desc');
	
	$productData['price'] = 12.34;
	$productData['weight'] = 23.25;
	$productData['tax_class_id'] =2;
	$productData['page_layout'] ='two_columns_left';
		
	$new_product_id = $api->create('simple',$attribute_sets[0]['set_id'],'ND3',$productData);
	
	print_r($new_product_id);
	
	$stockItem = Mage::getModel('cataloginventory/stock_item');
	$stockItem->loadByProduct( $new_product_id );
	
	$stockItem->setData('use_config_manage_stock', 1);
	$stockItem->setData('qty', 100);
	$stockItem->setData('min_qty', 0);
	$stockItem->setData('use_config_min_qty', 1);
	$stockItem->setData('min_sale_qty', 0);
	$stockItem->setData('use_config_max_sale_qty', 1);
	$stockItem->setData('max_sale_qty', 0);
	$stockItem->setData('use_config_max_sale_qty', 1);
	$stockItem->setData('is_qty_decimal', 0);
	$stockItem->setData('backorders', 0);
	$stockItem->setData('notify_stock_qty', 0);
	$stockItem->setData('is_in_stock', 1);
	$stockItem->setData('tax_class_id', 0);
	
	$stockItem->save();
	
	$product = Mage::getModel('catalog/product')->load($new_product_id);
	
	$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
	$product->addImageToMediaGallery ('E:/High Res Images/High Res Images/GC00012.jpg', array ('image','small_image','thumbnail'), false, false); 
	$product->addImageToMediaGallery ('E:/High Res Images/High Res Images/GC00014_1.jpg', array ('image','small_image','thumbnail'), false, false); 
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

	$product->save();
	
	
	$product = Mage::getModel('catalog/product')->load($new_product_id);
	$optionData = 	array(
						"title" => "Custom Text Field Option Title 1",
						"type" => "field",
						"is_require" => 1,
						"sort_order" => 1,
						"price" => 0,
							"price_type" => "fixed",
							"sku" => "",
							"max_characters" => 15
					);
	
	$product->setHasOptions(1);
	$option = Mage::getModel('catalog/product_option')
			  ->setProductId($new_product_id)
			  ->setStoreId(1)
			  ->addData($optionData);
	$option->save();
	$product->addOption($option);
	
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

	$product->save();
	
	$product = Mage::getModel('catalog/product')->load($new_product_id);
	$optionData = 	array(
						"title" => "Custom Text Field Option Title 2",
						"type" => "field",
						"is_require" => 1,
						"sort_order" => 2,
						"price" => 0,
							"price_type" => "fixed",
							"sku" => "",
							"max_characters" => 25
					);
	
	$product->setHasOptions(1);
	$option = Mage::getModel('catalog/product_option')
			  ->setProductId($new_product_id)
			  ->setStoreId(1)
			  ->addData($optionData);
	$option->save();
	$product->addOption($option);
	
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

	$product->save();
?>
Feb 222012
 

To set a Magento session variable:

$myValue = 'Hello World';
Mage::getSingleton('core/session')->setMyValue($myValue);

To Retrieve:

$myValue = '';
$myValue=Mage::getSingleton('core/session')->getMyValue();

To Unset:

Mage::getSingleton('core/session')->unsMyValue();

Note that ‘MyValue’ can be any text you want but ‘set’, ‘get’ and ‘uns’ prefixes are required.