Wednesday 25 February 2015

Convert Number to words through Js

var th = ['', 'Thousand', 'Million', 'Billion', 'Trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
var tn = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
var tw = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];

function toWords(s)
{
s = s.toString();
s = s.replace(/[\, ]/g, '');
if (s != parseFloat(s)) return 'Not a Number';
    var x = s.indexOf('.');
if (x == -1) x = s.length; if (x > 15) return 'Too Big';
    var n = s.split('');
    var str = '';
    var sk = 0;
    for (var i = 0; i < x; i++)
{
        if ((x - i) % 3 == 2)
        {
   if (n[i] == '1')
{
  str += tn[Number(n[i + 1])] + ' '; i++; sk = 1;
}
else if (n[i] != 0)
{
  str += tw[n[i] - 2] + ' '; sk = 1; }
}
        else if (n[i] != 0)
{
    str += dg[n[i]] + ' ';
if ((x - i) % 3 == 0) str += 'Hundred '; sk = 1;
}
        if ((x - i) % 3 == 1)
{
  if (sk) str += th[(x - i - 1) / 3] + ' '; sk = 0;
}
    }
if (x != s.length) { var y = s.length; str += 'and  ';
    var remain_count = y-(x+1);
 
    for (var i = x + 1; i < y; i++)
    {
if (n[i] == '1')
{
if(n[i] == '1' && remain_count == 1)
{
str += tn[Number(0)];
}
else
{
str += tn[Number(n[i + 1])];
}
}
else if (n[i] != 0 && remain_count == 2)
{
  if((i+1) == y)
  {
  if(n[i] != 1 && n[i-1] != 1)
  {
     str += dg[n[i]] + '  ';
  }
  }
  else
  {
     str += tw[n[i] - 2] + ' ';
  }
}
else
{
str += tw[n[i] - 2] + ' ';
}
    }
    str +=' Cents' } return str.replace(/\s+/g, ' ') +' Only';
}

var alert_var = toWords(1256789.18);
alert(alert_var);

Wednesday 28 January 2015

Get date list by month, year and day in php

function dates_month($month,$year,$day_name)
{
  $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  $dates_month=array();
  for($i=1;$i<=$num;$i++)
  {
   $mktime=mktime(0,0,0,$month,$i,$year);
   $date=date("d-M-Y",$mktime);
   
   // If your day is come with Mon, Tue, etc..
   /*$date_day = date('D',$mktime); 
   if($date_day == $day_name)
   {
      $dates_month[$i]=$date.'day'.$date_day;
   }
   */
   
   // If your day is come with 1,2, etc..
   $date_day = date('w',$mktime); 
   if($date_day == $day_name)
   {
      $dates_month[$i]=$date;
   }
   
  }
  $dates = array_values($dates_month);
  return $dates;
}


echo"<pre>";

print_r(dates_month(2,2015,2)); 

echo"</pre>"; 

Tuesday 30 September 2014

How to count month and days between two dates through JavaScript?

if (firstDate.getTime() > secondDate.getTime()) {
    var tmp = firstDate;
    firstDate = secondDate;
    secondDate = tmp;
}

var years = secondDate.getFullYear() - firstDate.getFullYear();
var months = secondDate.getMonth() - firstDate.getMonth();
var days = secondDate.getDate() - firstDate.getDate();

for (var i = 0; days < 0; ++i) {
    // while the day difference is negative
    // we break up months into days, starting with the first
    months -= 1;
    days += new Date(
        firstDate.getFullYear(),
        firstDate.getMonth() + 1 + i,
        0, 0, 0, 0, 0
    ).getDate();
}
for (; months < 0;) {
    years -= 1;
    months += 12;
}
console.log((12 * years + months) + ' months and ' + days + ' days');
Examples:
02/12 to 02/22: 10 days
09/27 to 11/01: 4 days, 1 month
12/31 to 03/01: 1 day, 2 months
05/31 to 06/30: 30 days
01/31 to 03/30: 30 days, 1 month
10/27/2010 to 08/26/2014: 30 days, 45 months
if (firstDate.getTime() > secondDate.getTime()) {
   var tmp = firstDate;
   firstDate = secondDate;
   secondDate = tmp;
}
var years = secondDate.getFullYear() - firstDate.getFullYear();
var months = secondDate.getMonth() - firstDate.getMonth();
var days = secondDate.getDate() - firstDate.getDate();
for (var i = 0; days < 0; ++i) {
    // while the day difference is negative
    // we break up months into days, starting with the first
    months -= 1;
    days += new Date(
        firstDate.getFullYear(),
        firstDate.getMonth() + 1,
        0, 0, 0, 0, 0
    ).getDate();
}
for (; months < 0;) {
    years -= 1;
    months += 12;
}
console.log((12 * years + months) + ' months and ' + days + ' days');
Examples:
02/12 to 02/22: 11 days 
09/27 to 11/01: 5 days, 1 month 
12/31 to 03/01: 2 day, 2 months 
05/31 to 06/30: 31 days 
01/31 to 03/30: 31 days, 1 month 
10/27/2010 to 08/26/2014: 31 days, 45 months

Thursday 23 January 2014

Create category custom field in wordpress

Add this code in function.php

/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );

function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
  <label for="category_custom_order">Custom Order</label>
  <input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
  <p class="description">Enter a custom order value.</p>
</div>
<?php
}

function category_form_custom_field_edit( $tag, $taxonomy ) {

    $option_name = 'category_custom_order_' . $tag->term_id;
    $category_custom_order = get_option( $option_name );

?>
<tr class="form-field">
  <th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
  <td>
    <input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
    <p class="description">Enter a custom order value.</p>
  </td>
</tr>
<?php
}

/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 );   
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );

function category_form_custom_field_save( $term_id, $tt_id ) {

    if ( isset( $_POST['category_custom_order'] ) ) {           
        $option_name = 'category_custom_order_' . $term_id;
        update_option( $option_name, $_POST['category_custom_order'] );
    }
}

Tuesday 7 January 2014

Remove all orders from Magento database

First take backup your site. Run directly on the backend database.



SET FOREIGN_KEY_CHECKS=0;
TRUNCATE `sales_flat_creditmemo`;
TRUNCATE `sales_flat_creditmemo_comment`;
TRUNCATE `sales_flat_creditmemo_grid`;
TRUNCATE `sales_flat_creditmemo_item`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_comment`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_quote_shipping_rate`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_comment`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_track`;
TRUNCATE `sales_invoiced_aggregated`;
TRUNCATE `sales_invoiced_aggregated_order`;
TRUNCATE `sales_order_aggregated_created`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;
ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;

Remove all customer from magento database

First take backup your site. Run directly on the backend database. This will delete all customer data including logs.



SET FOREIGN_KEY_CHECKS=0;
-- reset customers
TRUNCATE customer_address_entity;
TRUNCATE customer_address_entity_datetime;
TRUNCATE customer_address_entity_decimal;
TRUNCATE customer_address_entity_int;
TRUNCATE customer_address_entity_text;
TRUNCATE customer_address_entity_varchar;
TRUNCATE customer_entity;
TRUNCATE customer_entity_datetime;
TRUNCATE customer_entity_decimal;
TRUNCATE customer_entity_int;
TRUNCATE customer_entity_text;
TRUNCATE customer_entity_varchar;
TRUNCATE log_customer;
TRUNCATE log_visitor;
TRUNCATE log_visitor_info;

ALTER TABLE customer_address_entity AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_datetime AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_decimal AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_int AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_text AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_varchar AUTO_INCREMENT=1;
ALTER TABLE customer_entity AUTO_INCREMENT=1;
ALTER TABLE customer_entity_datetime AUTO_INCREMENT=1;
ALTER TABLE customer_entity_decimal AUTO_INCREMENT=1;
ALTER TABLE customer_entity_int AUTO_INCREMENT=1;
ALTER TABLE customer_entity_text AUTO_INCREMENT=1;
ALTER TABLE customer_entity_varchar AUTO_INCREMENT=1;
ALTER TABLE log_customer AUTO_INCREMENT=1;
ALTER TABLE log_visitor AUTO_INCREMENT=1;
ALTER TABLE log_visitor_info AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;

Monday 16 December 2013

Wordpress login redirect on magento login

When user click on wordpress login it will be redirected on magento login page. After successfully login it will be redirected to wordpress site.

Add function in wp-content/themes/themefolder/functions.php

    <?php
    // For login page
    function redirect_login_page(){
       $page_viewed = basename($_SERVER['REQUEST_URI']);
       $redirect_link = base64_encode($_GET['redirect_to']);
       $home_url = base64_encode( site_url() );
        if( $page_viewed == "wp-login.php") {
          // And away they go...
           $login_page  = 'store url/customer/account/login/referer/'.$home_url; // Store url is your magento site url
           wp_redirect($login_page);
         }
         // For redirection url
         if($_GET['redirect_to'] != '') {
          $login_page  = 'store url/customer/account/login/referer/'.$redirect_link; // Store url is your magento site url
              // This condition is checked for Admin user.
              if( $_GET['redirect_to'] != 'website url/wp-admin/') {  // website url is your website site url
                 wp_redirect($login_page);
              }
         }
    }
    add_action('init','redirect_login_page');
   
    // For logout page
    function redirect_logoutpage()
    {
       $logout_page  = 'store url/customer/account/logout/'; // Store url is your magento site url
       $redirect_link = base64_encode($_GET['redirect_to']);
         if($_GET['loggedout'] || $_GET['action'] == 'logout') {
              if( $_GET['redirect_to'] != 'website url/wp-admin/') { // website url is your website site url
                 wp_redirect($logout_page);
              }
         }
    }
    add_action('init','redirect_logoutpage');
    ?>