Tag: Woocommerce

WooCommerce hide unavailable veriations from dorpdown

If there are greater than 20 variations in the product, it will use ajax to find matches and won’t hide combinations (because doing so would mean loading all variations before display).

Use is code in your functions.php

function custom_wc_ajax_variation_threshold( $qty, $product ) {
	return 10;
}

add_filter( 'woocommerce_ajax_variation_threshold', 'custom_wc_ajax_variation_threshold', 10, 2 );

 

Change the return value as per your number of combination.

WooCommerce: How to apply discount coupon automatically

Step 1: Create a coupon (http://docs.woothemes.com/document/coupon-management/)

 

Step 2: Add this code in your theme function.php

add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );

function apply_matched_coupons() {
    global $woocommerce;

    $coupon_code = '10percent'; // your coupon code here

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( $woocommerce->cart->cart_contents_total >= 500 ) {
        $woocommerce->cart->add_discount( $coupon_code );
        $woocommerce->show_messages();
    }

}

 

WooCommerce Single page: Content position change

How to change product title, price, short description, add to cart position on product details page :

Step 1:

Apply remove filters on your theme function.php

<?php

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title',5);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price',10);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt',20);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta',40);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_sharing',50);
?

Step 2:

Now add the hooks again and change their position

<?php

add_action('woocommerce_single_product_summary', 'woocommerce_template_single_title',5);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price',20);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt',30);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',10);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta',40);
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_sharing',50);
?>

WooCommerce – Add a special field to the checkout, order emails and user/order meta

/**
 * Add the field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

	echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';

	/**
	 * Output the field. This is for 1.4.
	 *
	 * To make it compatible with 1.3 use $checkout->checkout_form_field instead:

	 $checkout->checkout_form_field( 'my_field_name', array( 
	 	'type' 			=> 'text', 
	 	'class' 		=> array('my-field-class orm-row-wide'), 
	 	'label' 		=> __('Fill in this field'), 
	 	'placeholder' 	=> __('Enter a number'),
	 	));
	 **/
	woocommerce_form_field( 'my_field_name', array( 
		'type' 			=> 'text', 
		'class' 		=> array('my-field-class orm-row-wide'), 
		'label' 		=> __('Fill in this field'), 
		'placeholder' 	=> __('Enter a number'),
		), $checkout->get_value( 'my_field_name' ));

	echo '</div>';

	/**
	 * Optional Javascript to limit the field to a country. This one shows for italy only.
	 **/
	?>
	<script type="text/javascript">
		jQuery('select#billing_country').live('change', function(){

			var country = jQuery('select#billing_country').val();

			var check_countries = new Array(<?php echo '"IT"'; ?>);

			if (country && jQuery.inArray( country, check_countries ) >= 0) {
				jQuery('#my_custom_checkout_field').fadeIn();
			} else {
				jQuery('#my_custom_checkout_field').fadeOut();
				jQuery('#my_custom_checkout_field input').val('');
			}

		});
	</script>
	<?php
}

/**
 * Process the checkout
 **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
	global $woocommerce;

	// Check if set, if its not set add an error. This one is only requite for companies
	if ($_POST['billing_company'])
		if (!$_POST['my_field_name']) 
			$woocommerce->add_error( __('Please enter your XXX.') );
}

/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');

function my_custom_checkout_field_update_user_meta( $user_id ) {
	if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
	if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
	$keys[] = 'My Field';
	return $keys;
}