Search This Blog

Wednesday, April 27, 2011

Magento get last product or recent product added to cart or shopping cart

Magento get last product or recent product added to cart or shopping cart


Mage::getSingleton('checkout/session')->getLastAddedProductId(true);

Tuesday, April 26, 2011

Add magento to wishlist code

a href="'.Mage::helper("wishlist")->getAddUrl($_product).'" class="link-cart">Add to Wishlist /a

or

if ($this->helper('wishlist')->isAllow()) :
a href="helper('wishlist')->getAddUrl($_product) ?>" class="link-cart">__('Add to Wishlist') ?> /a
endif;

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