PHP SOLUTIONS

This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Saturday 2 June 2012

Magento : Get Product Qty



  $connection = Mage::getSingleton('core/resource')
->getConnection('core_write');
$connection->beginTransaction();
$fields = array();
$fields['name']= 'test';
$fields['description']= 'test';
$connection->insert('tablename', $fields);
$connection->commit();

Magento : Get category details form product id



$children = Mage::getModel('catalog/category')->getCategories(""ProductId"");
foreach ($children as $category)
{
    echo $category->getName();
}

Magento : Create a custom page in magento library


<!---- First Create One Phtml File  ----->

==> Go to app/design/forntend/default/base/tamplet/catalog/product
==> Here Copy any phtml file,
==> Paste that copied file,
==> Change the name with your Name
==> Example ::=> categoryid.phtml

<!---- Second Create One Php File  ----->

==> Go to app/code/core/mage/catalog/block/product
==> Here Copy PHP file Which name is as your phtml file which you select and copy-paste,
==> Paste that PHP copied file,
==> Change the name with Same name of PHtml file and Must "First character be a Capital"
==> Example ::=> Categoryid.php


<!---- Third Add this code in Product controller File  ----->

==> Go to app/code/core/mage/catalog/controller
==> Put belove code in this file

public function "Here your phtml file name in small letter"Action()
{

$this->loadLayout();
        $this->renderLayout();
}

==> Example ::=>

public function categoryidAction()
{

$this->loadLayout();
        $this->renderLayout();
}


<!---- Fourth Add this code in Catalog.xml File  ----->

==> Go to app/design/forntend/default/base/layout/catalog.xml
==> Put belove code in this file

<catalog_product_Your Phtml file Name>
  <reference name="content">
<block name="Your Phtml file Name" template="catalog/product/Your Phtml file Name.phtml" type="catalog/product_Your Phtml file Name" as="Your Phtml file Name"/>
  </reference>
</catalog_product_Your Phtml file Name>

==> Example ::=>

<catalog_product_categoryid>
  <reference name="content">
<block name="category" template="catalog/product/categoryid.phtml" type="catalog/product_categoryid" as="categoryid"/>
  </reference>
</catalog_product_categoryid>

Magento : Get CMS Static Block at Front-End


echo $this->getLayout()->createBlock('cms/block')->setBlockId(''Here Identifire Name'')->toHtml();

Magento : Get parent category and sub category (Id,Name)


<?php

$_helper = Mage::helper('catalog/category')
$_categories = $_helper->getStoreCategories()
$currentCategory = Mage::registry('current_category')
if (count($_categories) > 0):
 
        foreach($_categories as $_category):

echo $_category->getId();
echo $_category->getName();
           
        $_category = Mage::getModel('catalog/category')->load($_category->getId())
                $_subcategories = $_category->getChildrenCategories()
                if (count($_subcategories) > 0):
               
                foreach($_subcategories as $_subcategory):
                 
echo $_subcategory->getId();
echo $_subcategory->getName();
                               
foreach (Mage::getModel('catalog/category')->load(  $_subcategory->getId())->getChildrenCategories() as $childCategory)

        {
echo $childCategory->getId();
echo $childCategory->getName();    
        }
                           
                        endforeach;
                 
     endif;

        endforeach;

endif; ?>

Friday 1 June 2012

Magento : Get Current URL


   $currenturl = $this->helper('core/url')->getCurrentUrl();

Magento : Get product attribute value for Drop-Down


     $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', """"Attribute Name """");

     foreach ( $attribute->getSource()->getAllOptions(true, true) as $option ):

echo   $option['value'] ;
echo  $option['label'];

    endforeach;

Magento : Get product attribute value for Text-Field


    echo $_product->getAttributeName(""""Attribute Name """");

         or

   echo $_product->getResource()->getAttribute(""""Attribute Name """")->getFrontend()->getValue($_product);

Magento : Get product details



$model = Mage::getModel('catalog/product'); //getting product model

$_product = $model->load($pro_id); //getting product object for particular product id



echo $product = $_product->getid();  //Get 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

echo $_product->getImageUrl(); //product's image url

Magento : Using resource model get all details of product

     $collection = Mage::getResourceModel('catalog/product_collection');

     foreach ($collection as $_product):

    $pro = $_product->getId();


                $model = Mage::getModel('catalog/product'); //getting product model
$_product1 = $model->load($pro);


    $_product1->getName();
    $_product1->getDescription();

    endforeach;




Magento : Check status of product is Enable / Disable


       $product = Mage::getModel('catalog/product')->load(""""Here product ID"""");

      echo $product->getStatus();

      //1 = Enabled
      //2 = Disabled

Magento : Check availability of product is In-Stock / Out-of-Stock

     $product = Mage::getModel('catalog/product')->load(""""Here product ID"""");

     $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct(""""Here product ID"""");

    if(($product->isSaleable())  &&  ($stock->getQty() > 0) )

   {
echo "In Stock";
    }

   else
   {
echo "Out of Stock";
    }

Magento : Get product Qty details


$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

You can check stock data in this way:-

1)  echo "<pre>"; print_r($stock->getData()); echo "</pre>";

      Or,
2)
   you can print individually like this:-

   echo $stock->getQty();
   echo $stock->getMinQty();
   echo $stock->getMinSaleQty();