Home

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;
    }
?>

Thursday, June 11, 2015

jQuery Window Width Determined Initially and When Resized

jQuery(window).ready(function() {
    var wi = jQuery(window).width(); 
    jQuery("p.testp").text('Initial screen width is currently: ' + wi + 'px.');

    jQuery(window).resize(function() {
        var wi = jQuery(window).width();

        if (wi <= 480){
            jQuery("p.testp").text('Screen width is less than or equal to 480px. Width is currently: ' + wi + 'px.');           
            }
        else if (wi <= 767){
            jQuery("p.testp").text('Screen width is between 481px and 767px. Width is currently: ' + wi + 'px.');           
            }
        else if (wi <= 980){
            jQuery("p.testp").text('Screen width is between 768px and 980px. Width is currently: ' + wi + 'px.');           
            }
        else if (wi <= 1200){
            jQuery("p.testp").text('Screen width is between 981px and 1199px. Width is currently: ' + wi + 'px.');           
            }
        else {
            jQuery("p.testp").text('Screen width is greater than 1200px. Width is currently: ' + wi + 'px.');
//        jQuery().addClass();
            }
    });           
});

<p class="testp"></p>

Reference By :  http://www.surfingsuccess.com/jquery/window-width.html#.VXk5YPnPxNM

Friday, January 23, 2015

How to add product in woocommerce using programmatically with wpml

function woocommerce_add_products_dynamicaly()
 {
        //empty cart
        global $woocommerce;
        $woocommerce->cart->empty_cart();
        // Remove default cart message
        $woocommerce->clear_messages();
       
        //price
        $invoiceprice = 1;
       
        //Generate title
        $timestampedtitle = rand()." Date: ".date("d/m/Y")." Amount: £".$invoiceprice;
       
        //Generate message
        $message = date_timestamp_get(date_create())." Date: ".date('m/d/Y h:i:s a', time())." Invoice amount: £".$invoiceprice;

        $post = array(
        'post_author' => '2',
        'post_status' => "publish",
        'post_title' => $timestampedtitle,
        'post_content' => $message,
        'post_parent' => '',
        'post_type' => "product",
        //'post_status' => 'private',
        );

Monday, October 6, 2014

Change and Update WordPress URLS in Database When Site is Moved to new Host

 
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

Tuesday, September 16, 2014

Anyone know how to install pdftk for CentOS?

Reference from http://wiki.centos.org/HowTos/SetupRpmBuildEnvironment
 
# yum install rpm-build
sudo yum install rpm-build
sudo yum install redhat-rpm-config
sudo yum install make
yum install gcc
 
Reference from https://www.pdflabs.com/docs/install-pdftk-on-redhat-or-centos/
 
sudo yum install gcc gcc-java libgcj libgcj-devel gcc-c++ 
wget https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-src.zip 
unzip pdftk-2.02-src.zip 
cd pdftk-2.02-dist/pdftk 
make -f Makefile.Redhat 
sudo make -f Makefile.Redhat install
Upon completion, you will find pdftk in /usr/local/bin.


Reference from http://pkgs.org/centos-6/repoforge-x86_64/pdftk-1.44-1.el6.rf.x86_64.rpm.html

wget apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/pdftk-1.44-1.el6.rf.x86_64.rpm
rpm -Uvh pdftk-1.44-1.el6.rf.x86_64.rpm OR rpm -Uvh *rpm
yum install pdftk 




How to get PDF boxes information using PHP

If you want to extract Total no of pages,Creator,Creation Date,Producer,Tagged,Page size,MediaBox,CropBox,BleedBox,TrimBox,ArtBox,File size,PDF version etc then you can used below code.

---------------------------------------------------------------------------------------------------------------------
$file_name = "files/client_bussinesscard_front.pdf";
echo "<b>All Details : </b>";
$command_pagesize8 = "pdfinfo -box  $file_name";
shell_exec($command_pagesize8); //run the command
echo "<br><br><hr>";

echo "<b>Total Pages : </b>";
$command_pagesize1 = "pdfinfo -box  $file_name | grep Pages";
shell_exec($command_pagesize1); //run the command
echo "<br><br><hr>";

echo "<b>Page size : </b>";
$command_pagesize2 = "pdfinfo -box  $file_name | grep 'Page size'";
shell_exec($command_pagesize2); //run the command
echo "<br><br><hr>";

$command_pagesize3 = "pdfinfo -box  $file_name | grep 'MediaBox'";
mm($command_pagesize3,"<b>MediaBox Details (wight x height):</b><br>");

$command_pagesize4 = "pdfinfo -box  $file_name | grep 'CropBox'";
mm($command_pagesize4,"<b>CropBox Details (wight x height):</b><br>");

$command_pagesize5 = "pdfinfo -box  $file_name | grep 'BleedBox'";
mm($command_pagesize5,"<b>BleedBox Details (wight x height):</b><br>");

$command_pagesize6 = "pdfinfo -box  $file_name | grep 'TrimBox'";
mm($command_pagesize6,"<b>TrimBox Details (wight x height):</b><br>");

$command_pagesize7 = "pdfinfo -box  $file_name | grep 'ArtBox'";
mm($command_pagesize7,"<b>ArtBox Details (wight x height):</b><br>");

function mm($command,$title)
{
    echo $title;   
    $array = explode(" ",shell_exec($command));   
    foreach($array as $val)
    {
      if($val!="")
      {
          $final_array[].=$val;
      }
    }   
    ?>
    <table width="50%" border="1">
        <tr><th>Points</th><th>MM</th></tr>
        <tr>
            <td align="center"><?php echo $final_array[3] . " x " .$final_array[4]; ?></td>
            <td align="center"><?php echo round(($final_array[3]*0.35277777778),2) . " x " .round(($final_array[4]*0.35277777778),2);?><br>
            <?php echo round(($final_array[3]*0.35277777778)) . " x " .round(($final_array[4]*0.35277777778));?></td>
           </tr>
    </table>
    <?php   
}
rathoddhirendra.blogspot.com-Google pagerank and Worth