Home

Tuesday, August 22, 2017

How to get visitor country name,currency rate,currency symbol in PHP

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$dataArray = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
echo "<pre>";
print_r($dataArray);
echo "You are from: ".$dataArray->geoplugin_countryName;
?>

Output :
stdClass Object
(
    [geoplugin_request] => 27.109.6.218
    [geoplugin_status] => 206   
    [geoplugin_city] => 
    [geoplugin_region] => Jammu and Kashmīr
    [geoplugin_areaCode] => 0
    [geoplugin_dmaCode] => 0
    [geoplugin_countryCode] => IN
    [geoplugin_countryName] => India
    [geoplugin_continentCode] => AS
    [geoplugin_latitude] => 33.8042
    [geoplugin_longitude] => 74.2583
    [geoplugin_regionCode] => 12
    [geoplugin_regionName] => Jammu and Kashmīr
    [geoplugin_currencyCode] => INR
    [geoplugin_currencySymbol] => ₨
    [geoplugin_currencySymbol_UTF8] => ₨
    [geoplugin_currencyConverter] => 64.1175
)
You are from: India

Saturday, July 22, 2017

Base url without index.php in magento

<?php
            echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
?>

Saturday, July 8, 2017

How to Remove index.php in codeigniter ?

Step: 1  Open the file config.php

--> Find the below code in file
$config['index_page'] = "index.php"
--> Remove index.php
$config['index_page'] = ""

// Find the below code in file
$config['uri_protocol'] = "AUTO"
// Replace it as $config['uri_protocol'] = "REQUEST_URI"


Step: 2  Go to your CodeIgniter folder and create .htaccess  file and Write below code in .htaccess file

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>

Thursday, June 22, 2017

How to add images for products in magento pragmatically ?

Use can upload Base-Image, Small-Image, Thumbnail-Image, and Image-Gallery for the product using CSV file in Magento programmatically.
=====================================================================
<?php
require_once '../app/Mage.php';
set_time_limit(0);
ini_set('memory_limit','1024M');
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

// Images with SKU
$importDir = Mage::getBaseDir('media') . DS . 'import/diamond/shape/big/';
$file_handle = fopen("images5.csv", "r");
$c = 0;


while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 10000);

if($line_of_text[0] !="")
{
$productSKU = $line_of_text[0];
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
$lastproductSKU = $productSKU;

$basefileName = $line_of_text[1];
$smallfileName = $line_of_text[2];
$thumbfileName = $line_of_text[3];

$basefilePath = $importDir.$basefileName;
$smallfilePath = $importDir.$smallfileName;
$thumbfilePath = $importDir.$thumbfileName;

// Base Image
if (file_exists($basefilePath)) {
$ourProduct->addImageToMediaGallery($basefilePath, array('image'), false, false);
}

// Small Image
if (file_exists($smallfilePath)) {
$ourProduct->addImageToMediaGallery($smallfilePath, array('small_image'), false, false);
}

// thumbnail Image
if (file_exists($thumbfilePath)) {
$ourProduct->addImageToMediaGallery($thumbfilePath, array('thumbnail'), false, false);
}

$extrafileName = $line_of_text[4];
$extrafilePath = $importDir.$extrafileName;
if (file_exists($extrafilePath)) {
$ourProduct->addImageToMediaGallery($extrafilePath, null, false, false);
}
$ourProduct->save();
}else
{
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$lastproductSKU);
$extrafileName = $line_of_text[4];
$extrafilePath = $importDir.$extrafileName;
if (file_exists($extrafilePath)) {
$ourProduct->addImageToMediaGallery($extrafilePath, null, false, false);
$ourProduct->save();
}
}
$c++;
}
fclose($file_handle);
?>

Please check image for CSV format.


How to get all images of particular product in magento pragmatically ?

<?php
require_once '../app/Mage.php';
set_time_limit(1);
ini_set('memory_limit','1024M');
ini_set('max_execution_time', 3000);
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

// Get Product SKU using csv file
$file_handle = fopen("skus.csv", "r");
$c = 1;
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 10000);
$productSKU = $line_of_text[0];


if($line_of_text[0]!="")
{
$sku = substr($productSKU,0,-1);
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
if (false !== $id) {
  //sku exists
$model =  Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$productMediaConfig = Mage::getModel('catalog/product_media_config');

if($model->getImageUrl())
{
$baseImageUrl  = $productMediaConfig->getMediaUrl($model->getImage());
$smallImageUrl = $productMediaConfig->getMediaUrl($model->getSmallImage());
$thumbnailUrl  = $productMediaConfig->getMediaUrl($model->getThumbnail());

$basefileName = explode("/",$model->getImage());
$smallfileName = explode("/",$model->getSmallImage());
$thumbfileName = explode("/",$model->getThumbnail());

echo $c.")baseImageUrl ".$sku."==>".$baseImageUrl;
echo "<br>";
echo $c.")smallImageUrl ".$sku."==>".$smallImageUrl;
echo "<br>";
echo $c.")thumbnailUrl ".$sku."==>".$thumbnailUrl;


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

$items = array();

foreach($gallery_images as $g_image) {
$items[] = $g_image['url'];
}

echo "<pre>";
print_r($items);
echo "</pre>";
echo "<br>";
echo "<br>";echo "<br>";*/
}
echo "<br>";
$c++;
}
else {
  //sku does not exist
  echo $productSKU."==>sku does not exist";
  echo "<br>";
}
}
}
fclose($file_handle);
?>

Please check image for CSV format.


Monday, January 23, 2017

How to programmatically update short and long description using csv file for magento products ?

<?php
require_once '../app/Mage.php';
umask(0) ;
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$csv = new Varien_File_Csv();
$data = $csv->getData('sku_description.csv'); //path to csv
array_shift($data);

for($i=0;$i<count($data);$i++)
{
if($data[$i][0] != "")
{
$product_sku = $data[$i][0];
$short_description = $data[$i][1];
$long_description = $data[$i][2];

$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$product_sku);

if($product) {
$product->setShortDescription($short_description);
$product->setDescription($long_description);
$product->save();
echo "Updated product " . $productSku . "<br>";
}else
{
echo "Not - Updated product " . $productSku . "<br>";
}
}
}
?>

Please check image for CSV format.


Tuesday, January 10, 2017

How submit or insert data and files using JQuery Ajax ?

Step : 1 create Index.php file
----------------------------------------
<form id="data" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>FirstName</td>
<td><input type="text" name="firstName" value="" /></td>
</tr>
<tr>
<td>LastName</td>
<td><input type="text" name="lastName" value="" /></td>
</tr>
<tr>
<td>Attachment</td>
<td><input name="image" type="file" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
<div id="response"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$("form#data").submit(function(){
    var formData = new FormData($(this)[0]);
    $.ajax({
        url: "data.php",
        type: 'POST',
        data: formData,
        async: false,
        success: function (data) {
            //alert(data)
    $("#response").html(data);
        },
        cache: false,
        contentType: false,
        processData: false
    });
    return false;
});
</script>

Step : 2 create data.php file
----------------------------------------
<?php
echo "<pre>";
print_r($_FILES);
echo "<pre>";
print_r($_POST );
/// Put here your logic for insert and uploading code.
?>

Monday, January 2, 2017

How to change a product dropdown attribute to a multiselect in Magento using Database Query ?

UPDATE eav_attribute SET
entity_type_id = '10', // set here entity_type_id  from eav_attribute
attribute_model = NULL,
backend_model = 'eav/entity_attribute_backend_array',
backend_type = 'varchar',
backend_table = NULL,
frontend_model = NULL,
frontend_input = 'multiselect',
frontend_class = NULL
WHERE attribute_id = '1100'; // set here attribute_id  from eav_attribute


INSERT INTO catalog_product_entity_varchar ( entity_type_id, attribute_id, store_id, entity_id, value)
SELECT entity_type_id, attribute_id, store_id, entity_id, value
FROM catalog_product_entity_int
WHERE attribute_id = 1100; // set here attribute_id  from eav_attribute


DELETE FROM catalog_product_entity_int
WHERE entity_type_id = 10 and attribute_id = 1100; // set here attribute_id  from eav_attribute

How to add to cart configurable product in magento programmatically ?

<?php

include 'app/Mage.php';

Mage::app();

// Need for start the session

Mage::getSingleton('core/session', array('name' => 'frontend'));

try {

    $product_id = '389';

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

    $cart = Mage::getModel('checkout/cart');

    $cart->init();

    $params = array(

        'product' => $product_id,

        'super_attribute' => array(

            1102 => 351,              
        ),

        'qty' => 1,

    );

    $cart->addProduct($product, $params);

    $cart->save();

    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

    Mage::getSingleton('core/session')->addSuccess('Product added successfully');

    header('Location: ' . 'index.php/checkout/cart/');

} catch (Exception $e) {

    echo $e->getMessage();

}

?>

Saturday, December 24, 2016

How to set character limit for Product Name, Description, Short Description in Magento ?

<?php
$productName = $_helper->productAttribute($_product, $_product->getName(), 'name');
echo Mage::helper('core/string')->truncate($productName, $length = 50, $etc = '...', $remainder = '', $breakWords = true);
?>

Wednesday, September 28, 2016

How to get all configurable products in magento ?

$collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable');

foreach ($collection as $product)
{
     echo $product->getName();
}

Saturday, August 20, 2016

How to get Diamond Inventory from RapNet using API ?

<?php
$data = '{"request": {"header": {"username": "xxxxxxx","password": "xxxx"},
"body": {
"shapes": [],
"size_to": "",
"size_from": "",
"color_from": "",
"color_to": "",
"clarity_from": "",
"clarity_to": "",
"cut_from": "",
"cut_to": "",
"polish_from": "",
"polish_to": "",
"symmetry_from": "",
"symmetry_to": "",
"labs": [],
"price_total_from": "",
"price_total_to": "",
"page_number": "1",
"page_size": "20",
"sort_by": "price",
"sort_direction": "ASC"
}}}';
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_URL, 'http://technet.rapaport.com/HTTP/JSON/RetailFeed/GetDiamonds.aspx');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$data = json_decode($result,true);
$diamonds_returned = $data['response']['body']['search_results']['diamonds_returned'];
$total_diamonds_found =  $data['response']['body']['search_results']['total_diamonds_found'];
echo "<pre>";
print_r($data);
curl_close($curl);
?>

How to Change the price before adding to cart in magento with custom option selection price?

Put below code in app\code\community\YBizz\PriceChange\Model\Observer.php
<?php
class DRC_PriceChange_Model_Observer  {

    public function change_price(Varient_Event_Observer $observer) 
    {   
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {$item = $item->getParentItem();}
        $price = $item->getProduct()->getFinalPrice();
        $new_price = 20 + $price;

        $item->setCustomPrice($new_price);
        $item->setOriginalCustomPrice($new_price);
        $item->getProduct()->setIsSuperMode(true);      
    }
}
?>

Wednesday, August 3, 2016

How to get value of Attribute Option in magento ?

<?php
require_once '../app/Mage.php';
umask(0) ;
Mage::app();

function getAttributeOptionValue($arg_attribute, $arg_value)
{
$attribute_model = Mage::getModel('eav/entity_attribute');
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table');  
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
$attribute = $attribute_model->load($attribute_code);
$attribute_table = $attribute_options_model->setAttribute($attribute);
$options = $attribute_options_model->getAllOptions(false);  
foreach($options as $option)
{
if ($option['label'] == $arg_value)
{
return $option['value'];
}
}  
return false;
}
echo $optionValue = getAttributeOptionValue("diamond_symmetry", "Very Good");
?>

How to add new value in attribute magento ?

<?php
require_once 'app/Mage.php';
umask(0) ;
Mage::app();

$optionValue = addAttributeValue("diamond_symmetry", "Excellent");

function addAttributeValue($arg_attribute, $arg_value)
    {
        $attribute_model        = Mage::getModel('eav/entity_attribute');

        $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
        $attribute              = $attribute_model->load($attribute_code);

        if(!attributeValueExists($arg_attribute, $arg_value))
        {
            $value['option'] = array($arg_value,$arg_value);
            $result = array('value' => $value);
            $attribute->setData('option',$result);
            $attribute->save();
        }

$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
        $attribute_table        = $attribute_options_model->setAttribute($attribute);
        $options                = $attribute_options_model->getAllOptions(false);

        foreach($options as $option)
        {
            if ($option['label'] == $arg_value)
            {
                return $option['value'];
            }
        }
       return false;
    }

function attributeValueExists($arg_attribute, $arg_value)
    {
        $attribute_model        = Mage::getModel('eav/entity_attribute');
        $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;

        $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
        $attribute              = $attribute_model->load($attribute_code);

        $attribute_table        = $attribute_options_model->setAttribute($attribute);
        $options                = $attribute_options_model->getAllOptions(false);

        foreach($options as $option)
        {
            if ($option['label'] == $arg_value)
            {
                return $option['value'];
            }
        }

        return false;
    }
?>
rathoddhirendra.blogspot.com-Google pagerank and Worth