Saturday, December 1, 2012

Magento - Get all Categories and Subcategories

It is not such a typical task. Here is the code to get all Categories and Subcategories in Magento -


<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<div id="tabs-1">
    <ul id="tree1">
        <?php foreach($_categories as $_category): ?>
            <li>
                    <input type="checkbox" value="<?php echo $_category->getId() ?>"><label><?php echo $_category->getName() ?></label>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <input type="checkbox" value="<?php echo $_subcategory->getId() ?>"><label><?php echo $_subcategory->getName() ?></label>
<?php $_category = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
               <?php $_subsubcategories = $_subcategory->getChildrenCategories() ?>
               <?php if (count($_subsubcategories) > 0): ?>
                   <ul>
                       <?php foreach($_subsubcategories as $_subsubcategory): ?>
                           <li>
                               <input type="checkbox" value="<?php echo $_subsubcategory->getId() ?>"><label><?php echo $_subsubcategory->getName() ?></label>
                           </li>
                       <?php endforeach; ?>
                   </ul>
               <?php endif; ?>

                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>

Enjoy!! :-)

Friday, November 30, 2012

Get Latitude and Longitude using Address in php

Here is the code snippet by which you can find the Latitude and Longitude using the Customer Address -


public function getLatLongArray($_Country, $_State)
{
$address = $_Country."+".$_State;

$_CountryName = Mage::app()->getLocale()->getCountryTranslation($_Country);

$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$_CountryName";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;

return array(
'latitude' => $lat,
'longitude' => $long,
);
}

Enjoy!! :-)

Saturday, October 20, 2012

Magento : Get URLs

Here is the code to get base url, media url, store url, javascript url, images path etc in Magento.

To retrieve URL path in Static Block -

To get Skin URL
{{skin url='images/anx.jpg '}}
To get Media URL
{{media url='/anx.jpg'}}
To get Store URL
{{store url='anx.html'}}
To get Base URL
{{base url='storename/anx.html'}}

To Retrieve URL path in PHTML -
Note: In editing PHTML don't forget to enclode the following code with PHP tag

Not secure Skin URL:
<?php echo getSkinUrl('images/anx.jpg') ?> 
Secure Skin URL
<?php echo getSkinUrl('images/ anx.gif', array('_secure'=>true)) ?>
Get Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();
Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();
Get Magento Media Url
 Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
Get Magento Media Url
 Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
Get Magento Skin Url
 Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
Get Magento Store Url
 Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
Get Magento Js Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

Thursday, September 27, 2012

Bestselling Products in Magento

To add a block into Home Page you need to  login into the Magento : CMS > Manage Pages > Home. Then add the following to the Content area:

{{block type=”catalog/product” show_total=”12″ template=”catalog/product/bestseller.phtml”}}

Now put the following code into catalog/product/bestseller.phtml -

   $visibility = array(
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
  );

  $_productCollection = Mage::getResourceModel('reports/product_collection')
                      ->addAttributeToSelect('*')
                      ->addOrderedQty()
                      ->addAttributeToFilter('visibility', $visibility)
                      ->setOrder('ordered_qty', 'desc');
   $totalPerPage = ($this->show_total) ? $this->show_total : 6 ;
   $counter = 1;
Here you have the Best selling Product Collection. Now feel free to manage your Bestseller page with your own Style.

Friday, August 17, 2012

Magento - Get all Cart Product's Categories

Here is the following Code to Get all Product Categories related with Cart :
Use Checkout Session :
$session = Mage::getSingleton('checkout/session');
foreach($session->getQuote()->getAllItems() as $item):
     $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item->getSku());
     $cartProductCatId = $_product->getCategoryIds();
endforeach;

Wednesday, May 23, 2012

List of Observers in Magento

Magento Uses a fantastic Event hooking System that enables us to add additional functionality as plug-in without modifying our Code. Here is the List of Observers :



Magento
Module
Event Name
Mage
Admin
admin_session_user_login_success
Mage
Admin
admin_session_user_login_failed
Mage
Admin
admin_user_authenticate_after
Mage
Adminhtml
adminhtml_block_html_before
Mage
Adminhtml
adminhtml_catalog_category_tabs
Mage
Adminhtml
adminhtml_catalog_category_tree_is_moveable
Mage
Adminhtml
adminhtml_catalog_category_tree_can_add_root_category
Mage
Adminhtml
adminhtml_catalog_category_tree_can_add_sub_category
Mage
Adminhtml
adminhtml_product_attribute_types
Mage
Adminhtml
adminhtml_catalog_product_edit_prepare_form
Mage
Adminhtml
adminhtml_catalog_product_edit_element_types
Mage
Adminhtml
adminhtml_catalog_product_attribute_set_main_html_before
Mage
Adminhtml
adminhtml_catalog_product_attribute_set_toolbar_main_html_before
Mage
Adminhtml
adminhtml_catalog_product_form_prepare_excluded_field_list
Mage
Adminhtml
adminhtml_catalog_product_edit_prepare_form
Mage
Adminhtml
adminhtml_catalog_product_edit_element_types
Mage
Adminhtml
adminhtml_catalog_product_edit_tab_attributes_create_html_before
Mage
Adminhtml
adminhtml_cms_page_edit_tab_content_prepare_form
Mage
Adminhtml
adminhtml_cms_page_edit_tab_design_prepare_form
Mage
Adminhtml
adminhtml_cms_page_edit_tab_main_prepare_form
Mage
Adminhtml
adminhtml_cms_page_edit_tab_meta_prepare_form
Mage
Adminhtml
adminhtml_widget_grid_filter_collection
Mage
Adminhtml
adminhtml_widget_container_html_before
Mage
Adminhtml
adminhtml_controller_action_predispatch_start
Mage
Adminhtml
adminhtml_customer_prepare_save
Mage
Adminhtml
adminhtml_customer_save_after
Mage
Adminhtml
on_view_report
Mage
Adminhtml
catalog_category_prepare_save
Mage
Adminhtml
catalog_controller_category_delete
Mage
Adminhtml
catalog_product_new_action
Mage
Adminhtml
catalog_product_edit_action
Mage
Adminhtml
catalog_product_prepare_save
Mage
Adminhtml
catalog_controller_product_delete
Mage
Adminhtml
catalog_product_to_website_change
Mage
Adminhtml
cms_page_prepare_save
Mage
Adminhtml
adminhtml_cmspage_on_delete
Mage
Adminhtml
adminhtml_cmspage_on_delete
Mage
Adminhtml
admin_permissions_role_prepare_save
Mage
Adminhtml
adminhtml_controller_catalogrule_prepare_save
Mage
Adminhtml
adminhtml_controller_salesrule_prepare_save
Mage
Adminhtml
adminhtml_sales_order_create_process_data
Mage
Adminhtml
adminhtml_sales_order_creditmemo_register_before
Mage
Adminhtml
admin_system_config_changed_section_{$section}
Mage
Adminhtml
store_group_save
Mage
Adminhtml
store_edit
Mage
Adminhtml
store_add
Mage
Adminhtml
store_delete
Mage
Adminhtml
sales_convert_order_to_quote
Mage
Adminhtml
sales_convert_order_item_to_quote_item
Mage
Api
api_user_authenticated
Mage
Bundle
bundle_product_view_config
Mage
Bundle
prepare_catalog_product_index_select
Mage
Bundle
prepare_catalog_product_price_index_table
Mage
Bundle
catalog_product_prepare_index_select
Mage
Bundle
catalog_product_get_final_price
Mage
Catalog
catalog_block_product_list_collection
Mage
Catalog
catalog_product_view_config
Mage
Catalog
catalog_product_upsell
Mage
Catalog
catalog_controller_category_init_before
Mage
Catalog
catalog_controller_category_init_after
Mage
Catalog
catalog_controller_product_init_before
Mage
Catalog
catalog_controller_product_init
Mage
Catalog
catalog_controller_product_init_after
Mage
Catalog
catalog_controller_product_view
Mage
Catalog
catalog_product_compare_add_product
Mage
Catalog
catalog_product_compare_remove_product
Mage
Catalog
catalog_helper_output_construct
Mage
Catalog
catalog_category_tree_move_before
Mage
Catalog
$this→_eventPrefix.’_move_before’
Mage
Catalog
$this→_eventPrefix.’_move_after’
Mage
Catalog
catalog_category_tree_move_after
Mage
Catalog
category_move
Mage
Catalog
$this→_eventPrefix.’_validate_before’
Mage
Catalog
$this→_eventPrefix.’_validate_after’
Mage
Catalog
catalog_model_product_duplicate
Mage
Catalog
catalog_product_is_salable_before
Mage
Catalog
catalog_product_is_salable_after
Mage
Catalog
$this→_eventPrefix.’_delete_after_done’
Mage
Catalog
catalog_product_import_after
Mage
Catalog
catalog_product_website_update_before
Mage
Catalog
catalog_product_website_update
Mage
Catalog
catalog_product_status_update
Mage
Catalog
catalog_product_get_final_price
Mage
Catalog
catalog_category_change_products
Mage
Catalog
$this→_eventPrefix . ‘_load_before’
Mage
Catalog
$this→_eventPrefix . ‘_load_after’
Mage
Catalog
$this→_eventPrefix . ‘_add_is_active_filter’
Mage
Catalog
catalog_category_tree_init_inactive_category_ids
Mage
Catalog
catalog_category_tree_init_inactive_category_ids
Mage
Catalog
catalog_category_tree_move_before
Mage
Catalog
catalog_category_tree_move_after
Mage
Catalog
$this→_eventPrefix . ‘_load_before’
Mage
Catalog
$this→_eventPrefix . ‘_load_after’
Mage
Catalog
$this→_eventPrefix . ‘_add_is_active_filter’
Mage
Catalog
catalogindex_prepare_price_select
Mage
Catalog
catalog_prepare_price_select
Mage
Catalog
catalog_product_collection_load_before
Mage
Catalog
catalog_product_collection_load_after
Mage
Catalog
catalog_product_collection_before_add_count_to_categories
Mage
Catalog
catalog_product_collection_apply_limitations_after
Mage
Catalog
catalog_product_compare_item_collection_clear’
Mage
Catalog
catalog_product_flat_prepare_columns
Mage
Catalog
catalog_product_flat_prepare_indexes
Mage
Catalog
catalog_product_flat_rebuild
Mage
Catalog
catalog_product_flat_update_product
Mage
Catalog
prepare_catalog_product_index_select
Mage
Catalog
prepare_catalog_product_index_select
Mage
Catalog
prepare_catalog_product_index_select
Mage
Catalog
prepare_catalog_product_index_select
Mage
Catalog
prepare_catalog_product_price_index_table
Mage
Catalog
prepare_catalog_product_price_index_select
Mage
CatalogIndex
catalogindex_plain_reindex_after
Mage
CatalogIndex
catalogindex_prepare_price_select
Mage
CatalogIndex
catalogindex_get_minimal_price
Mage
CatalogIndex
catalogindex_prepare_price_select
Mage
CatalogIndex
catalogindex_prepare_price_select
Mage
CatalogIndex
catalogindex_prepare_price_select
Mage
CatalogIndex
catalogindex_prepare_price_select
Mage
CatalogRule
catalogrule_before_apply
Mage
CatalogRule
catalogrule_after_apply
Mage
CatalogSearch
catalogsearch_reset_search_result’
Mage
Checkout
checkout_cart_add_product_complete
Mage
Checkout
checkout_controller_multishipping_shipping_post
Mage
Checkout
checkout_multishipping_controller_success_action’
Mage
Checkout
checkout_onepage_controller_success_action’
Mage
Checkout
checkout_controller_onepage_save_shipping_method
Mage
Checkout
checkout_allow_guest
Mage
Checkout
checkout_cart_product_add_after
Mage
Checkout
checkout_cart_update_items_before
Mage
Checkout
checkout_cart_update_items_after
Mage
Checkout
checkout_cart_info_item_unset_product_before
Mage
Checkout
checkout_quote_init
Mage
Checkout
checkout_quote_destroy
Mage
Checkout
checkout_type_multishipping_set_shipping_items
Mage
Checkout
checkout_type_multishipping_create_orders_single
Mage
Checkout
checkout_type_onepage_save_order
Mage
Checkout
checkout_type_onepage_save_order_after
Mage
Cms
cms_controller_router_match_before
Mage
Cms
cms_page_get_available_statuses
Mage
Core
core_block_abstract_prepare_layout_before
Mage
Core
core_block_abstract_prepare_layout_after
Mage
Core
core_block_abstract_to_html_before
Mage
Core
core_block_abstract_to_html_after
Mage
Core
http_response_send_before
Mage
Core
controller_action_layout_load_before
Mage
Core
controller_action_layout_generate_xml_before
Mage
Core
controller_action_layout_generate_blocks_before
Mage
Core
controller_action_layout_generate_blocks_after
Mage
Core
controller_action_layout_render_before’
Mage
Core
controller_action_layout_render_before_’.$this→getFullActionName()
Mage
Core
controller_action_predispatch
Mage
Core
controller_action_predispatch_’.$this→getRequest()→getRouteName()
Mage
Core
controller_action_predispatch_’.$this→getFullActionName()
Mage
Core
controller_action_postdispatch_’.$this→getFullActionName()
Mage
Core
controller_action_postdispatch_’.$this→getRequest()→getRouteName()
Mage
Core
controller_action_postdispatch
Mage
Core
controller_action_noroute
Mage
Core
controller_action_nocookies
Mage
Core
controller_front_init_before
Mage
Core
controller_front_init_routers
Mage
Core
model_load_after
Mage
Core
$this→_eventPrefix.’_load_after’
Mage
Core
model_save_commit_after
Mage
Core
$this→_eventPrefix.’_save_commit_after’
Mage
Core
model_save_before
Mage
Core
$this→_eventPrefix.’_save_before’
Mage
Core
model_save_after
Mage
Core
$this→_eventPrefix.’_save_after’
Mage
Core
model_delete_before
Mage
Core
$this→_eventPrefix.’_delete_before’
Mage
Core
model_delete_after
Mage
Core
$this→_eventPrefix.’_delete_after’
Mage
Core
model_delete_commit_after
Mage
Core
$this→_eventPrefix.’_delete_commit_after’
Mage
Core
application_clean_cache
Mage
Core
core_locale_set_locale
Mage
Core
resource_get_tablename
Mage
Core
core_layout_update_updates_get_after
Mage
Core
core_layout_update_updates_get_after
Mage
Core
core_collection_abstract_load_before
Mage
Core
core_collection_abstract_load_after
Mage
Customer
customer_registration_is_allowed
Mage
Customer
customer_customer_authenticated
Mage
Customer
customer_session_init
Mage
Customer
customer_login
Mage
Customer
customer_login
Mage
Customer
customer_login
Mage
Customer
customer_logout
Mage
Eav
adminhtml_block_eav_attribute_edit_form_init
Mage
Eav
eav_attribute_get_backend_type_by_input
Mage
Eav
eav_attribute_get_default_value_by_input
Mage
Eav
eav_collection_abstract_load_before
Mage
GoogleCheckout
google_checkout_discount_item_price
Mage
Log
log_log_clean_before
Mage
Log
log_log_clean_after
Mage
Log
log_visitor_collection_load_before
Mage
Oscommerce
store_add
Mage
Payment
payment_method_is_active
Mage
Paypal
checkout_type_onepage_save_order
Mage
Paypal
checkout_type_onepage_save_order
Mage
PaypalUk
checkout_type_onepage_save_order
Mage
PaypalUk
checkout_type_onepage_save_order
Mage
Review
review_controller_product_init_before
Mage
Review
review_controller_product_init
Mage
Review
review_controller_product_init_after
Mage
Review
review_review_collection_load_before
Mage
Rss
rss_catalog_category_xml_callback
Mage
Rss
rss_catalog_new_xml_callback
Mage
Rss
rss_catalog_special_xml_callback
Mage
Rss
rss_catalog_tagged_item_xml_callback
Mage
Rss
rss_order_new_collection_select
Mage
Rule
rule_environment_collect
Mage
Sales
sales_order_place_before
Mage
Sales
sales_order_place_after
Mage
Sales
sales_quote_remove_item
Mage
Sales
sales_quote_add_item
Mage
Sales
$this→_eventPrefix . ‘_collect_totals_before’
Mage
Sales
$this→_eventPrefix . ‘_collect_totals_after’
Mage
Sales
$this→_eventPrefix . ‘_merge_before’
Mage
Sales
$this→_eventPrefix . ‘_merge_after’
Mage
Sales
sales_convert_order_to_quote
Mage
Sales
sales_convert_quote_to_order
Mage
Sales
sales_convert_quote_address_to_order
Mage
Sales
sales_convert_quote_address_to_order_address
Mage
Sales
sales_convert_quote_payment_to_order_payment
Mage
Sales
sales_convert_quote_item_to_order_item
Mage
Sales
sales_quote_item_collection_products_after_load
Mage
Sales
sales_sale_collection_query_before
Mage
Sales
sales_order_creditmemo_refund
Mage
Sales
sales_order_creditmemo_cancel
Mage
Sales
sales_order_invoice_pay
Mage
Sales
sales_order_invoice_cancel
Mage
Sales
sales_order_item_cancel
Mage
Sales
sales_order_payment_place_start
Mage
Sales
sales_order_payment_place_end
Mage
Sales
sales_order_payment_capture
Mage
Sales
sales_order_payment_pay
Mage
Sales
sales_order_payment_cancel_invoice
Mage
Sales
sales_order_payment_void
Mage
Sales
sales_order_payment_refund
Mage
Sales
sales_order_payment_cancel_creditmemo
Mage
Sales
sales_order_payment_cancel
Mage
Sales
sales_quote_item_qty_set_after
Mage
Sales
sales_quote_item_set_product
Mage
Sales
$this→_eventPrefix . ‘_import_data_before’
Mage
Sales
sales_quote_address_discount_item
Mage
Sales
sales_quote_address_discount_item
Mage
SalesRule
salesrule_validator_process
Mage
SalesRule
sales_quote_address_discount_item
Mage
SalesRule
sales_quote_address_discount_item
Mage
SalesRule
salesrule_rule_condition_combine
Mage
Sendfriend
sendfriend_product
Mage
Tag
tag_tag_product_collection_load_after
Mage
Tax
tax_rate_data_fetch
Mage
Tax
tax_settings_change_after’
Mage
Tax
tax_settings_change_after’
Mage
Tax
tax_settings_change_after’
Mage
Tax
tax_settings_change_after’
Mage
Tax
tax_settings_change_after’
Mage
Wishlist
wishlist_add_product
Mage
Wishlist
wishlist_share

Friday, May 11, 2012

Get Store Information in Magento

Here is the following code to get Store information in Magento :


Get store data
Mage::app()->getStore();
Store Id
Mage::app()->getStore()->getStoreId();
Store code
Mage::app()->getStore()->getCode();
Website Id
Mage::app()->getStore()->getWebsiteId();
Store Name
Mage::app()->getStore()->getName();
Is Active
Mage::app()->getStore()->getIsActive();
Store Home Url
Mage::app()->getStore()->getHomeUrl();
------------------------------------------------------------
Use it and have fun :)

Saturday, April 28, 2012

Product Collection in Magento


In this blog, we will see some important function in magento product collection class.
Product Collection class in magento is Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection. Lets look at the important functions
Get All Products of a category


$collection = Mage::getResourceModel('catalog/product_collection')
            ->setStoreId($this->getStoreId())
            ->addCategoryFilter($category);

Tn this the addCategoryFilter() function, is used to get all products of a particular category. So, if you want to get all products of a certain category use this function.
Visibility Filter


$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);


The addVisibleFilterToCollection() adds visibility filter to a product collection i.e only products which are visible in frontend. The product which have “Not Visible Individually” selected in admin are removed.
Status Filter


$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);


This basically filters out products which are “Disabled”. Only “Enabled” products remain in the collection.
Add Product Price To Collection


$collection = Mage::getResourceModel('catalog/product_collection');
$collection ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents();


This adds the product prices, i.e base price, final price etc to the collection. Also, price after tax, if applicable.
Filter By Ids


$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter(array(1,2,3));
//$collection->addIdFilter(array(1,2,3),false);


This puts an id filter, only product with ids 1,2,3 remain in the collection. The function parameter is true/false, this means include/exclude products from collection.
Add Website ID to the collection


$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addWebsiteNamesToResult();