Tuesday 2 December 2014

Magento get actual price and special price of a product?

$_formattedActualPrice = Mage::helper('core')->currency($_product->getPrice(),true,false);

$_formattedSpecialPrice = Mage::helper('core')->currency($_product->getFinalPrice(),true,false);

Hope it helps. Thanks.

Magento – Add “You Save” amount and percentage to Special Price in Magento.

<?php if($_finalPrice < $_price): ?>
 <?php
    $_savePercent = 100 - round(($_finalPrice / $_price) * 100);
    $_saveAmount = number_format(($_price - $_finalPrice), 2);
?>
    <p class='yousave'>
        <span class='price-label label'>You Save: </span>
        <span class='price'>
            <strong class='save-amount'>$<?php echo $_saveAmount; ?></strong> (<?php echo $_savePercent; ?>%)
        </span>
    </p>
<?php endif; ?>

Magento get Bestseller products.

To display bestselling product we need to create a block file.

class Magento_kamal_Block_Bestsellers extends Mage_Core_Block_Template
{
    public function getBestsellerProducts()
    {
        $storeId = (int) Mage::app()->getStore()->getId();
        // Date
        $date = new Zend_Date();
        $toDate = $date->setDay(1)->getDate()->get('Y-MM-dd');
        $fromDate = $date->subMonth(1)->getDate()->get('Y-MM-dd');
        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addStoreFilter()
            ->addPriceData()
            ->addTaxPercents()
            ->addUrlRewrite()
            ->setPageSize(6);
        $collection->getSelect()
            ->joinLeft(
                array('aggregation' => $collection->getResource()->getTable('sales/bestsellers_aggregated_monthly')),
                "e.entity_id = aggregation.product_id AND aggregation.store_id={$storeId} AND aggregation.period BETWEEN '{$fromDate}' AND '{$toDate}'",
                array('SUM(aggregation.qty_ordered) AS sold_quantity')
            )
            ->group('e.entity_id')
            ->order(array('sold_quantity DESC', 'e.created_at'));
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        return $collection;
    }
}

After that we need to create bestsellers.phtml file.

<div class="box-best-sellers"> 
<h2>Best Selling Products</h2>
<table border="0" cellspacing="0">
    <tbody>
    <?php $counter=0; foreach ($this->getBestsellerProducts() as $product): ?>
        <?php if ($counter%2 == 0): ?><tr class="<?php echo $counter%4 ? 'even' : 'odd'; ?>"><?php endif ?>
        <td>
            <a href="<?php echo $product->getProductUrl() ?>"><img class="product-img" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(99); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" width="95" border="0" /></a>
            <div class="product-description">
                <p><a href="<?php echo $product->getProductUrl() ?>"><?php echo $this->stripTags($product->getName(), null, true); ?></a></p>
            </div>
        </td>
        <?php if ($counter++%2): ?></tr><?php endif ?>
    <?php endforeach; ?>
    </tbody>
</table>
</div>

I hope it will help...

Magento Check if a module is installed, enabled or active.

$moduleName = 'Mage_Core'; // edit to your required module name
if (Mage::helper('core')->isModuleEnabled($moduleName)) {
    echo "Module is enabled.";
} else {
    echo "Module is disabled.";
}

Magento How to get product stock quantity & other stock information?

1. Load product by product ID
$id = 52;
$_product = Mage::getModel('catalog/product')->load($id);

2. Load product by SKU
$sku = "microsoftnatural";
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

Now, get stock information for the loaded product.
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

You can check stock data in this way:-
echo "<pre>"; print_r($stock->getData()); echo "</pre>";

Or, you can print individually like this:-
echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Magento How to get all associated children product of a configurable product?

Load product by product id

$product = Mage::getModel('catalog/product')
                    ->load(YOUR_PRODUCT_ID);
 
Get children products (all associated children products data)

$childProducts = Mage::getModel('catalog/product_type_configurable')
                ->getUsedProducts(null,$product);

Magento Load product of specific store.

$storeId = Mage::app()->getStore()->getId();

$product = Mage::getModel('catalog/product')
            ->setStoreId($storeId)
            ->load($key);

Magento Load multiple products at once.

Following are load product useing ids.

$productIds = array(5, 22, 45, 75, 88);

$productIds = array(5, 22, 45, 75, 88);
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')
                ->getCollection()              
                ->addAttributeToFilter('entity_id', array('in' => $productIds))
                ->addAttributeToSelect($attributes);
               
Following are load product useing SKUs.

$productSku = array('1111', '1112', '1113', 'HTC Touch Diamond', 'microsoftnatural');
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')
                ->getCollection()              
                ->addAttributeToFilter('sku', array('in' => $productSku))
                ->addAttributeToSelect($attributes);
               

Hope it helps. Thanks.