Monday, 2 November 2015

Getting Configurable Product from Simple Product ID in Magento

$simpleProductId = 465;
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
                  ->getParentIdsByChild($simpleProductId);

$product = Mage::getModel('catalog/product')->load($parentIds[0]);
echo $product->getId();

Monday, 26 October 2015

Display associated posts on a Magento Product page

<?php echo $this->getLayout()
        ->createBlock('wordpress/post_associated')
            ->setTemplate("wordpress/post/associated.phtml")
            ->setTitle('Related Blog Posts')
            ->setEntity('product')
            ->setCount(5)
            ->toHtml() ?>
 Display associated products on a blog post page

                        <?php echo $this->getLayout()
    ->createBlock('wordpress/post_associated_products')
        ->setTemplate('wordpress/post/associated/products.phtml')
        ->setTitle('Related Products')
        ->toHtml() ?>

Monday, 19 October 2015

How to remove white space around the product image in Magento

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->keepFrame(false)->resize(270,396); ?>" width="270" height="396"  />

Thursday, 15 October 2015

Magento Log file created custom page

                $customererror='';               
                try {
                            $address->save();
                            $customer->save();
                        }                           
                         catch (Exception $ex) {
                            $customererror=$ex->getMessage();
                           
                            Mage::log($customererror,null,'customer.log');   
                        }

Monday, 12 October 2015

Reading a file line by line in php

<?php
$handle = fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>

Monday, 5 October 2015

Facebook image sharing

<meta property="og:title" content="<?php echo $Brand?>"/>
<meta property="og:image" content="<?php echo $imgurl.'?ts='.time();?>"/>
<meta property="og:image:secure_url" content="<?php echo $imgurl.'?ts='.time();?>"/>
<meta property="og:url" content="<?php echo $curUrl?>" />
<meta property="og:description" content="<?php echo $ProName?>"/>

================OR========================
<?php $product = Mage::registry('current_product');
if ($product): ?>
<meta property="og:title" content="<?php echo $product->getName(); ?>" />
<meta property="og:type" content="product" />
<meta property="og:url" content="<?php echo $this->helper('catalog/product')->getProductUrl($product); ?>" />
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(300, 300); ?>" />
<meta property="og:description" content="<?php echo strip_tags($product->getShortDescription()); ?>" />
<meta property="og:site_name" content="<?php echo Mage::app()->getStore()->getName() ?>" />
<?php endif; ?>
================OR=========================
<?php /* Open Graph Protocol for Facebook and SEO START */ ?>
<?php if(Mage::registry('current_product')): ?>
 <?php $product = Mage::registry('current_product'); ?>
 <meta property="og:title" content="<?php echo ($product->getName()); ?>" />
 <meta property="og:type" content="product" />
 <meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(200,200);?>" />
 <meta property="og:url" content="<?php echo Mage::registry('product')->getProductUrl(); ?>" />
 <meta property="og:description" content="<?php echo strip_tags(($product->getShortDescription())); ?>" />
 <meta property="og:site_name" content="<?php echo Mage::app()->getStore()->getName(); ?>" />
<?php elseif(Mage::registry('current_category')): ?>
 <meta property="og:title" content="<?php echo $this->getTitle() ?>" />
 <meta property="og:type" content="product.group" />
 <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
 <meta property="og:description" content="<?php echo strip_tags($this->getDescription()) ?>" />
 <meta property="og:site_name" content="<?php echo Mage::app()->getStore()->getName(); ?>" />
<?php elseif((Mage::getSingleton('cms/page')->getIdentifier() == 'home' &&
 Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')) : ?>
 <meta property="og:title" content="<?php echo $this->getTitle() ?>" />
 <meta property="og:type" content="website" />
 <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
 <meta property="og:description" content="<?php echo strip_tags($this->getDescription()) ?>" />
 <meta property="og:site_name" content="<?php echo Mage::app()->getStore()->getName(); ?>" />
<?php else: ?>
 <meta property="og:title" content="<?php echo $this->getTitle() ?>" />
 <meta property="og:type" content="article" />
 <meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
 <meta property="og:description" content="<?php echo strip_tags($this->getDescription()) ?>" />
 <meta property="og:site_name" content="<?php echo Mage::app()->getStore()->getName(); ?>" />
<?php endif; ?>
<?php /* Open Graph Protocol for Facebook and SEO END */ ?>

Friday, 18 September 2015

Configurable product add to cart on custom page in magento


==============================Price============

 Price section :- <span class="con_price">
                <?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(); ?>
                </span>

=============================================
 <?php /*********Configurable product add to cart **********/ ?>
                               <?php
            $productAttributeOptions = $_product->getTypeInstance(true)
            ->getConfigurableAttributesAsArray($_product);
            $super_attributedcode='';
            $super_attributedfrontend_label='';
            $super_a_id='';
            foreach($productAttributeOptions as $supperattributed){
                $super_attributedcode=$supperattributed['attribute_code'];
                $super_a_id=$supperattributed['attribute_id'];
                $super_attributedfrontend_label=$supperattributed['frontend_label'];
            }
           
            $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
            $childProducts = $conf->getUsedProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', array('eq' => 1))
            ->addFilterByRequiredOptions();
            ?>
           
            <span class="attributed_label"><?php echo $super_attributedfrontend_label; ?>: </span>
            <select class="con_productselectoption" name="con_productselectoption" id="con_product__<?php echo $_product->getId(); ?>">
            <?php
            foreach($childProducts as $_simproduct)
            {
                $sim_productid=$_simproduct->getId();
                $_sproduct= Mage::getModel('catalog/product')->load($sim_productid);
                $childPrice =  number_format($_sproduct->getPrice(),2);
                $childRetailPrice = number_format($_sproduct->getRetailPrice(),2);
                $childFinalPrice = number_format($_sproduct->getFinalPrice(),2);
               
                $attr = $_sproduct->getResource()->getAttribute($super_attributedcode);
                $option_id = $attr->getSource()->getOptionId($_sproduct->getAttributeText($super_attributedcode));
               
                $params = array(
               
                'product'=> $_product->getId(),
                Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => Mage::helper('core/url')->getEncodedUrl(),
                'form_key'=>Mage::getSingleton('core/session')->getFormKey()
                );
               
                $addToCartUrl= $this->getUrl("checkout/cart/add/", $params ).'?super_attribute['.$super_a_id.']='.$option_id;
               
            ?>
                 <option value="<?php echo $option_id; ?>" data-formatedprice="<?php echo Mage::helper('core')->currency($childFinalPrice, true, false); ?>" data-price="<?php echo $childFinalPrice; ?>"  data-carturl="<?php echo $addToCartUrl ?>"  ><?php echo $_sproduct->getAttributeText($super_attributedcode); ?></option>
                <?php
            }
           
            ?>
            </select>
           
            <a href="#" class="cls-add-to-cart" ><span><span><?php echo $this->__("Add To cart") ?></span></span></a>

================JQury=========================


    jQuery(document).ready(function(){
        var superattributedid='';
        var con_productid='';
        var option_price='';
   
    jQuery(".con_productselectoption").each(function(){
       
        option_price=jQuery(this).find("option:selected").data("formatedprice");
        jQuery(this).closest("li").find(".con_price").html(option_price);
        jQuery(this).closest("li").find(".cls-add-to-cart").attr("href",jQuery(this).find("option:selected").data("carturl"));
       
    });
   
    jQuery(".con_productselectoption").change(function(){
        option_price=jQuery(this).find("option:selected").data("formatedprice");
        jQuery(this).closest("li").find(".con_price").html(option_price);
        jQuery(this).closest("li").find(".cls-add-to-cart").attr("href",jQuery(this).find("option:selected").data("carturl"));
       
    });
    });

======================End=================