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();
?>
Jun 222012
 

Look at the line 26 to 31.

I have add validation if some condition is fulfill.

var validator = $("#deal").validate({
    rules: {
                    dealName: "required",
                    dealCategoryId: "required",
                    dealPhone: {
                                    required: true,
                                    digits:true
                    },
                    dealVenueName: "required",
                    dealVenueAddress: "required",
                    dealVenueStateId: "required",
                    dealVenueZip: {
                                    required: true,
                                    minlength: 5,
                                    digits:true
                    },
                    dealVenueContactEmail: {
                                    required: true,
                                    email: true
                    },                                             
                    dealVenueWebSite: {
                                    url: true
                    },
                    dealStartDate: "required",
                    dealEndDate: "required",
                    dealPrice: {required: function(element){
                      return document.getElementById('dealIsFree').checked != true}
                    },
                    dealTicketOnSaleDate: {required: function(element){
                      return document.getElementById('dealIsFree').checked != true}
                    },
    },
    messages: {
                    dealName: "Please enter a deal name",
                    dealCategoryId: "Please select a category",
                    dealPhone: {
                                    required: "Please enter phone no",
                                    digits:  jQuery.format("Please enter only digits")
                    },
                    dealVenueName: "Please enter venue name",
                    dealVenueAddress: "Please enter venue address",
                    dealVenueStateId: "Please select a state",
                    dealVenueZip: {
                                    required:  "Please enter a zipcode",
                                    minlength:  jQuery.format("Please enter at least {0} characters"),
                                    digits:  jQuery.format("Please enter only digits")
                    },
                    dealVenueContactEmail: {
                                    required:  "Please enter a contact email address",
                                    email: "Please enter a valid email address"
                    },
                    dealVenueWebSite: {
                                    url: "Please enter a valid website URL"
                    },
                    dealStartDate: "Please select a start date",
                    dealEndDate: "Please select a end date",
                    dealPrice: "Please enter price",
                    dealTicketOnSaleDate: "Please select a date"
    },
    
    // set this class to error-labels to indicate valid fields
    success: function(label) {
                    // set &nbsp; as text for IE
                    label.html("&nbsp;").addClass("checked");
    }
});
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.

Feb 172012
 

// PHP Function

<?php
backup_tables('Host','User','Pass','DB');

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
	
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);
	
	//get all of the tables
	if($tables == '*')
	{
		$tables = array();
		$result = mysql_query('SHOW TABLES');
		while($row = mysql_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	
	//cycle through
	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		
		$return.= 'DROP TABLE IF EXISTS '.$table.';';
		$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysql_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j<$num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}
		$return.="\n\n\n";
	}
	
	//save file
	$handle = fopen('file-name.sql','w+');
	fwrite($handle,$return);
	fclose($handle);
}
?>
Mar 182010
 

Please add following code in .htaccess file

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /
# Rule 1
RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.com$ [NC]
RewriteRule ^(.*)$ http://www.MYWEBSITE.com/$1 [L,R=301]

# Rule 2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.MYWEBSITE.com/ [R=301,L]

# Rule 3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]