Thursday 31 January 2013

Add Newsletter checkBox to Contacts page

add this line to the template/contacts/form.phtml


<li class="control"><input type="checkbox" name="is_subscribed" id="subscription" value="1" title="<?php echo $this->__('General Subscription') ?>" class="checkbox" /><label for="subscription"><?php echo $this->__('General Subscription') ?></label></li>



add the code to app/code/core/Mage/Contacts/controllers/indexcontroller.php


if($post['is_subscribed'] == 1){
$status = Mage::getModel('newsletter/subscriber')->subscribe($post['email']);
if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
    Mage::getSingleton('core/session')
       ->addSuccess($this->__('Confirmation request has been sent.'));
}
else {
    Mage::getSingleton('core/session')
        ->addSuccess($this->__('Thank you for your subscription.'));
}
 }      

in function :- public function postAction()

Friday 25 January 2013

Quickly Add to Cart via Quantity and SKU of the product

 Put This code to your home page or where you want to add this module:-


<?php
// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');

if(isset($_POST['submit'])){

    if($sku = $_POST['sku']){
        $sku = $_POST['sku'];
        $p = Mage::getModel('catalog/product')->getIdBySku($sku);

        // call the Magento catalog/product model
        $product = Mage::getModel('catalog/product')
                     // set the current store ID
                     ->setStoreId(Mage::app()->getStore()->getId())
                     // load the product object
                     ->load($p);
   
        $cart->addProduct($product, array(
            'qty' => $_POST['qty'],
            'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
            )
        );
    }
    if($sku1 = $_POST['sku1']){
        $sku1 = $_POST['sku1'];
        $p1 = Mage::getModel('catalog/product')->getIdBySku($sku1);

        // call the Magento catalog/product model
        $product = Mage::getModel('catalog/product')
                     // set the current store ID
                     ->setStoreId(Mage::app()->getStore()->getId())
                     // load the product object
                     ->load($p1);
   
        $cart->addProduct($product, array(
            'qty1' => $_POST['qty1'],
            'super_attribute' => array( key($_POST['super_attribute']) => $_POST['super_attribute'][525] )
            )
        );
}
// save the cart
    $cart->save();
   
// very straightforward, set the cart as updated
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}else{
?>
<html>
<head></head>
<body>
<div style="width: 400px; margin: auto">
   
   
    <div style="width: 180px; margin:auto; line-height: 30px">
        <form action="#" method="post">   
            Quantity: <input type="text" name="qty" size="1" />
            SKU: <input type="text" name="sku" size="1" />
            Quantity: <input type="text" name="qty1" size="1" />
            SKU: <input type="text" name="sku1" size="1" />

            <input type="submit" name="submit" value="Add This">
           
        </form>
    </div>
    <hr>
    <strong><?php echo "Cart Qty: ".$cart->getItemsQty();?></strong>
</div>
</body>
</html>
<?php } ?>

Add a Specific product to Home page

<?php
$_product = Mage::getModel('catalog/product')->load(4); //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
$p_img = $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url 

?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $p_img; ?>" width="135" height="135" alt="image" /></a>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">Add To Cart</a>

Add active class to custom Cms page added to top_menu.phtml page

<li class="level0 nav-5<?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl().'special/'):?> active<?php endif;?>"<a href="<?php echo $this->getUrl('sale'); ?>">Sale</a></li>>

Sunday 13 January 2013

Showing the Best Seller Product by code..


Step 1: Create a file app/code/local/Mage/Catalog/Block/Product/Bestseller.php and the following lines of code in it

class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract{
public function __construct(){
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc'); // most best sellers on top
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products );

$products->setPageSize(3)->setCurPage(1);
$this->setProductCollection($products);
}
}



Step 2: Create a file app/design/frontend/default/YourTheme/template/catalog/product/bestseller.phtml file and add the following lines of code in it

<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="page-title">
<h2><?php echo $this->__('Best Seller Products') ?></h2>
</div>
<?php $_collectionSize = count($_products->getItems()) ?>
<table class="products-grid" id="products-grid-table">
<?php $i=1; foreach ($_products->getItems() as $_product): ?>
<?php if ($i%1!==0): ?>
<tr>
<?php endif ?>
<td id="td_<?php echo $i;?>" <?php if($i%3==0 or $i==$_collectionSize){echo 'class="last"';} ?> >
<?php contentBlock('top') ?>
<div id="cont_<?php echo $i;?>">
<h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
<a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(122, 109); ?>" width="122" height="109" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
<div class="a-center">
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($_product->isSaleable()): ?>
<button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
<div class="clear"></div>
<?php else: ?>
<p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
<div class="clear"></div>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
<?php if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
</div>
</div>
</td>
<?php if ($i%3==0 or $i==$_collectionSize): ?>
</tr>
<?php endif ?>
<?php $i++; endforeach; $kol = $_collectionSize; ?>
</table>
<?php endif; ?>


Step 3: This above files will create a list of best selling products which can be shown anywhere on your Magento store. All you have to do is place the following line of code block in your template to show the best selling products.

{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}

New and sale label on products


on view page in place in between:-<div class="product-img-box">
            <?php echo $this->getChildHtml('media') ?>



<?php // SALE label placement
if ( ($_product->getFinalPrice() - $_product->getPrice()) < 0) { ?>
<img class="labels labels-top-right" src="<?php echo $this->getSkinUrl('images/labels/sale.png'); ?>">
<?php } ?>
<?php // NEW label placement
$now = date("Y-m-d");
$newsFrom= substr($_product->getData('news_from_date'),0,10);
$newsTo=  substr($_product->getData('news_to_date'),0,10);
if ($now>=$newsFrom && $now<=$newsTo)
{?> <img class="labels labels-top-left" src="<?php echo $this->getSkinUrl('images/labels/new.png'); ?>"><?};
?>






</div>





css:-
/* Labels CSS
-------------------------------------------------------------- */
.labels { border:none; position:absolute; z-index:99; }
.labels-top-right { top:-2px; right:-2px; }
.labels-bottom-right { bottom:-2px; right:-2px; }
.labels-bottom-left { bottom:-2px; left:-2px; }
.labels-top-left { top:-2px; left:-2px; }




in .product-img-box


position: relative;




To show current category's Subcatagory


<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>

        <?php foreach($_categories as $_category): ?>


                <?php if ($currentCategory->getId() == $_category->getId()): ?>
                    <?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>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                        <?php endforeach; ?>
                        </ul>
                <?php endif; ?>
                <?php endif; ?>

     <?php endforeach; ?>

<?php endif; ?>