Magento Coding Solutions

Adding new Attribute option in magento

<?php $arg_attribute = "attribute code";
$dataArry=array('M','L','SL','XXL');

$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
foreach($dataArry as $dataVal){
$option['attribute_id'] = $attr_id;
$option['value'][][0] = $dataValh;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
?>

get shipping address , Billing address and item details by orderid programmatic in magento

<?php $orderId=$this->getOrderId();
 $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
 $shipping_address_data = $order->getShippingAddress();
      $billing_address_data = $order->getBillingAddress();
 $country1 = Mage::getModel('directory/country')->loadByCode($shipping_address_data['country_id']);
 $ctryname1=$country1->getName();
 $country2 = Mage::getModel('directory/country')->loadByCode($billing_address_data['country_id']);
 $ctryname2=$country2->getName();

 /**Billing and Shipping Information start**/
 $shipbillhtml='';
 $shipbillhtml.='<table class=orderconftab>';
 $shipbillhtml.='<tr><td><h3>Shipping Address</h3></td><td><h3>Billing Address</h3></td></tr>';
 $shipbillhtml.='<tr><td>'.$shipping_address_data['firstname'].' '.$shipping_address_data['lastname'].'</td><td>'.$billing_address_data['firstname'].' '.$billing_address_data['lastname'].'</td></tr>';
 $shipbillhtml.='<tr><td>'.$shipping_address_data['street'].'</td><td>'.$billing_address_data['street'].'</td></tr>';
 $shipbillhtml.='<tr><td>'.$shipping_address_data['city'].', '.$shipping_address_data['region'].', '.$shipping_address_data['postcode'].'</td><td>'.$billing_address_data['city'].', '.$billing_address_data['region'].', '.$billing_address_data['postcode'].'</td></tr>';
 $shipbillhtml.='<tr><td>'.$ctryname1.'</td><td>'.$ctryname2.'</td></tr>';
 $shipbillhtml.='<tr><td>'.$shipping_address_data['telephone'].'</td><td>'.$billing_address_data['telephone'].'</td></tr>';
      $shipbillhtml.='</table>';
      echo $shipbillhtml;
 /**Billing and Shipping Information end**/

 /**Payment Method Start**/
 $payment_method = $order->getPayment()->getMethodInstance()->getTitle();
      echo '<h3 class=orderconfpm>Payment Method</h3>';
      echo '<p>'.$payment_method.'<p>';
 /**Payment Method end**/
 /**Ordered Item Start**/
      $ordered_items = $order->getAllItems();
      echo '<table class=orderconfdetail>
    <thead>
   <th>Product Name</th>
<th class="a-center">Price</th>
<th class="a-center">Qty</th>
<th class="a-center">Subtotal</th>
</thead>';
      foreach($ordered_items as $item) {
$itemsubtotal=Mage::helper('core')->currency($item->getPrice()*$item->getQtyToInvoice());
$productId=$item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$productMediaConfig = Mage::getModel('catalog/product_media_config');
$thumbnailUrl = $productMediaConfig->getMediaUrl($product->getThumbnail());
$productImage='<img src="'.$thumbnailUrl.'" height="50px" />';
echo '<tr>';
echo '<td>'.$productImage.$item->getName().'</td>';
echo '<td>'.Mage::helper('core')->currency($item->getPrice()).'</td>';
echo '<td>'.$item->getQtyToInvoice().'</td>';
echo '<td>'.$itemsubtotal.'</td>';
echo '</tr>';
}
echo '<tr><td colspan="3">Subtotal</td><td>'.Mage::helper('core')->currency($order->getSubtotal()).'</td></tr>';
echo '</table>';
echo '<h5 class="grand_total">Grand Total: '.Mage::helper('core')->currency($order->getBaseGrandTotal()).'</h5>';
/**Ordered Item end**/
 ?>

Get configurable product collection in magento

/* **
Get product Collection only configurable product
**
*/
                        $notAssocative=array();
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eg'=>'configurable'));
foreach($collectionConfigurable->getData() as $notAss){
array_push($notAssocative,$notAss['entity_id']);
}
print_r($notAssocative);

get simple produt collection not associative product

$collection = Mage::getResourceModel('catalog/product_collection')
//->addAttributeToFilter('type_id', array('eg'=>'configurable'))
->addAttributeToFilter('type_id', array('eg'=>'simple'))
->addAttributeToSelect('*'); //or just the attributes you need

$collection->getSelect()->joinLeft(array('link_table' => 'catalog_product_super_link'),
'link_table.product_id = e.entity_id',array('product_id')
);

$collection->getSelect()->where('link_table.product_id IS NULL');
$notAssocative=array();
foreach($collection->getData() as $notAss){
array_push($notAssocative,$notAss['entity_id']);
}

print_r($notAssocative);

listing filterable options in product listing page in magento programmatically

<?php /*Create filter dependencies*/
$_filters = Mage::getSingleton('Mage_Catalog_Block_Layer_State')->getActiveFilters();
foreach ($_filters as $_filter){
echo '<div class="filters">'.$this->stripTags($_filter->getLabel()).'  <a href="'.$_filter->getRemoveUrl().'" title="Remove This Item"> X </a> </div>';
} ?>
<?php
/* *Get catalog Max price nad min price*/
        if (!($currentCategory = Mage::registry('current_category'))) {
            $currentCategory = Mage::getModel('catalog/category')->load(Mage::app()->getStore()->getRootCategoryId());
        }
$max_price=Mage::getSingleton('catalog/layer')->setCurrentCategory($currentCategory)->getProductCollection()->getMaxPrice();
   $minPrice=Mage::getSingleton('catalog/layer')->setCurrentCategory($currentCategory)->getProductCollection()->getMinPrice();

/** show catalog filtering Customize */
$_category = $currentCategory;
$category_id= $_category->getId();

$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($category_id);
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();


$filter='';
$filterList='';
foreach($attributes as $attribute){
if($attribute->getAttributeCode() == 'price'){
$filterBlockName = 'catalog/layer_filter_price';
}elseif($attribute->getBackendType() == 'decimal'){
$filterBlockName = 'catalog/layer_filter_decimal';
}else{
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();

$count = count($result->getItems());
if($count > 0){
if($attribute->getAttributeCode() == 'price'){

$filterList.='<div class="col-sm-4 col-md-4">
<div class="accordian_menu">
<a href="javascript:void(0)" class="heading">'.$attribute->getFrontendLabel().'<strong class="arrow"></strong>
</a>
<div class="submenu">
<div class="cheqbutton">
<div class="checkbox_btn">
<div class="range_slider padding_15">          
<input id="range_02" />
<script>
jQuery(document).ready(function () {
jQuery("#range_02").ionRangeSlider({
type: "double",                                          
min: '.$minPrice.',
max: '.$max_price.',
from: 0,
to: '.$max_price.',
postfix: "€"
});
});
</script>                          
 </div>
</div>
</div>

</div>
</div>
</div>';

}elseif($attribute->getAttributeCode() == 'shoes_color'){

$filterList.='<div class="col-sm-4 col-md-4">
<div class="accordian_menu">
<a href="javascript:void(0)" class="heading">Couleur<strong class="arrow"></strong>
</a>
<div class="submenu">
<div class="cheqbutton">
<div class="checkbox_btn">
<div class="color_style padding_15">
<ul>';
foreach($result->getItems() as $option) {
$filterList.=' <li class="color_box"><a href="'.$this->urlEscape($option->getUrl()).'" style="background:url('.Mage::helper('attributeoptionimage')->getAttributeOptionImage($option->getValue()).')"></a></li>';
}
$filterList.='</ul>
 </div>
</div>
</div>
</div>
</div>
</div>';

}elseif($attribute->getAttributeCode() == 'color'){
$filterList.='<div class="col-sm-4 col-md-4">
<div class="accordian_menu">
<a href="javascript:void(0)" class="heading">Couleur<strong class="arrow"></strong>
</a>
<div class="submenu">
<div class="cheqbutton">
<div class="checkbox_btn">
<div class="color_style padding_15">
<ul>';
foreach($result->getItems() as $option) {
$filterList.=' <li class="color_box"><a href="'.$this->urlEscape($option->getUrl()).'" style="background:url('.Mage::helper('attributeoptionimage')->getAttributeOptionImage($option->getValue()).')"></a></li>';
}
$filterList.='</ul>
 </div>
</div>
</div>

</div>
</div>
</div>';


}elseif($attribute->getAttributeCode() == 'shoes_size'){

$filterList.='<div class="col-sm-4 col-md-4">
<div class="accordian_menu">
<a href="javascript:void(0)" class="heading">'.$attribute->getFrontendLabel().'
<strong class="arrow"></strong>
</a>
<div class="submenu">
<div class="cheqbutton">
<div class="checkbox_btn">
<div class="size_list padding_15">
<ul>';
foreach($result->getItems() as $option) {
$filterList.='<li><a class="sizes" href="'.$this->urlEscape($option->getUrl()).'">'.$option->getLabel().'</a></li>';
}
$filterList.='</ul>
</div>
</div>
</div>

</div>
</div>
</div>';

}elseif($attribute->getAttributeCode() == 'size'){


$filterList.='<div class="col-sm-4 col-md-4">
<div class="accordian_menu">
<a href="javascript:void(0)" class="heading">'.$attribute->getFrontendLabel().'
<strong class="arrow"></strong>
</a>
<div class="submenu">
<div class="cheqbutton">
<div class="checkbox_btn">
<div class="size_list padding_15">
<ul>';
foreach($result->getItems() as $option) {
$filterList.='<li><a class="sizes" href="'.$this->urlEscape($option->getUrl()).'">'.$option->getLabel().'</a></li>';
}
$filterList.='</ul>
</div>
</div>
</div>

</div>
</div>
</div>';


}elseif($attribute->getAttributeCode() == 'marque'){


$filterList.='<div class="col-sm-4 col-md-4">
<div class="accordian_menu">
<a href="javascript:void(0)" class="heading">Marques
<strong class="arrow"></strong>
</a>
<div class="submenu">
<div class="cheqbutton">
<div class="checkbox_btn">';

$l=0;
$k=0;
foreach($result->getItems() as $option) {
//print_r($option->getData());
$_itemUrl = "'".$this->urlEscape($option->getMSelected() ? $option->getRemoveUrl() : $option->getUrl())."'";
if($l<2){
$filterList.='<div>
<input id="checkbox'.$l.'" type="checkbox" name="checkbox" value="1" ><label for="checkbox'.$l.'" onClick="serchFilter('.$_itemUrl.')"><span></span>'.$option->getLabel().'</label>
</div>';
}else{
if($k==0){
$filterList.='<a href="javascript:void(0)" class="drop">+ de Marques
<strong class="arrow"></strong>
</a>
<ul class="submenu">'; }

$filterList.='<li><div>
<input id="checkbox'.$l.'" type="checkbox" name="checkbox" value="1" ><label for="checkbox'.$l.'" onClick="serchFilter('.$_itemUrl.')"><span></span>'.$option->getLabel().'</label>
</div></li>';

$k++;
}
$l++;
}

$filterList.='</ul></div>

</div>

</div>
</div>
</div>';


}else{


}
}



}
echo $filterList;
?>

get Product Media image by productId Magento

 <?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>  
<?php if($_images){?>          
<?php $i=0; foreach($_images as $_image)
print_r($_image);
if ($i++ < 5) { $i++; ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
<?php } ?>

getMedia Image By product Id in magento

  $_product = Mage::getModel('catalog/product')->load($product->getId());

  $product->getMediaGalleryImages();        // This returns nothing
  $_product->getMediaGalleryImages(); 
 
 
 
<?php
$model = Mage::getModel('catalog/product') //getting product model
$_product = $model->load($productid); //getting product object for particular 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
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url  
?>