Showing posts with label WooCommerce. Show all posts
Showing posts with label WooCommerce. Show all posts

Friday, May 24, 2013

Add Extra field in Billing Address Form on checkout page in Woocommerce

Below code should be added in theme's functions.php file to add an extra Filed in Billing Address Form on checkout page , below code sample is to add 'Land-line Phone' Filed .

// Hook in*/
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
 
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['billing']['shipping_Landlinephone'] = array(
        'label'     => __('Landline Phone', 'woocommerce'),
    'placeholder'   => _x('Landline Phone', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );
 
     return $fields;
}

function display_shipping_Landlinephone($order){
    echo "<p><strong>Landline Phone:</strong> " . $order->order_custom_fields['_shipping_Landlinephone'][0] . "</p>";
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_shipping_Landlinephone', 10, 1 );


//validate Field */
add_action('woocommerce_checkout_process', 'billing_phone_checkout_field_process');
 
function billing_phone_checkout_field_process() {
    global $woocommerce;
 
    // Check if set, if its not set add an error.
    if (!$_POST['billing_phone'])
         $woocommerce->add_error( __('Please enter something into Land-line Phone field.') );
}

Set Default State in Woo commerce on Checkout page

Below code has to be added in theme's functions.php file to set some default state on checkout form in woo commerce

add_filter( 'default_checkout_state', 'change_default_checkout_state' );
 

 
function change_default_checkout_state() {
  return 'Maharashtra'; // state code
}
Note : Above code sample is to set 'Maharashtra ' as default state

Add currency in Woocommerce

Below code can be used to add currency in woo commerce (if it is not there by default)

add_filter( 'woocommerce_currencies', 'add_inr_currency' );
add_filter( 'woocommerce_currency_symbol', 'add_inr_currency_symbol' );
 
function add_inr_currency( $currencies ) {
    $currencies['INR'] = 'INR';
    return $currencies;
}
 //addedy by tanvi for adding INR Currency
function add_inr_currency_symbol( $symbol ) {
 $currency = get_option( 'woocommerce_currency' );
 switch( $currency ) {
  case 'INR': $symbol = 'Rs.'; break;
 }
 return $symbol;
}

Note: This is to Add INR (Indian Rs) Currency , same code can be used for any currency 

Woo-commerce Set the Pay Pal Exchange Rate While converting from one currency to another

Below is the code to set the exchange rate in PayPal while converting from INR Currency to USD

add_filter('woocommerce_paypal_args', 'convert_INR_to_USD');
function convert_INR_to_USD($paypal_args){
 if ( $paypal_args['currency_code'] == 'INR'){
  $convert_rate = 53; //set the converting rate
  $paypal_args['currency_code'] = 'USD'; //change INR to USD
  $i = 1;

  while (isset($paypal_args['amount_' . $i])) {
   $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
   ++$i;
  }

 }
return $paypal_args;
}

Sunday, March 17, 2013

WooCommerce: Trigger Free Shipping min_amount Logic on discounted Final Order Amount

WooCommerce, by default, applies the "Free Shipping" logic on the Order total (not discounted) before the application of a discount coupon.

Here is a Use-Case: Free Shipping is enabled if the cart total is greater than $800, that is, when the cart total is, for example, $1000, free shipping should be available.

However, if a discount coupon of 50% discount is applied on the $1000 total, Free shipping must not be available - because now the new total is $500. WooCommerce does not do this and Free Shipping logic triggers on the Un-discounted Cart Total and not on the new Total after the discount coupon is applied. Therefore, even when the final order amount is $500 (less than $800), Free Shipping is enabled.

Here is how this behaviour can be changed:

1) Open the file class-wc-free-shipping.php present in the plugins/woocommerce/classes/shipping folder.

2) Within the function is_available($package), replace this piece of code:

if ( $woocommerce->cart->prices_include_tax )
    $total = $woocommerce->cart->tax_total + $woocommerce->cart->cart_contents_total;
else
    $total = $woocommerce->cart->cart_contents_total;

with this,


if ( $woocommerce->cart->prices_include_tax )
    $total = $woocommerce->cart->tax_total + $woocommerce->cart->cart_contents_total - $woocommerce->cart->discount_total;
else
    $total = $woocommerce->cart->cart_contents_total - $woocommerce->cart->discount_total;