Archive for December, 2010

How to add Youtube video to product details page in magento

1. Add an attribute called video_embed_code
2. Add the attribute into your products’ attribute sets.
3. Fill in youtube embed codes into these attributes.
4. Where you want to render the videos, place the following code:


<div><code> <div class='youtube-vid'>
<?php if ($_product->getData('video_embed_code')): ?>
 <?php $youtubevid= $_product->getResource()->getAttribute('video_embed_code')->getFrontend()->getValue($_product); ?>
 <?php echo $youtubevid; ?>
<?php endif; ?>
</div> </code></div>
<div>
now enjoy magento

for more info visit mage-people

Advertisements

How to get Product attibute name of a specific product in magento

In my running project I faced a problem to print product attribute value for a specific product with product name and SKU. lastly it is solved here is solution so i liked to share it.


<h5><?php echo $this->htmlEscape($_item->getName()) ?></h5> // for product name
 <div><strong><?php echo $this->helper('sales')->__('SKU') ?>:</strong> <?php echo implode('<br />', Mage::helper('catalog')->splitSku($this->htmlEscape($this->getSku()))); ?></div> // for SKU
<h5>

<?php

$_prod = Mage::getModel('catalog/product')->load($_item->getProductId());

?>

<?php
//echo $_prod->getDropshipper(); // for printing attribute ID
?>

<h5>Drop Shipper : <?php echo $_prod->getAttributeText('dropshipper')?>  </h5> // for pribting attribute value's name

so enjoy magento

for more magento tutorial visit: http://blog.free-extension.com/

Adding Custom shipping method in Magento

On my going Project a client requirement was adding custom shipping method so I was trying to solve the problem at last got a solution and it so i liked to share it hope it will be helpful for you.

In this article I will Discuss how to write custom shipping method in Magento commerce. First we need to write our shipping method, it’s a class in folder app/code/core/Mage/Shipping/Model/Carrier/.
I called it Inchoocustom.php and it’s based on Flat rate shipping method but you can developed your own class

< ?php

class Mage_Shipping_Model_Carrier_Inchoocustom
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{

    protected $_code = 'inchoocustom';

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $freeBoxes = 0;
        if ($request->getAllItems()) {
            foreach ($request->getAllItems() as $item) {
                if ($item->getFreeShipping() && !$item->getProduct()->isVirtual()) {
                    $freeBoxes+=$item->getQty();
                }
            }
        }
        $this->setFreeBoxes($freeBoxes);

        $result = Mage::getModel('shipping/rate_result');
        if ($this->getConfigData('type') == 'O') { // per order
            $shippingPrice = $this->getConfigData('price');
        } elseif ($this->getConfigData('type') == 'I') { // per item
            $shippingPrice = ($request->getPackageQty() * $this->getConfigData('price')) - ($this->getFreeBoxes() * $this->getConfigData('price'));
        } else {
            $shippingPrice = false;
        }

        $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);

        if ($shippingPrice !== false) {
            $method = Mage::getModel('shipping/rate_result_method');

            $method->setCarrier('inchoocustom');
            $method->setCarrierTitle($this->getConfigData('title'));

            $method->setMethod('inchoocustom');
            $method->setMethodTitle($this->getConfigData('name'));

            if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
                $shippingPrice = '0.00';
            }

            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);

            $result->append($method);
        }

        return $result;
    }

    public function getAllowedMethods()
    {
        return array('inchoocustom'=>$this->getConfigData('name'));
    }

}
?>

Pay attention on following lines:


$method->setCarrier('inchoocustom');
$method->setCarrierTitle($this->getConfigData('title'));

$method->setMethod('inchoocustom');
$method->setMethodTitle($this->getConfigData('name'));

They determine how we will set up our method. Keyword is ‘inchoocustom’. In folder app/code/core/Mage/Shipping/etc/ we need to edit
system.xml and config.xml in order to enable our new shipping method.

system.xml:

<inchoocustom translate="label">
    <label>Inchoo Custom Shipping Method</label>
    <frontend_type>text</frontend_type>
    <sort_order>2</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <fields>
        <active translate="label">
            <label>Enabled</label>
            <frontend_type>select</frontend_type>
            <source_model>adminhtml/system_config_source_yesno</source_model>
            <sort_order>1</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </active>
        <name translate="label">
            <label>Method name</label>
            <frontend_type>text</frontend_type>
            <sort_order>3</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </name>
        <price translate="label">
            <label>Price</label>
            <frontend_type>text</frontend_type>
            <sort_order>5</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </price>
        <handling_type translate="label">
            <label>Calculate Handling Fee</label>
            <frontend_type>select</frontend_type>
            <source_model>shipping/source_handlingType</source_model>
            <sort_order>7</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </handling_type>
        <handling_fee translate="label">
            <label>Handling Fee</label>
            <frontend_type>text</frontend_type>
            <sort_order>8</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </handling_fee>
        <sort_order translate="label">
            <label>Sort order</label>
            <frontend_type>text</frontend_type>
            </sort_order><sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>

        <title translate="label">
            <label>Title</label>
            <frontend_type>text</frontend_type>
            <sort_order>2</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </title>
        <type translate="label">
            <label>Type</label>
            <frontend_type>select</frontend_type>
            <source_model>adminhtml/system_config_source_shipping_flatrate</source_model>
            <sort_order>4</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </type>
        <sallowspecific translate="label">
            <label>Ship to applicable countries</label>
            <frontend_type>select</frontend_type>
            <sort_order>90</sort_order>
            <frontend_class>shipping-applicable-country</frontend_class>
            <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </sallowspecific>
        <specificcountry translate="label">
            <label>Ship to Specific countries</label>
            <frontend_type>multiselect</frontend_type>
            <sort_order>91</sort_order>
            <source_model>adminhtml/system_config_source_country</source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </specificcountry>
        <showmethod translate="label">
            <label>Show method if not applicable</label>
            <frontend_type>select</frontend_type>
            <sort_order>92</sort_order>
            <source_model>adminhtml/system_config_source_yesno</source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </showmethod>
        <specificerrmsg translate="label">
            <label>Displayed Error Message</label>
            <frontend_type>textarea</frontend_type>
            <sort_order>80</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </specificerrmsg>
    </fields>
</inchoocustom>

config.xml:

 



<pre><inchoocustom>
               <active>0</active>
               <sallowspecific>0</sallowspecific>
               <model>shipping/carrier_inchoocustom</model>
               <name>Inchoo Custom Shipping Method</name>
               <price>5.00</price>
               <title>Inchoo Custom Shipping Method</title>
               <type>I</type>
               <specificerrmsg>This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.</specificerrmsg>
               <handling_type>F</handling_type>
       </inchoocustom>

Now we need to enable our new shipping method in admin panel (System->Configuration->Shipping Methods) And we have our custom shipping method. Enjoy coding.

 

for more info visit mage-people

How to display featured product in magento


<div>

<?php

 $featuredproducts = array();
 $i = 0;
 try {
 $storeId = Mage::app()->getStore()->getId();
 $resource = Mage::getSingleton('core/resource');
 $read = $resource->getConnection('catalog_read');
 $categoryProductTable = $resource->getTableName('catalog/category_product');
 $productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
 $eavAttributeTable = $resource->getTableName('eav/attribute');

 $select = $read->select()
 ->from(array('cp'=>$categoryProductTable))
 ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
 ->joinNatural(array('ea'=>$eavAttributeTable))
 ->where('pei.value=1')
 ->where('ea.attribute_code="fooger_featured_product"'); //this is attribute code

 $featuredproducts = $read->fetchAll($select);
 } catch (Exception $e) {
 // Blank
 }
 if(count($featuredproducts)) :
 foreach($featuredproducts as $data) :
 $prodInfo = Mage::getModel('catalog/product')->setStoreId($storeId)->load($data['product_id']);
?>
 <div>
 <div>
 <a href="<?php echo $baseUrl.$prodInfo->url_path ?>" title="View <?php echo $prodInfo->name ?>" target="_blank">
 <img height="114" width="140" src="<?php echo $this->helper('catalog/image')->init($prodInfo, 'image');//->resize(82); ?>" />
 </a>
 </div>
 </div>
 <?php
 $i++;
 if($i == 3) return; // Total 3 feature products only
 ?>
 <?php endforeach ?>
 <?php else :?>
 <div>No feature products</div>
 <?php endif ?>
 </div>

&nbsp;

for more magento tutorial visit: http://blog.free-extension.com/

How to insert CSV data to mysql database using php


<?php

/********************************/
/* Database configuration
/********************************/
$databasehost = "localhost";
$databasename = "your database name";
$databasetable = "database table name";
$databaseusername ="username";
$databasepassword = "password";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "customers.csv"; //CSV file that you want to insert
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "csvinput.sql";
/********************************/


if(!file_exists($csvfile)) {
 echo "File not found. Make sure you specified the correct path.\n";
 exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
 echo "Error opening data file.\n";
 exit;
}

$size = filesize($csvfile);

if(!$size) {
 echo "File is empty.\n";
 exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());

$lines = 0;
$queries = "";
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {

 $lines++;

 $line = trim($line," \t");

 $line = str_replace("\r","",$line);

 /************************************
 This line escapes the special character. remove it if entries are already escaped in the csv file
 ************************************/
 $line = str_replace("'","\'",$line);
 /*************************************/

 $linearray = explode($fieldseparator,$line);

 $linemysql = implode("','",$linearray);

 if($addauto)
 $query = "insert into $databasetable values('','$linemysql');";
 else
 $query = "insert into $databasetable values('$linemysql');";

 $queries .= $query . "\n";

 @mysql_query($query);
}

@mysql_close($con);

if($save) {

 if(!is_writable($outputfile)) {
 echo "File is not writable, check permissions.\n";
 }

 else {
 $file2 = fopen($outputfile,"w");

 if(!$file2) {
 echo "Error writing to the output file.\n";
 }
 else {
 fwrite($file2,$queries);
 fclose($file2);
 }
 }

}

echo "Found a total of $lines records in this csv file.\n";


?>

for more magento tutorial visit: http://blog.free-extension.com/

Finding the Online Customer in magento

You can easily find out number of online customer by the code  in the content section

 

<?php $collection = Mage::getResourceSingleton(‘log/visitor_collection’)
 ->useOnlineFilter();
 echo “Online Customer “.count($collection);
 ?>

for more magento tutorial visit: http://blog.free-extension.com/

How to get Total Cart Item and total Price in magento

<?php
  $count = $this->helper('checkout/cart')->getSummaryCount();  //get total items in cart
  $total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price
  if($count==0)
  {
    echo $this->__('Items: %s',$count);
  }
  if($count==1)
  {
    echo $this->__(' Item: %s',$count);
  }
  if($count>1)
  {
    echo $this->__(' Items: %s',$count);
  }
  echo $this->__(' Total: %s', $this->helper('core')->formatPrice($total, false));

for more magento tutorial visit: <a title="http://blog.free-extension.com" href="http://blog.free-extension.com">http://blog.free-extension.com/</a>
?>

for more magento tutorial visit: http://blog.free-extension.com/