Search This Blog

Friday, April 22, 2011

Magento PHP Interview Questions And Answers

1) What are the different types of Errors in PHP?
Answer:
There are three basic types of runtime errors in PHP:
1. Notices: These are small, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although the default behavior can be changed.
2. Warnings: Warnings are more severe errors like attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.




2) Why Magento use EAV database model ?

Answer:

In EAV database model, data are stored in different smaller tables rather than storing in a single table.

product name is stored in catalog_product_entity_varchar table
product id is stored in catalog_product_entity_int table
product price is stored in catalog_product_entity_decimal table

Magento Use EAV database model for easy upgrade and development as this model gives more flexibility to play with data and attributes.


3) What is the difference between Mage::getSingletone() and Mage::getModel() in Magento
Answer:

Mage::getSingletone() always finds for an existing object if not then create that a new object but Mage::getModel() always creates a new object.

Get Product ID and Product Name in Magento

load($productid); //getting product object for particular product id

echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url

?>
------OR------------
getCollection(); //products collection
foreach ($collection as $product) //loop for getting products
{

$model->load($product->getId());
$pname = $model->getName();
if(strcmp($pname,$product_name)==0)
{
$id = $product->getId();
}
}
echo 'Required ID->'.$id; //id of product
?>

Monday, April 11, 2011

Magento Collection Functions getSelect,getSize,getSelectSql,setOrder,distinct, getData,resetData,printLogQuery,getCurPage,getLastPageNumber,getPageSize,getSize,getFirstItem,getLastItem,getItems,clear


Here are some of the functions that you can use in your collection object:-


 Get Zend_Db_Select instance

$collection->getSelect();

 Add select order

$collection->getSelect()->setOrder($field, $direction);

 Get collection size : $collection->getSelect()->getSize(); : $collection->count()

 Retrieve collection all items count : $collection->getSelect()->getSize();

 Get sql select string or object

$collection->getSelect()->getSelectSql();


Field Filter to collection
 If $attribute is an array will add OR condition with following format:
 array(
 array('attribute'=>'email', 'like'=>'ramu@%'),
 array('attribute'=>'phone', 'like'=>'9759%'),
 )

$collection->getSelect()->setOrder($field, $condition);

 Get all data array for collection: $collection->getSelect()->getData();

 Set select distinct : $collection->getSelect()->distinct($flag);

 Print and/or log query : $collection->getSelect()->printLogQuery(true, true);

Reset loaded for collection data array : $collection->getSelect()->resetData();




Get current collection page : $collection->getSelect()->getCurPage();


 Retrieve collection last page number : $collection->getSelect()->getLastPageNumber();


 Retrieve collection page size :$collection->getSelect()->getPageSize();

 Clear collection : $collection->getSelect()->clear();

 Retrieve collection first item : $collection->getSelect()->getFirstItem();


 Retrieve collection last item : $collection->getSelect()->getLastItem();


 Retrieve collection items : $collection->getSelect()->getItems();



Monday, April 4, 2011

get skin url, get js url, get media url, get store url ,get base url in magneto

These are the following methods to get Magento Base Url, Magento Skin Url, Magento Media Url, Magento Js Url.to get all write the following code

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK); or you can write $this->getUrl();
e.g:- http://yoursite.com/index.php/

Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
e.g:- http://yoursite.com/media/
Get Magento Skin Url

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN); or you can write $this->getSkinUrl();
e.g:- http://yoursite.com/skin/

Get Magento Store Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
e.g:- http://yoursite.com/

Get Magento Js Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
http://www.yoursite.com/js


Source http://xhtmlandcsshelp.blogspot.com/2010/11/magento-get-url-for-skin-media-js-store.html

Tuesday, March 22, 2011

Magento integrate editor Wysiwyg or magento what you see is what you get in form

In you Form.php integrate the code to get editor Wysiwyg

$fieldset->addField('field_name', 'editor', array(
'name' => 'field_name',
'label' => Mage::helper('Module_Helper_Name')->__('field_name'),
'title' => Mage::helper('Module_Helper_Name')->__('field_name'),
'style' => 'width:700px; height:500px;',
'wysiwyg' => true,
'config' => Mage::getSingleton('Module/wysiwyg_config')->getConfig(),

));

Now dont forget to add the WYSIWYG foder from the cms/model to your model folder

Sunday, March 20, 2011

How to create Configurable Products in Magento




Magento offers two options to create configurable products (products with options such as Size or Color):
  • Simple products with custom options (Recommended if you don’t plan to track inventory.)
  • Configurable products and associate simple products to it. (Recommended if you plan to track inventory.)
To create simple products log in your admin control panel select Catalogue => Manage Products => Create new product.
1. Simple products with custom options:
a) Create Simple Product
Magento
b) Fill out all required fields such as: General, Price, Meta Information, Description, Inventory, Websites and Categories.

Magento
c) Create Custom Option
Magento
d) Add a new option and add your own custom option. Our example is for Size.
Magento
e) Save the product and that is all. Your product must show your custom option on the front end.
Magento
2) Create a configurable product and associate simple products to it.
a) First you must create the attributes (make sure it can be used for configurable product)
To create an attribute log in your admin control panel select Catalogue => Manage Attributes => Create new attribute.
Magento
b) Our attribute code in this example is for Jacket Color.
Magento
c) Add label options and save the attribute.
Magento
d) Manage attribute set to default
To manage the attribute set log in your admin control panel select Catalogue => Manage Attributes => Manage Attribute Set.
Magento
e) Move the attribute created (in our case jacket color) to default attributes. You can easily move it, just drag and drop it, exactly where you want to display it.
Magento
f) Moved under default fields and save the attribute set.
Magento
g) Create the simple products and fill in the attribute. It is not required to add images, description. Set visibility to nowhere & save the product.
To create simple products log in your admin control panel select Catalogue => Manage Products => Create new product.
Magento
In our case we have our Jacket Color attribute in 4 colors: Red, Green, White, and Blue. We now have to create a simple product for each color. In total there will be 4 simple products. We can duplicate the products to make the process faster. (Just use a different Product name, Sku & Jacket Color.)
h) Once all simple products are done, we can create 1 configurable product for it.
Magento
i) In this example Configurable Product uses Jacket Color and therefore the box (for Jacket Color) is checked.
Magento
j) Create the configurable product and fill in all the dates required such as general, description, images, websites, and categories.
Magento
k) Under associated products category, the simple products created must be associated with the 4 simple products in our case & save the configurable product.
Magento
l) On the front end the drop down menu from Jacket Color will now be displayed.
Magento
Source 
http://webshop-conversion.com/2009/07/24/how-to-create-configurable-products-in-magento/

Tuesday, March 15, 2011