Friday, January 27, 2012

Getting a Price Filter of category or Category Filters in magento programmatically

Hi,
If you need to call or display category filters by using category id in magneto use this code

this below code will display price Filter of category


$html='<div>';
$html .= '
<h4>Price</h4>
<ul class="price_grid">';
$html .= $this->priceHtmlnav($maincategoryId);
$html .='</ul>';

echo $html;



/*price block html
it took price filter and display in menu*/
public function priceHtmlnav($maincategoryId) {

$html='';
$layer = Mage::getModel('catalog/layer');
$category = Mage::getModel('catalog/category')->load($maincategoryId);
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
$attributes = $layer->getFilterableAttributes('price');

foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
$result = $this->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
foreach($result->getItems() as $option) {
$t=str_replace('<span class="price">', "", $option->getLabel());
$t=str_replace('</span>', "", $t);
$html .='<li><a href="'.$category->getUrl().'?price='.$option->getValue()
.'">'.$t.'</a></li>';

}
}
}

return $html;
}

Wednesday, January 11, 2012

Product Image Re-size with out frame or removing white space

Hi,
Need to remove the white border around your images?

when we re-size product image we will get white space in border, so we can remove that white space when product image is re-sized

use this code to re-size the product image with out white space

<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135)->keepFrame(FALSE); ?>


->constrainOnly(true) This will not resize an image that is smaller than the dimensions inside the resize() part.

->keepAspectRatio(true) This will not distort the height/width of the image.

->keepFrame(false) This will not put a white frame around your image.


<?php echo $this->helper('catalog/image')->init($_product, 'image')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(350, null) ?>


This would resize your images to a max 350 width and constrain the height. If your image is taller than it is wide, you will end up with a nicely resized vertical image.

Category Image Re-size

Hi,

We don't have any function to re-size the category image, i saw one post where it clearly mention how to do it

call this code where you need to display

$_category=Mage::getModel('catalog/category')->load($categoryId());
$_imgUrl = $_category->getResizedImage(70,70);

and extend category model to your local system or your custom module
XML

<global>
<models>
<catalog>
<rewrite>
<category>NameSpace_Modulename_Model_Category</category>
</rewrite>
</catalog>
</models>
</global>


in side model folder add this file Category.php and paste this code


<?php

class NameSpace_Modulename_Model_Category extends Mage_Catalog_Model_Category
{

public function getResizedImage($width, $height = null, $quality = 100) {

if (! $this->getImage ())
return false;

$imageUrl = Mage::getBaseDir ( 'media' ) . DS . "catalog" . DS . "category" . DS . $this->getImage ();
if (! is_file ( $imageUrl ))
return false;

$imageResized = Mage::getBaseDir ( 'media' ) . DS . "catalog" . DS . "product" . DS . "cache" . DS . "cat_resized" . DS . $this->getImage ();// Because clean Image cache function works in this folder only
if (! file_exists ( $imageResized ) && file_exists ( $imageUrl ) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) :
$imageObj = new Varien_Image ( $imageUrl );
$imageObj->constrainOnly ( true );
$imageObj->keepAspectRatio ( true );
$imageObj->keepFrame ( false );
$imageObj->quality ( $quality );
$imageObj->resize ( $width, $height );
$imageObj->save ( $imageResized );
endif;

if(file_exists($imageResized)){
return Mage::getBaseUrl ( 'media' ) ."/catalog/product/cache/cat_resized/" . $this->getImage ();
}else{
return $this->getImageUrl();
}

}

}

Display category images in top menu or multi- column top menu with images in magento

Hi,

I used Raptor Explodedmenu extension for multi- column top menu

but i need to display images of subcategory of particular category ex;- Brand etc so i customized the Raptor Explodedmenu extension and attached in this post

in admin side you have to mention name of the parent category which sub-category images has to display not name

check this link

http://www.magentocommerce.com/boards/viewthread/270613/

Thursday, January 5, 2012

Apply coupon discount on Original Price not on special price in magento programmatically

Hi,

In Magento Default coupon code will apply to special price if special price is there else it will apply to original price ,
but in some case we need to apply coupon code discount to original price and then apply to special price
EX:- Special price 1000
Original Price :- 2000
Coupon discount is 10%

Then in cart page it will show special price- discountamount( acc to special price) i't 1000-100 = 900

but we need special price- discountamount( acc to Original price) i'e 1000- 200 =800

for that we need to create a event

add this xml code in config.xml in side events tag i'e <events></events>




<!-- event for default coupon but discount will apply for MRP-->
            <salesrule_validator_process>
                <observers>
                    <new_dicount_coupon>
                        <type>singleton</type>
                        <class>Namespcae_Modulename_Model_Observer</class>
                        <method>newcoupondiscountcal</method>
                    </new_dicount_coupon>
                </observers>
            </salesrule_validator_process>



And created Observer.php inside your module/model if not exists else add only this function newcoupondiscountcal($observer) in side Observer class



class Namespcae_Modulename_Model_Observer
{


//event for default coupon but discount will apply for MRP
public function newcoupondiscountcal($observer)
{

$item=$observer['item'];
$rule=$observer['rule'];
$rulePercent = max(0, 100-$rule->getDiscountAmount());
//print_r($rule->getData()); exit;

if($rule->getSimpleAction()=='by_percent') {
$disPer=$rule->getDiscountAmount();
$product=Mage::getModel('catalog/product')->load($item->getProductId());
$ratdisc=($product->getPrice()*$disPer)/100;
$DiscountAmount=$ratdisc+$item->getDiscountAmount();
$BaseDiscountAmount=$ratdisc+$item->setBaseDiscountAmount();

$result = $observer['result'];
$result->setDiscountAmount($DiscountAmount);
$result->setBaseDiscountAmount($BaseDiscountAmount);

}
}


clear cache and apply the code

Tuesday, January 3, 2012

Creating or Calling deafult Soap or Webservice API in magento programmatically

Hi,

As you all know in magneto we can create web services by using soap in magento
Magento as set of default API, i am using one default api writing code please check it

First Create web service user and role in magneto back end (system->Web Service) and assign roles to user

Enable soap dll i'e extension=php_soap.dll from your php.ini file

Paste this code in file and place this file in root folder or as you wish and run this file through browser




<?php
$proxy = new SoapClient('http://yourdomine/index.php/api/soap/?wsdl');

$sessionId = $proxy->login('USERNAME', 'API_Key');  // you will get this when you create web service user in admin side
     
   $filters = array(
    'sku' => array('like'=>'test%')
);
 
$products = $proxy->call($sessionId, 'product.list', array($filters));
 
var_dump($products);

?>