Basic Magento Functions to get Product Information


Load product by id

<?php

$ productid =1234;

$_product = Mage::getModel('catalog/product')->load(1234); //product object is loaded

$_product->getData();//get data of loaded product

?>

Load product by Attribute

<?php

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku','1234');?>//product object is loaded by attribute

$_product->getData();//get data of loaded product

?>

Get Product id by Product SKU

<?php

$sku = 1234;

$productid = Mage::getModel('catalog/product')->getIdBySku($sku);

?>

Get Parent Product from Child (Simple) Products

<?php

$ childId =1234;

For configurable products:

$parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($childId);

For bundle products:

$parent_ids = Mage::getModel('bundle/product_type')->getParentIdsByChild($childId);

?>

Get Configurable product's Child products

<?php

// input is $_product and result is iterating child products

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

?>

Get Configurable product's Children's (simple product) custom attributes

<?php

// input is $_product and result is iterating child products

$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);

$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

foreach($col as $simple_product){

var_dump($simple_product->getId());

}

?>

Call Static Block

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block-name')->toHtml(); ?>

Get methods of an object

First, use get_class to get the name of an object's class.
<?php $class_name = get_class($object); ?>
Then, pass that get_class_methods to get a list of all the callable methods on an object
<?php
$class_name = get_class($object);
$methods = get_class_methods($class_name);
foreach($methods as $method)
{
var_dump($method);
}
?>

Is product purchasable?
<?php if($_product->isSaleable()) { // do stuff } ?>

Get associated products

In /app/design/frontend/default/site/template/catalog/product/view/type/

<?php $_helper = $this->helper('catalog/output'); ?>

<?php $_associatedProducts = $this->getAllowProducts() ?>

<?php //var_dump($_associatedProducts); ?>

<?php if (count($_associatedProducts)): ?>

<?php foreach ($_associatedProducts as $_item): ?>

<a href="<?php echo $_item->getProductUrl() ?>"><?php echo $_helper->productAttribute($_item, $_item->getName(), 'name') ?> | <?php echo $_item-

>getName() ?> | <?php echo $_item->getPrice() ?></a>

<?php endforeach; ?>

<?php endif; ?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s