Remove .std div (wrapper) from Magento CMS Pages through xml

Add below xml code to local.xml of your current theme or cms.xml file.

1. Remove from all cms pages.

<cms_page>
    <reference name="content">
        <remove name="cms.wrapper"/>
    </reference>
</cms_page>

2. Remove from CMS Home page only

<cms_index_index>
    <reference name="content">
        <remove name="cms.wrapper"/>
    </reference>
</cms_index_index>

Display Magento cart total Block in custom pages or XML

There is a two way to display checkout cart total block to any page.

1. Through .phtml

<?php
    // Method 1: 
              echo $this->getLayout()->createBlock('checkout/cart_totals')
                                     ->setTemplate('checkout/cart/totals.phtml')->toHtml();

    // Method 2: 
               $layout = $this->getLayout();
               $layout->getUpdate()
                      ->addHandle('default')
                      ->addHandle('checkout_cart_index')
                      ->load();
               $layout->generateXml()->generateBlocks();
               $total_html = $layout->getBlock('checkout.cart.totals')->toHtml();
               echo $total_html;
?>

2. Through xml

<reference name="content">
    <block type="checkout/cart_totals" name="custom.cart.total" template="checkout/cart/totals.phtml" />
</reference>

Split single table field to two separate fields in Magento collection

Here is the example of split order street address in to 2 separate fields separated by ‘n’ newline.

<?php
$collection = Mage::getModel('sales/order_address')->getCollection()
            ->addExpressionFieldToSelect('street_line1',
                    'IF(LOCATE("n", {{full_street}}) > 0,
                        SUBSTRING({{full_street}}, 1, LOCATE("n", {{full_street}}) - 1),
                        {{full_street}} )',
                array('full_street' => 'street'))

            ->addExpressionFieldToSelect('street_line2',
                    'IF(LOCATE("n", {{full_street}}) > 0,
                        SUBSTRING({{full_street}}, LOCATE("n", {{full_street}}) + 1),
                        NULL )',
                array('full_street' => 'street'));            
print_r($collection->getData());        
?>

On print_r($collection->getData()); you can see the 2 new field added to result ‘street_line1′ and ‘street_line2′.

Note : Use ‘addExpressionAttributeToSelect’ if not works ‘addExpressionFieldToSelect’.

Merge two table fields to single in Magento collection

Here is the example of merging Product name and SKU from Order Item collection by ‘ ‘ Space.

<?php 
$collection = Mage::getResourceModel('sales/order_item_collection')
            ->addExpressionFieldToSelect(
                'skuname',
                'CONCAT({{itemsku}}, ' ', {{itemname}})',
                array('itemsku' => 'sku', 'itemname' => 'name'));

print_r($collection->getData()); 
?>

On print_r($collection->getData()); you can see the one new field added to result ‘skuname’ having sku and name merged by ‘ ‘ space.
Note : Use ‘addExpressionAttributeToSelect’ if not works ‘addExpressionFieldToSelect’.

create tar in php

PharData is PHP Class that can be used to create tar and tar.gz compressed files.
It works for php 5.3+ onwards. Lets have a look.

<?php 

$file = new PharData('compress.tar');    

// Adding single files
$file->addFile('index.html');
$file->addFile('myfile.php');

// Adding Directory 
$file->buildFromDirectory('directory/path/');


// Compress tar
$file->compress(Phar::GZ);

?>

Magento custom search engine supported (SEO) URL.

1. Create Helper file and function in your module to use throughout of your project.

class <Namespace>_<Module>_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function geturlRewrite($id_path, $request_path, $target_path, $store_id ="0", $is_system=false)
    {
        $isrewrite = Mage::getModel('core/url_rewrite')->load($id_path,id_path);

        if($isrewrite->url_rewrite_id !=""){
            $rewrite = Mage::getModel('core/url_rewrite')->load($isrewrite->url_rewrite_id);
           //throw new Exception("Your Id path must be unique, given id path already in use !");
        }else{
            $rewrite = Mage::getModel('core/url_rewrite');
        }
            $rewrite->setStoreId($store_id)
            ->setIdPath($id_path)
            ->setRequestPath($request_path)
            ->setTargetPath($target_path)
            ->setIsSystem($is_system)
            ->save();  

        return true;
    }

    // Generate seo url from author name (any string)
    public function formatAuthorUrl($str)
    {
        $urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
        $urlKey = strtolower($urlKey);
        $urlKey = trim($urlKey, '-');
        return $urlKey;
    }
}

2. Call the helper function on save Action of any controller based on your requirement OR
You may also use helper function any where in project where required.

public function saveAction()
{
&nbsp;
// .... faq saving process
$aincr = $model->save()->getId();
&nbsp;

// Generate URL key
    try{    
        $id_path = "author/".$aincr;  // Id path must be unique
        $request_path = "author/".Mage::helper('<Namespace>_<Module>')->formatAuthorUrl($authorName);
        $target_path  = "author/index/index/id/".$aincr.";                 
        Mage::helper('<Namespace>_<Module>')->geturlRewrite($id_path, $request_path, $target_path);    
        $slug['author_slug'] = $request_path;
        $model->addData($slug)->save();
    }
    catch (Exception $e) {                 
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());                
    }
}

Add Custom Tab in Product view page.

1. Add below xml code at appdesignfrontendyour packageyour themelayoutlocal.xml

<catalog_product_view>
     <reference name="product.info">
           <block type="catalog/product_view" name="author.bio" after="product.description" as="authorbio" template="catalog/product/view/author-bio.phtml">
                   <action method="addToParentGroup"><group>detailed_info</group></action>
                    <action method="setTitle" translate="value"><value>Author Bio</value></action>
           </block>                
     </reference>
</catalog_product_view>
<!-- Here we have added Author Bio as extra tab -->

2. Create new file for content to show – appdesignfrontendyour packageyour themetemplatecatalogproductviewauthor-bio.phtml

Here example of Product attribute ‘Author bio’ content or you may write your own content here

$_authorid = $this->getProduct()->getAuthor(); // Get Product Attribute
$author = Mage::getModel('krish_author/author')->getCollection()
          ->addFieldToFilter('author', $_authorid)->getFirstItem();

if ($author->getId()): 
?>
    <h2><?php echo $this->__('About '.$this->getProduct()->getAttributeText('author')) ?></h2>
    <div class="std">   	
        <?php if($author->getAuthorImage() != '') echo '<img src="'.$author->getAuthorImage().'" class="author_image"/>'; ?>    
        <?php if($author->getAuthorInfo() != '') echo '<b>About '.$this->getProduct()->getAttributeText('author').' :</b> '.$author->getAuthorInfo(); ?>
        <?php if($author->getAuthorLinks() != '') echo '<b>Realted Links: </b>'.$author->getAuthorLinks(); ?>
    </div>
<?php endif; ?>

 

Display Multiselect product attribute Values

<?php 
    // Here 'author' is multiselect product attribute.
    $authors = $_product->getAttributeText('author'); 
    $author_names = (is_array($authors)) ? implode( ', ', $authors) : $authors; 
    // Comma separated values, You can change your convenient.
    if($author_names!= ''):
        echo $author_names;
    endif;
?>