Monday, 16 March 2015

Detect Home Page – Magento

Detect Home Page – Magento

Method 1 (For All Version of Magento):
if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true)))
{
  // Home page
}
Method 2 (For Magento 1.5 and Above):
if($this->getIsHomePage())
{
  // Home page
}

Detect Category Page – Magento

if (Mage::registry('current_category'))
{
  // category page
}
Now see how we can get ID and Name of the category if current page is category page.
if (Mage::registry('current_category'))
{
  // Category Name
  echo Mage::registry('current_category')->getName();

  // Category ID
  echo Mage::registry('current_category')->getId();
}

Detect CMS Page – Magento

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
{
  // CMS page
}
Get CMS page name if current one is the CMS page.
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
{
  echo Mage::getSingleton('cms/page')->getIdentifier();
}

Detect Product Detail Page – Magento

if(Mage::registry('current_product'))
{
  // Product detail page
}

Detect Configure Product Page – Magento

if(Mage::app()->getFrontController()->getRequest()->getRequestedActionName() == 'configure')
{
  // Product Configuration page
}

Detect Cart Page – Magento


 $request = $this->getRequest();
 $module = $request->getModuleName();
 $controller = $request->getControllerName();
 $action = $request->getActionName();
 if($module == 'checkout' && $controller == 'cart' && $action == 'index')
 {
   //Cart Page
 }

Update Prefix sales order id in magento

Update Prefix sales order id :




UPDATE ex_eav_entity_store
       INNER JOIN ex_eav_entity_type
         ON ex_eav_entity_type.entity_type_id = ex_eav_entity_store.entity_type_id
SET    ex_eav_entity_store.increment_last_id = 'EL0100001663',ex_eav_entity_store.increment_prefix = 'EL0'
WHERE  ex_eav_entity_type.entity_type_code = 'order'
       AND ex_eav_entity_store.store_id = 1;



UPDATE ex_eav_entity_store
       INNER JOIN ex_eav_entity_type
         ON ex_eav_entity_type.entity_type_id = ex_eav_entity_store.entity_type_id
SET    ex_eav_entity_store.increment_last_id = 'EL0100001647',ex_eav_entity_store.increment_prefix = 'EL0'
WHERE  ex_eav_entity_type.entity_type_code = 'quote'
       AND ex_eav_entity_store.store_id = 1;



UPDATE ex_eav_entity_store
       INNER JOIN ex_eav_entity_type
         ON ex_eav_entity_type.entity_type_id = ex_eav_entity_store.entity_type_id
SET    ex_eav_entity_store.increment_last_id = '100000002'
WHERE  ex_eav_entity_type.entity_type_code = 'invoice'
       AND ex_eav_entity_store.store_id = 1;



UPDATE ex_eav_entity_store
       INNER JOIN ex_eav_entity_type
         ON ex_eav_entity_type.entity_type_id = ex_eav_entity_store.entity_type_id
SET    ex_eav_entity_store.increment_last_id = '100000009'
WHERE  ex_eav_entity_type.entity_type_code = 'shipment'
       AND ex_eav_entity_store.store_id = 1;


UPDATE ex_eav_entity_store
       INNER JOIN ex_eav_entity_type
         ON ex_eav_entity_type.entity_type_id = ex_eav_entity_store.entity_type_id
SET    ex_eav_entity_store.increment_last_id = '100064999'
WHERE  ex_eav_entity_type.entity_type_code = 'creditmemo',ex_eav_entity_store.increment_prefix = 'EL0'
       AND ex_eav_entity_store.store_id = 1;

Update Magento product / inventory from external source.

Update Magento product / inventory from external source.

Update inventory creating new module:
At first create new module in local pool to handle inventory update work. And create all of your necessary files. Then follow below steps to update your inventory.
Load product: load product using product id. If you have product sku then retrieve product id for corresponding sku.
1
2
3
4
5
6
7
8
// retrieve product id using sku
 $product_id = Mage::getModel('catalog/product')
                    ->getIdBySku($sku);

// call product model and create product object
$product    = Mage::getModel('catalog/product');
// Load product using product id
 $product ->load($product_id);
Set updated inventory data: set all of your updated data in product object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// get product's general info such price, status, description
$productInfoData = $product->getData();

// update general info using new data
$productInfoData['price'] = 11;
$productInfoData['description'] = 'Testing product update';
$productInfoData['status'] = 1;

// then set product's general info to update
$product->setData($productInfoData);

// get product's stock data such quantity, in_stock etc
$stockData = $product->getStockData();

// update stock data using new data
$stockData['qty'] = 356;
$stockData['is_in_stock'] = 1;

// then set product's stock data to update
$product->setStockData($stockData);

// call save() method to save your product with updated data
$product->save();
Basically our product update process is completed but in this situation updated product will not be save. Magento want origData to save product object. Without admin session magento cannot create origData for product. This is a magento security issue. So if you want to update magento product outside of admin or without creating admin session then you need to override Mage_Catalog_Model_Product class.
Configure new module and create app/local/M4U/catalog/etc/c onfig.xml file and write below code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0"?>
<config>
    <global>
    <modules>
           <m4u_catalog>
               <version>0.1.0</version>
           </m4u_catalog>
        </modules>
    <models>
      <catalog>
            <rewrite>
              <product>M4U_Catalog_Model_Product</product>
            </rewrite>
          </catalog>
       </models>
   </global> 
</config>
Override Mage_Catalog_Model_Product class and write setOrigData method as below.
1
2
3
4
5
6
7
8
9
10
11
12
13
class M4U_Catalog_Model_Product extends Mage_Catalog_Model_Product
{
    /**
     * Set original loaded data if needed
     * @param string $key
     * @param mixed $data
     * @return Varien_Object
     */
    public function setOrigData($key=null, $data=null)
    {
       return Mage_Catalog_Model_Abstract::setOrigData($key, $data);
    }
}



Magento: Change the ‘Price’ sort by option to ‘Price: Low > High’ and ‘Price: High > Low’

Magento: Change the ‘Price’ sort by option to ‘Price: Low > High’ and ‘Price: High > Low’

Posted on
This is a quick way of change the Price sort by option in Magento, to ‘Price: Low > High’ and ‘Price: High > Low’.
I know there is the sort by direction ‘arrow’ next to the sort by option, but it is not clearly visible to the customers.

So, make the following changes to the /app/design/frontend/base/default/template/catalog/product/list/toolbar.phtml file.
Change
1
2
3
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) ?>
</option>
to
1
2
3
4
5
6
7
8
9
10
11
12
<?php if ($_order != 'Price'): ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) ?>
</option>
<?php else: ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key) && $this->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) . ': Low > High' ?>
</option>
<option value="<?php echo $this->getOrderUrl($_key, 'desc') ?>"<?php if($this->isOrderCurrent($_key) && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
    <?php echo $this->__($_order) . ': High > Low' ?>
</option>
<?php endif; ?>


SMTP settings for Magento

SMTP settings for Magento
If you want to send emails with Magento you need to configure it to use an SMTP server – that is, the outgoing server that takes care of delivering your messages.
Remember anyway that if you set a normal SMTP server on Magento – like the ones associated to Gmail or Hotmail – you could always run into deliverability problems. Only a professional SMTP service will guarantee a full delivery rate, so if you plan to set up an email campaign you should consider this option seriously.
And here's the procedure:
1. Login to Magento and choose System->Configuration->Advanced->System->Mail Sending Settings.
2. Insert your SMTP settings: your SMTP server name (if you don't know it have a look at our handy list of the major ones) and your SMTP port (the default one is 25, but you can choose among others as well).
3. Copy in your local the file app/code/core/Mage/core/Model/Email/Template.php.
4. Enable that module.
5. Open the file Template.php and change the getMail() function this way:
public function getMail() 
      {
           if (is_null($this->_mail)) {               
               /* changes begin */                   $my_smtp_host = Mage::getStoreConfig('system/smtp/host');
             $my_smtp_port = Mage::getStoreConfig('system/smtp/port');
             $
config = array(
                       
'port' => $my_smtp_port,                                              'auth' => 'login',                 
                       'username' => 'email@domain.com',
                       
'password' => 'yourpassword'                                               );
            $transport = new Zend_Mail_Transport_Smtp($my_smtp_host, $config);
            Zend_Mail::setDefaultTransport($transport);
            /* Changes End */
            $this->_mail = new Zend_Mail('utf-8');
        }
        return $this->_mail;
  
}
That's it, you can deliver emails with Magento.



Rename or remove [Add New] button from grid in magento admin

Normally, the [Add New] button is present on upper right corner of Grid Page in Magento backend. If you don’t wish to show the [Add New] button in the Grid you need to code a little to disable it.
How to rename [Add New] button
Go to grid page block file, the directory likes app -> code -> local -> YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
Then add the following code to __construct function
?
1
$this->_addButtonLabel = Mage::helper('yourmodulename')->__('Add Report'); //you can replace 'Add Report' with any text you want
How to remove [Add New] button
Go to grid page block file, the directory likes app -> code -> local -> YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile.php
Then add the following code to __construct function(note: the remove statement code must be under the parent constructor statement)
?
1
2
parent::__construct();
$this->_removeButton('add');
Also, how to remove back, delete, and save button in edit page
Go to grid page block file, the directory likes app -> code -> local -> YourNamespace -> YourModule -> Block -> Adminhtml -> YourFile -> edit.php
Then add the following code to __construct function(note: the remove statement code must be under the parent constructor statement)
?
1
2
3
4
parent::__construct();
$this->_removeButton('delete');
$this->_removeButton('save');
$this->_removeButton('back');



Remove My Downloadable Products link from the My Account Block in Magento



In Magento, on your ecommerce site may be such that you do not have any downloadable products. In that case, you may not want to show the “My Downloadable Products” link in the My Account sidebar. Follow the steps below to remove the link.
1. Copy the file downloadable.xml from app/design/frontend/base/default/layout to app/design/frontend/YOUR_PACKAGE/YOUR_THEME/layout or app/design/frontend/default/YOUR_THEME/layout
2. Edit the downloadable.xml copied to the app/design/frontend/YOUR_PACKAGE/YOUR_THEME/layout folder or app/design/frontend/default/YOUR_THEME/layout folder
3. Look for the following Code snippet
<customer_account>
<reference name="customer_account_navigation">
<action method="addLink" translate="label" module="downloadable"><name>downloadable_products</name><path>downloadable/customer/products</path><label>My Downloadable Products</label></action>
</reference>
</customer_account>

4. Comment the following Lines
<action method="addLink" translate="label" module="downloadable"><name>downloadable_products</name><path>downloadable/customer/products</path><label>My Downloadable Products</label></action>
to
<!--<action method="addLink" translate="label" module="downloadable"><name>downloadable_products</name><path>downloadable/customer/products</path><label>My Downloadable Products</label></action>-->

5. Refresh the Magento Cache and Website Page to see the changes



Remove My Application Link from Customer Account Page in magento

Monday, 17. December 2012
If you ever wanted to remove the “My Application” link from customer’s My Account page here is the layout file to look for
app/design/frontend/[YOUR PACKAGE]/[YOUR PACKAGE]/layout/oauth.xml
Around line 118 – you will see a block of code similar to snippet below which you can simply comment it out.
<!-- My Applications-->
<customer_account>
<reference name="customer_account_navigation">
<action method="addLink" translate="label" module="oauth">
<name>OAuth Customer Tokens</name>
<path>oauth/customer_token</path>
<label>My Applications</label>
</action>
</reference>
</customer_account>



Remove google site verification meta tag from magento

Remove google site verification meta tag from magento



Had the same issue and on Magento ver. 1.8.0.0 - community.

- Log in to your Admin account
- Navigate to System > Configuration > Under "General" section on left side > Design and under "HTML Head" paste the code in the input filed next to "Miscellaneous Scripts"

For me I used to place the verification code for Google (Youtube, Webmaster, Adsense)
i.g.: <-meta name="google-site-verification" content="xxxxxxxxxxxxxxxxxxxxxxxxxxxx" /->


Check backend System -> Configuration -> Design -> HTML Head -> Miscellaneous Scripts and remove mentioned code leaving only following line there:
<meta name="google-site-verification" content="MhgZldxDOmNWQ74FSWwNH3Ou7SfCHuTCq1N640thSUI" />


Remove Extra Spaces Using JavaScript

  1. Remove Extra Spaces Using JavaScript

This simple script uses regular expressions to remove unwanted extra spaces from a block of text. The following example illustrates the script (you can paste your own text here if you like):
 
To use this script, place the following code in the document head:
function
trimSpaces(){
        s
= document.getElementById("textString").value;
        s
= s.replace(/(^\s*)|(\s*$)/gi,"");
        s
= s.replace(/[ ]{2,}/gi," ");
        s
= s.replace(/\n /,"\n");
       
document.getElementById("textString").value
= s;
}
Place the following code in the document body (modify to suit your needs):
<textarea
name="textString" id="textString" cols="50"
rows="4">
 This
is a block of text with some  extra    spaces in it.   Click the
button to remove them. 
</textarea>


Redirect to home page if cart is empty in magento

Redirect to home page if cart is empty

  1. In app/design/frontend/default/your-theme/template/checkout/cart/noItems.phtml add (this may not be the best solution but does work)
    
     <?php
     Mage::app()->getResponse()->setRedirect($this->getContinueShoppingUrl());
     ?>
  2. Create an observer (try controller_action_predispatch_checkout_cart_delete) that check if your cart is empty then redirect see to the home page (for redirecting from observer see)
  3. Using javascript and timer so that the user will see that there cart is empty before redirecting to home page (see time delayed redirect?) Add code below to noItems.phtml see solution #1
    <script>
     setTimeout(function
     () {
       window.location.href
     = "<?php echo $this->getContinueShoppingUrl() ?>";
     //will redirect  to your blog page (an ex: blog.html)
     },
     2000); //will call the function after 2 secs.
    </script>


PDF Invoice; changing Sold to: in Billing to: other text in magento

PDF Invoice; changing Sold to: in Billing to: other text



Go app\design\adminhtml\default\default\locale\en_US
translate.csv
Add your text where u want to change first column Shipping & Handling second column on you want a text.
Add your label translate list on this file.
Example:
“Sold to:”, “Billing to:”