Saturday, May 25, 2013

Add Captcha in IQ Testimonials( Wordpress Plugin ) Form

If you need to add captcha to stop spam on Wordpress IQ Testimonials Plugin Form (http://wordpress.org/plugins/iq-testimonials/) as captcha is not inbuilt in the form

First We need to Install and activate wordpress plugin Flexible Captha (http://wordpress.org/plugins/flexible-captcha/)

Once this Plugin is activated successfully .
Add below code in plugins/iq-testimonials/lib/iq-testimonials-form.php file
First to display Captcha filed above submit button in  the form add below code
$html .=  do_shortcode('[FC_captcha_fields]' ) ;

(just before this line of code almost line no.257)
 $html .= '<input id="submit" name="submit" type="submit" value="Submit" />
Now to put functionality of verifying captcha text Add below lines of code almost after line no 69

 global $FlexibleCaptcha;
 if (!$FlexibleCaptcha->check_captcha_val()) {
  $result['valid'] = false;
  $result['reason']['FC_captcha_input'] = 'The entered text did not match the captcha image.';
  $formerrorBits['capt'] = true;
        $formerrors[] = "<p class=\"iq-testimonials-error\">The entered text did not match the captcha image.</p>\n";   
     //$form_errors_present = true;
 }



Above code can be added before this existing  code in the file
if ((empty($email)) && ($form_email == 'true') && (empty($email_field))){
   $formerrorBits['email'] = true;
   $form_errors_present = true;
  }

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

Show Average Rating on Product Page in Woocommerce

To display average rating of product on product page , Add below code in theme's functions.php file

add_action('woocommerce_after_shop_loop_item', 'my_print_stars' );


function my_print_stars(){
    global $wpdb;
    global $post;
    $count = $wpdb->get_var("
    SELECT COUNT(meta_value) FROM $wpdb->commentmeta
    LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
    WHERE meta_key = 'rating'
    AND comment_post_ID = $post->ID
    AND comment_approved = '1'
    AND meta_value > 0
");

$rating = $wpdb->get_var("
    SELECT SUM(meta_value) FROM $wpdb->commentmeta
    LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
    WHERE meta_key = 'rating'
    AND comment_post_ID = $post->ID
    AND comment_approved = '1'
");

if ( $count > 0 ) {

    $average = number_format($rating / $count, 2);

    echo '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';

    echo '<span class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woocommerce'), $average).'"><span style="width:'.($average*16).'px"><span itemprop="ratingValue" class="rating">'.$average.'</span> </span></span>';

    echo '</div>';
    }

Woo commerce Clear Cart Functionality on Car Page

Below is the code to Add 'Clear Cart ' functionality in woocommerce

1) Add this in Theme's functions.php file
add_action('init', 'woocommerce_clear_cart_url');

function woocommerce_clear_cart_url() {

 global $woocommerce;

 if( isset($_REQUEST['clear-cart']) ) {

  $woocommerce-> cart->empty_cart();

 }

}
2) Add Below code in plugins/woocommerce/templates/cart.php file

<input class="button" name="clear-cart" type="submit" value="&lt;?php _e('Empty Cart', 'woocommerce'); ?&gt;" />

Note:(You can add this just before below existing code


<input class="button" name="update_cart" type="submit" value="&lt;?php _e('Update Cart', 'woocommerce'); ?&gt;" /></code>

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;