Tuesday 27 May 2014

Get cart details in Magento

You can get the cart details with the following code

<?php
$cart = Mage::getModel('checkout/cart')->getQuote(); //to get cart detail
foreach ($cart->getAllItems() as $item) {
   echo  $productId = $item->getProduct()->getId(); // product id in cart
   echo $productPrice = $item->getProduct()->getPrice(); // product price in cart
$product = Mage::getModel('catalog/product')->load($productId); // load product to get all details of product
//load the categories of this product
$categoryCollection = $product->getCategoryCollection();
$catIds = $product->getCategoryIds(); // to get all category ids of products in cart
$catCollection = Mage::getResourceModel('catalog/category_collection')  // to get all all details of categories of products in cart
                     //->addAttributeToSelect('name')
                     //->addAttributeToSelect('url')
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('entity_id', $catIds)
                     ->addIsActiveFilter();

foreach($catCollection as $cat){
 // print_r($cat->getData());
 $catname[] =  $cat->getName();
  //echo $cat->getUrl();
}

}
print_r($catname); //category names array
?>

Friday 23 May 2014

import product image from external url in magento 1.8

Hi in some project i need to import the csv of products in that i have he values of product images is like

http://www.example.com/image.jpg

i need to import that by default there is no mechanism in magento to import external image.

for doinmg this we have to edit the import function in below file

app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php

around line 754 you will see some code like this

  foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
             if (isset($importData[$mediaAttributeCode])) {
                 $file = trim($importData[$mediaAttributeCode]);
                 if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
                    $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
                 }
            }
         }


you simply need to replace this dunction with the below one 

foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
                        if (isset($importData[$mediaAttributeCode])) {
                            $file = trim($importData[$mediaAttributeCode]);
                            if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
                                // Start Of Code To Import Images From Urls
                                if (preg_match('%http?://[a-z0-9\-./]+\.(?:jpe?g|png|gif)%i', $file)) {
                                    $path_parts = pathinfo($file);
                                    $html_filename = DS . $path_parts['basename'];
                                    $fullpath = Mage::getBaseDir('media') . DS . 'import' . $html_filename;
                                    if(!file_exists($fullpath)) {
                                        file_put_contents($fullpath, file_get_contents($file));}
                                    $arrayToMassAdd[] = array('file' => trim($html_filename), 'mediaAttribute' => $mediaAttributeCode);
                                }
                                else
                                {
                                    $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
                                }
                                // End Of Code To Import Images From URLs
                            }
                        }
                        }

Hope this will help you. thanks





Tuesday 20 May 2014

Get current product data in custom file in magento

HI in some project i need to develop a module through which i need to insert a newly created product attribute  on product page with the modue xml file . For that i need to havethe current product data in my module file.

for that i have use the below function

$product_id = Mage::registry('current_product')->getId(); 
 
with this when your file will include in the porduct view page.it will load
 product id from your file.You can get any value from this.

Tuesday 13 May 2014

How to check url contains http or htttps in php

Hi with the following code you can get the protocol of the server  
 
 
 
if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  $protocol = 'https://';
}
else {
  $protocol = 'http://';
}