Tag : magento

Magento – Categories Images

Get category image:


<?php
$_category = $this->getCurrentCategory();
$_imageUrl = $this->getImageUrl();
?>

Get category thumbnail:


<?php
$_category = $this->getCurrentCategory();
$_imageUrl = $this->getThumbnail();
?>

Magento get child products for a configurable product

Magento get products childs for a configurable product


// $currentProduct = $this->getProduct();
$configurable_products = Mage::getModel('catalog/product_type_configurable')->setProduct($currentProduct);
$products_collection = $configurable_products->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($products_collection as $_product){
echo $_product->getId() . ": " . $_product->getName();
}

Magento – Show Quantity Box in Products List

In file ‘list.phtml’ file change the <button> line near line 108 from this:


<?php if($_product->isSaleable()): ?>
<button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?<')">>span>
<?php echo $this->__('Add to Cart') ?></span></button>

with:


<form action="
<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId(); ?>">
<input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="
<?php echo $this->getMinimalQty($_product) ?>" />
<button class=form-button" onclick="productAddToCartForm_
<?php echo $_product->getId(); ?>.submit()"><span>
<?php echo $this->__('Add to Cart') ?></span></button>
</form>
<script type="text/javascript">
var productAddToCartForm_
<?php echo $_product->getId(); ?> = new VarienForm('product_addtocart_form_
<?php echo $_product->getId(); ?>');
productAddToCartForm_
<?php echo $_product->getId(); ?>.submit = function(){
if (this.validator.validate()) {
this.form.submit();
}
}.bind(productAddToCartForm_<?php echo $_product->getId(); ?>);
</script>

Read full article

Magento – include a static block into phtml file

Include a static block into a phtml file:


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

Magento – use singleton database connection

Magento database connection write mode:


$db = Mage::getSingleton('core/resource')->getConnection('core_write');

Magento database connection read mode:


$db = Mage::getSingleton('core/resource')->getConnection('core_read');

Query model


$result = $db->query("SELECT * FROM tablename");
if ($result)
{
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
// Use $row
}
}

Magento – save and get data from cache

Just a method to save and get data from magento cache


$mydataforcache = "My data for cache";
$cache_name = "maycachename";
$CACHEDATA = MAGE::app()->loadCache($cache_name);
if (isset($CACHEDATA) && ($CACHEDATA != ""))
{
// Use data
echo $CACHEDATA;
}
else
{
// Save data to cache
Mage::app()->saveCache($mydataforcache, $cache_name, array(), 10800);
}

Magento get customer group Id and group name

Get customer group Id


// Check if costomer is logged in
if(Mage::getSingleton('customer/session')->isLoggedIn())
{
// Get group Id
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
}

Get group name:


$group = Mage::getModel('customer/customer_group')->load($groupId);
$group->getName();

Magento get front controller name

Magento get front controller name:


Mage::app()->getFrontController()->getRequest()->getRouteName()

Magento get parameters

Get a parameter value:


$paramValue = $this->getRequest()->getParam($paramName);

Get all parameters and values:


$allParams = $this->getRequest()->getParams();