Tuesday, 2 December 2014

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.

Wednesday, 5 November 2014

Friday, 26 September 2014

Magento Category Accordian with Alphabatically sorting.

Add following code in any PHTML file and call in left/right side.

<div class="block block-left-nav" id="sidebox">
    <div class="block-title">
        <strong><span><?php echo $this->__('categories') ?></span></strong>
    </div>
    <div class="block-content">
        <ul id="nav-left">
            <?php
            $collection = Mage::getModel('catalog/category')->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToSelect('is_active')
                    ->addAttributeToSort('name', 'ASC');
            foreach ($collection as $_category1):
                if ($this->getRequest()->getModuleName() == 'vendor') {
                    $_category = Mage::getModel('catalog/category')->load($_category1['entity_id']);
                    ?>
                    <?php if ($_category->getIsActive() && $_category1['entity_id'] != 2 && $_category->getLevel() == 2) { ?>
                        <?php $potential = $_category->hasChildren(); ?>
                        <li><a id="block-title" class="<?php echo ($open) ? 'active' : 'inactive' ?>" href="<?php
                            if ($potential) {
                                echo $this->getCategoryUrl($_category);
                                //echo 'javascript:void(0)';
                            } else {
                                echo $this->getCategoryUrl($_category);
                            }
                            ?> "><?php echo $_category->getName(); ?></a>
                               <?php if ($potential) { ?>
                                <ul class="top-level">
                                    <?php
                                    $_cat_level_2 = array();
                                    foreach ($_category->getChildrenCategories() as $_category2) {
                                        $_cat_level_2[$_category2->getName()] = $_category2;
                                    }
                                    ksort($_cat_level_2);
                                    ?>
                                    <?php foreach ($_cat_level_2 as $subcategory): ?>
                                        <li class="second-category">
                                            <a  class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subcategory); ?>"><?php echo $subcategory->getName(); ?></a>
                                            <?php if ($potential) { ?>
                                                <ul class="sub-level">
                                                    <?php
                                                    $_cat_level_3 = array();
                                                    foreach ($subcategory->getChildrenCategories() as $_category3) {
                                                        $_cat_level_3[$_category3->getName()] = $_category3;
                                                    }
                                                    ksort($_cat_level_3);
                                                    ?>
                                                    <?php foreach ($_cat_level_3 as $subsubcategory): ?>
                                                        <li><a class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subsubcategory); ?>"><?php echo $subsubcategory->getName(); ?></a></li>
                                                    <?php endforeach; ?>
                                                </ul>
                                            <?php } ?>  
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                            <?php } ?>
                        </li>
                        <?php
                    }
                }
                else {
                    $_category = Mage::getModel('catalog/category')->load($_category1['entity_id']);
                    ?>
                    <?php if ($_category->getIsActive() && $_category1['entity_id'] != 2 && $_category->getLevel() == 2) { ?>
                        <?php
                        $open = $this->isCategoryActive($_category);
                        if (Mage::registry('current_category')->getid() == $_category1['entity_id']) {
                            $open = 'yes';
                        }
                        ?>
                        <?php $potential = $_category->hasChildren(); ?>
                        <li><a id="block-title" class="<?php echo ($open) ? 'active' : 'inactive' ?>" href="<?php
                            if ($open) {
                                echo $this->getCategoryUrl($_category);
                            } elseif ($potential) {
                                //echo 'javascript:void(0)';
                                echo $this->getCategoryUrl($_category);
                            } else {
                                echo $this->getCategoryUrl($_category);
                            }
                            ?> "><?php echo $_category->getName(); ?></a>
                               <?php if ($potential) { ?>
                                   <?php
                                   $_cat_level_2 = array();
                                   foreach ($_category->getChildrenCategories() as $_category2) {
                                       $_cat_level_2[$_category2->getName()] = $_category2;
                                   }
                                   ksort($_cat_level_2);
                                   ?>
                                <ul class="top-level">
                                    <?php foreach ($_cat_level_2 as $subcategory): ?>
                                        <?php
                                        $open = $this->isCategoryActive($subcategory);
                                        if (Mage::registry('current_category')->getid() == $subcategory->getId()) {
                                            $open = 'yes';
                                        }
                                        ?>
                    <?php $potential = $subcategory->hasChildren(); ?>
                                        <li class="second-category">
                                            <a  class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subcategory); ?>"><?php echo $subcategory->getName(); ?></a>
                                                <?php if ($potential) { ?>
                                                <ul class="sub-level">
                                                    <?php
                                                    $_cat_level_3 = array();
                                                    foreach ($subcategory->getChildrenCategories() as $_category3) {
                                                        $_cat_level_3[$_category3->getName()] = $_category3;
                                                    }
                                                    ksort($_cat_level_3);
                                                    ?>
                                                    <?php foreach ($_cat_level_3 as $subsubcategory): ?>
                                                        <?php
                                                        $open = $this->isCategoryActive($subsubcategory);
                                                        if (Mage::registry('current_category')->getid() == $subsubcategory->getId()) {
                                                            $open = 'yes';
                                                        }
                                                        ?>
                                                        <li><a class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subsubcategory); ?>"><?php echo $subsubcategory->getName(); ?></a></li>
                                                <?php endforeach; ?>
                                                </ul>
                                        <?php } ?>  
                                        </li>
                                <?php endforeach; ?>
                                </ul>
                        <?php } ?>
                        </li>
                    <?php }
                }
                ?>
<?php endforeach; ?>
        </ul>
    </div>
</div>