PHP, Magento, Codigniter, Opencart and Wordpress Developer. Contact for school and college projects on PHP, Codiegniter, Wordpress, Mysql etc.
Thursday, 20 October 2016
How to call static block in CMS page in admin?
Call static block in CMS Page by following code:
{{block type="cms/block" block_id="your_block_id"}}
Wednesday, 19 October 2016
Display Magento Category on any page or custom module page
How you can get list of all categories of your Magento Store.
If you want to display all categories in homepage or any CMS page or any custom module page. There are different ways to get the category listing. Some method are below:-
Get all Categories:
The below code will fetch all categories (both active and inactive), which are added in your Magento Store.
$categories = Mage::getModel('catalog/category') ->getCollection() ->addAttributeToSelect('*');
Get all active categories
The below code will fetch all active categories, which are added in your Magento Store. Thus filtering the inactive categories.
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
Get active categories of any particular level
The below code will fetch all active categories of particular level. Here, I have selected level 2.
$categories = Mage::getModel('catalog/category')->getCollection()
->addIsActiveFilter()
->addAttributeToFilter('level','2')
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToSelect('is_active');
Now, You can display Name, URL, id etc. by following code:
foreach ($categories as $category)
{
$entity_id = $category->getId();
$name = $category->getName();
$url_key = $category->getUrlKey();
$url_path = $category->getUrl();
}
Monday, 17 October 2016
How to add breadcrumbs in Magento.
You have add bredcum by two ways:
1. Via code (Add in a controller):
$crumbs = Mage::app()->getLayout->getBlock('breadcrumbs');
$crumbs->addCrumb('home', array(
'label' => 'Home',
'title' => 'Go to Home Page',
'link' => Mage::getUrl('')
));
2. Via Layout XML:
<reference name="breadcrumbs">
<action method="addCrumb">
<crumbname>home</crumbname> <!-- Class Name Which apply on li -->
<crumbinfo>
<label>Home</label>
<title>Go to Home Page</title>
<link>/</link>
</crumbinfo>
</action>
<action method="addCrumb">
<crumbName>cb</crumbName> <!-- Class Name Which apply on li -->
<crumbInfo>
<label>CB</label>
<title>CB</title>
</crumbInfo>
</action>
</reference>
Thursday, 13 October 2016
How to import products with custom options by CSV file in Magento
If you want to import products with custom options by CSV file. So, you need some changes in "app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php" file.
But it is core file of Magento, so we can not change directly in this file. Because when you update Magento, then this file will be replace from new file and your customization will be removed.
So, you copy this file on same location in "app/code/local" folder.
Now, open ("app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php") file and add some following codes.
The below line numbers can change in different Magento versions. I used line numbers according to Magento ver. 1.9.2.4.
At about line 734 you will see:
foreach ($importData as $field => $value) {
Just above add following code:
$custom_options = array();
At about line 742 you will see:
$attribute = $this->getAttribute($field);
if (!$attribute) {
continue;
}
Now need, add some code above the continue statement.
/* CUSTOM OPTION CODE */
if(strpos($field,':')!==FALSE && strlen($value))
{
$values=explode('|',$value);
if(count($values)>0)
{
@list($title,$type,$is_required,$sort_order) = explode(':',$field);
$title = ucfirst(str_replace('_',' ',$title));
$custom_options[] = array(
'is_delete'=>0,
'title'=>$title,
'previous_group'=>'',
'previous_type'=>'',
'type'=>$type,
'is_require'=>$is_required,
'sort_order'=>$sort_order,
'values'=>array()
);
foreach($values as $v)
{
$parts = explode(':',$v);
$title = $parts[0];
if(count($parts)>1) {
$price = $parts[1];
} else {
$price =0;
}
if(count($parts)>2) {
$price_type = $parts[2];
} else {
$price_type = 'fixed';
}
if(count($parts)>3) {
$sku = $parts[3];
} else {
$sku='';
}
if(count($parts)>4) {
$sort_order = $parts[4];
} else {
$sort_order = 0;
}
switch($type)
{
case 'file':
$custom_options[count($custom_options) - 1]['sku'] = $your_custom_sku;
$custom_options[count($custom_options) - 1]['file_extension'] = $your_custom_file_extension;
$custom_options[count($custom_options) - 1]['image_size_x'] = $your_custom_X_size;
$custom_options[count($custom_options) - 1]['image_size_y'] = $your_custom_Y_size;
$custom_options[count($custom_options) - 1]['price'] = $your_custom_price;
$custom_options[count($custom_options) - 1]['price_type'] = $your_custom_price_type;
break;
case 'field':
case 'area':
$custom_options[count($custom_options) - 1]['max_characters'] = $sort_order;
/* NO BREAK */
case 'date':
case 'date_time':
case 'time':
$custom_options[count($custom_options) - 1]['price_type'] = $price_type;
$custom_options[count($custom_options) - 1]['price'] = $price;
$custom_options[count($custom_options) - 1]['sku'] = $sku;
break;
case 'drop_down':
case 'radio':
case 'checkbox':
case 'multiple':
default:
$custom_options[count($custom_options) - 1]['values'][]=array(
'is_delete'=>0,
'title'=>$title,
'option_type_id'=>-1,
'price_type'=>$price_type,
'price'=>$price,
'sku'=>$sku,
'sort_order'=>$sort_order,
);
break;
}
}
}
}
/* END CUSTOM OPTION CODE */
Now, you'll see $product->save(); . Add following code just after.
/* Remove existing custom options attached to the product */
foreach ($product->getOptions() as $o)
{
$o->getValueInstance()->deleteValue($o->getId());
$o->deletePrices($o->getId());
$o->deleteTitles($o->getId());
$o->delete();
}
/* Add the custom options specified in the CSV import file */
if(count($custom_options))
{
foreach($custom_options as $option) {
try {
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
catch (Exception $e) {}
}
}
That's it. Now, you add all product custom options in CSV file.
For, import a custom option, you have need to add a new column to your CSV import file. The name of the column determines the name and type of the option. The format is: Name:Type:Is Required.
For example, You want to add drop down option called "Size". So your column header should be Size:drop_down:1 (1 for required, 0 for optional). Here is a list of the Types, which is used for "Custom Options" in the Magento admin area.
* field: Field
* area: Area
* file: File
* drop_down: Drop-down
* radio: Radio Buttons
* checkbox: Checkbox
* multiple: Multiple Select
* date: Date
* date_time: Date & Time
* time: Time
If you want import multiple values for one type (drop_down, radio, checkbox, multiple), so you can specify using a | separator. For Example, you are using Small, Medium, Large Size, you would use "Small|Medium|Large" as the value for the "Size:drop_down:1" column in your csv file.
Here's example of the import product with custom option format:
sku,name,description,price,Size:drop_down:1
T-Shirt1,T-Shirt,A T-Shirt,5.00,Small|Medium|Large
T-Shirt2,T-Shirt2,B T-Shirt,6.00,XS|S|M|L|XL
Now, if you want to an additional price and SKU for each option value. So, the complete syntax with all option values is:
title:price:price_type[fixed or percent]:sku:sort_order
Small|Medium:5:fixed::1|Large:10:percent:L_10:0
Here's the first example with additional price/sku modifiers.
sku,name,description,price,Size:drop_down:1
T-Shirt1,T-Shirt1,A T-Shirt,5.00,Small:0:fixed:-SM:0|Medium:2:percent:-MED:1|Large:3:percent:-LRG:2
But it is core file of Magento, so we can not change directly in this file. Because when you update Magento, then this file will be replace from new file and your customization will be removed.
So, you copy this file on same location in "app/code/local" folder.
app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
to
app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php
Now, open ("app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php") file and add some following codes.
The below line numbers can change in different Magento versions. I used line numbers according to Magento ver. 1.9.2.4.
At about line 734 you will see:
foreach ($importData as $field => $value) {
Just above add following code:
$custom_options = array();
At about line 742 you will see:
$attribute = $this->getAttribute($field);
if (!$attribute) {
continue;
}
Now need, add some code above the continue statement.
/* CUSTOM OPTION CODE */
if(strpos($field,':')!==FALSE && strlen($value))
{
$values=explode('|',$value);
if(count($values)>0)
{
@list($title,$type,$is_required,$sort_order) = explode(':',$field);
$title = ucfirst(str_replace('_',' ',$title));
$custom_options[] = array(
'is_delete'=>0,
'title'=>$title,
'previous_group'=>'',
'previous_type'=>'',
'type'=>$type,
'is_require'=>$is_required,
'sort_order'=>$sort_order,
'values'=>array()
);
foreach($values as $v)
{
$parts = explode(':',$v);
$title = $parts[0];
if(count($parts)>1) {
$price = $parts[1];
} else {
$price =0;
}
if(count($parts)>2) {
$price_type = $parts[2];
} else {
$price_type = 'fixed';
}
if(count($parts)>3) {
$sku = $parts[3];
} else {
$sku='';
}
if(count($parts)>4) {
$sort_order = $parts[4];
} else {
$sort_order = 0;
}
switch($type)
{
case 'file':
$custom_options[count($custom_options) - 1]['sku'] = $your_custom_sku;
$custom_options[count($custom_options) - 1]['file_extension'] = $your_custom_file_extension;
$custom_options[count($custom_options) - 1]['image_size_x'] = $your_custom_X_size;
$custom_options[count($custom_options) - 1]['image_size_y'] = $your_custom_Y_size;
$custom_options[count($custom_options) - 1]['price'] = $your_custom_price;
$custom_options[count($custom_options) - 1]['price_type'] = $your_custom_price_type;
break;
case 'field':
case 'area':
$custom_options[count($custom_options) - 1]['max_characters'] = $sort_order;
/* NO BREAK */
case 'date':
case 'date_time':
case 'time':
$custom_options[count($custom_options) - 1]['price_type'] = $price_type;
$custom_options[count($custom_options) - 1]['price'] = $price;
$custom_options[count($custom_options) - 1]['sku'] = $sku;
break;
case 'drop_down':
case 'radio':
case 'checkbox':
case 'multiple':
default:
$custom_options[count($custom_options) - 1]['values'][]=array(
'is_delete'=>0,
'title'=>$title,
'option_type_id'=>-1,
'price_type'=>$price_type,
'price'=>$price,
'sku'=>$sku,
'sort_order'=>$sort_order,
);
break;
}
}
}
}
/* END CUSTOM OPTION CODE */
Now, you'll see $product->save(); . Add following code just after.
/* Remove existing custom options attached to the product */
foreach ($product->getOptions() as $o)
{
$o->getValueInstance()->deleteValue($o->getId());
$o->deletePrices($o->getId());
$o->deleteTitles($o->getId());
$o->delete();
}
/* Add the custom options specified in the CSV import file */
if(count($custom_options))
{
foreach($custom_options as $option) {
try {
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
catch (Exception $e) {}
}
}
That's it. Now, you add all product custom options in CSV file.
For, import a custom option, you have need to add a new column to your CSV import file. The name of the column determines the name and type of the option. The format is: Name:Type:Is Required.
For example, You want to add drop down option called "Size". So your column header should be Size:drop_down:1 (1 for required, 0 for optional). Here is a list of the Types, which is used for "Custom Options" in the Magento admin area.
* field: Field
* area: Area
* file: File
* drop_down: Drop-down
* radio: Radio Buttons
* checkbox: Checkbox
* multiple: Multiple Select
* date: Date
* date_time: Date & Time
* time: Time
If you want import multiple values for one type (drop_down, radio, checkbox, multiple), so you can specify using a | separator. For Example, you are using Small, Medium, Large Size, you would use "Small|Medium|Large" as the value for the "Size:drop_down:1" column in your csv file.
Here's example of the import product with custom option format:
sku,name,description,price,Size:drop_down:1
T-Shirt1,T-Shirt,A T-Shirt,5.00,Small|Medium|Large
T-Shirt2,T-Shirt2,B T-Shirt,6.00,XS|S|M|L|XL
Now, if you want to an additional price and SKU for each option value. So, the complete syntax with all option values is:
title:price:price_type[fixed or percent]:sku:sort_order
Small|Medium:5:fixed::1|Large:10:percent:L_10:0
Here's the first example with additional price/sku modifiers.
sku,name,description,price,Size:drop_down:1
T-Shirt1,T-Shirt1,A T-Shirt,5.00,Small:0:fixed:-SM:0|Medium:2:percent:-MED:1|Large:3:percent:-LRG:2
Subscribe to:
Posts (Atom)