25 Best WooCommerce Snippets For WordPress Part 2

woocommerce-snippets-list.png

1 – REPLACE WOOCOMMERCE DEFAULT PAYPAL LOGO

1 /*
2  * Replace WooCommerce default PayPal icon
3  */
4 function paypal_checkout_icon() {
5  return 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_betalen_met_paypal_nl.jpg'; // write your own image URL here
6 }
7 add_filter( 'woocommerce_paypal_icon''paypal_checkout_icon' );

2 – REPLACE DEFAULT PRODUCT PLACEHOLDER IMAGE

1 /*
2 * goes in theme functions.php or a custom plugin. Replace the image filename/path with your own :)
3 *
4 **/
5 add_action( 'init''custom_fix_thumbnail' );
6  
7 function custom_fix_thumbnail() {
8   add_filter('woocommerce_placeholder_img_src','custom_woocommerce_placeholder_img_src');
9    
10     function custom_woocommerce_placeholder_img_src( $src ) {
11     $upload_dir = wp_upload_dir();
12     $uploads = untrailingslashit( $upload_dir['baseurl'] );
13     $src $uploads '/2012/07/thumb1.jpg';
14      
15     return $src;
16     }
17 }

3 – REMOVE “PRODUCTS” FROM BREADCRUMB

1 /*
2  * Hide "Products" in WooCommerce breadcrumb
3  */
4 function woo_custom_filter_breadcrumbs_trail ( $trail ) {
5   foreach $trail as $k => $v ) {
6     if strtolowerstrip_tags$v ) ) == 'products' ) {
7       unset( $trail[$k] );
8       break;
9     }
10   }
11
12   return $trail;
13 }
14
15 add_filter( 'woo_breadcrumbs_trail''woo_custom_filter_breadcrumbs_trail', 10 );

4 – EMPTY CART

1 /*
2  * Empty WooCommerce cart
3  */
4 function my_empty_cart(){
5     global $woocommerce;
6     $woocommerce->cart->empty_cart();
7 }
8 add_action('init''my_empty_cart');

5 – AUTOMATICALLY ADD PRODUCT TO CART ON VISIT

1 /*
2  * Add item to cart on visit
3  */
4 function add_product_to_cart() {
5     if ( ! is_admin() ) {
6         global $woocommerce;
7         $product_id = 64;
8         $found = false;
9         //check if product already in cart
10         if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
11             foreach $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
12                 $_product $values['data'];
13                 if $_product->id == $product_id )
14                     $found = true;
15             }
16             // if product not found, add it
17             if ( ! $found )
18                 $woocommerce->cart->add_to_cart( $product_id );
19         else {
20             // if no products in cart, add it
21             $woocommerce->cart->add_to_cart( $product_id );
22         }
23     }
24 }
25 add_action( 'init''add_product_to_cart' );

6 – ADD A CUSTOM CURRENCY / SYMBOL

1 add_filter( 'woocommerce_currencies''add_my_currency' );
2  
3 function add_my_currency( $currencies ) {
4      $currencies['ABC'] = __( 'Currency name''woocommerce' );
5      return $currencies;
6 }
7  
8 add_filter('woocommerce_currency_symbol''add_my_currency_symbol', 10, 2);
9  
10 function add_my_currency_symbol( $currency_symbol$currency ) {
11      switch$currency ) {
12           case 'ABC'$currency_symbol '$'break;
13      }
14      return $currency_symbol;
15 }

7 – CHANGE ADD TO CART BUTTON TEXT

1 /**
2  * Change the add to cart text on single product pages
3  */
4 function woo_custom_cart_button_text() {
5     return __('My Button Text''woocommerce');
6 }
7 add_filter('single_add_to_cart_text''woo_custom_cart_button_text');
8
9
10
11 /**
12  * Change the add to cart text on product archives
13  */
14 function woo_archive_custom_cart_button_text() {
15     return __( 'My Button Text''woocommerce' );
16 }
17 add_filter( 'add_to_cart_text''woo_archive_custom_cart_button_text' );

8 – REDIRECT SUBSCRIPTION ADD TO CART TO CHECKOUT PAGE

1 /**
2  * Redirect subscription add to cart to checkout page
3  *
4  * @param string $url
5  */
6 function custom_add_to_cart_redirect( $url ) {
7   
8   $product_id   = (int) $_REQUEST['add-to-cart'];
9     if class_exists'WC_Subscriptions_Product' ) ) {
10         if ( WC_Subscriptions_Product::is_subscription( $product_id ) ) {
11             return get_permalink(get_option( 'woocommerce_checkout_page_id' ) );
12         else return $url;
13     else return $url;
14     
15 }
16 add_filter('add_to_cart_redirect''custom_add_to_cart_redirect');

This snippet requires the Subscriptions plugin.

9 – REDIRECT TO CHECKOUT PAGE AFTER ADD TO CART

1 /**
2  * Redirect subscription add to cart to checkout page
3  *
4  * @param none
5  */
6 function add_to_cart_checkout_redirect() {
7     wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
8     die();
9 }
10 add_action( 'woocommerce_add_to_cart',  'add_to_cart_checkout_redirect', 11 );

10 – CC ALL EMAILS

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Add another email recipient to all WooCommerce emails
6  *
7  */
8 function woo_cc_all_emails() {
9   return 'Bcc: youremail@yourdomain.com' "rn";
10 }
11 add_filter('woocommerce_email_headers''woo_cc_all_emails' );

11 – SEND AN EMAIL WHEN A NEW ORDER IS COMPLETED WITH COUPONS USED

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Send an email each time an order with coupon(s) is completed
6  * The email contains coupon(s) used during checkout process
7  *
8  */
9 function woo_email_order_coupons( $order_id ) {
10         $order new WC_Order( $order_id );
11         
12         if$order->get_used_coupons() ) {
13         
14           $to 'youremail@yourcompany.com';
15             $subject 'New Order Completed';
16             $headers 'From: My Name ' "rn";
17             
18             $message 'A new order has been completed.n';
19             $message .= 'Order ID: '.$order_id.'n';
20             $message .= 'Coupons used:n';
21             
22             foreach$order->get_used_coupons() as $coupon) {
23                 $message .= $coupon.'n';
24             }
25             @wp_mail( $to$subject$message$headers );
26         }
27 }
28 add_action( 'woocommerce_thankyou''woo_email_order_coupons' );

12 – CHANGE RELATED PRODUCTS NUMBER

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Change number of related products on product page
6  * Set your own value for 'posts_per_page'
7  *
8  */
9 function woo_related_products_limit() {
10   global $product;
11     
12     $args array(
13         'post_type'             => 'product',
14         'no_found_rows'         => 1,
15         'posts_per_page'        => 6,
16         'ignore_sticky_posts'   => 1,
17         'orderby'               => $orderby,
18         'post__in'              => $related,
19         'post__not_in'          => array($product->id)
20     );
21     return $args;
22 }
23 add_filter( 'woocommerce_related_products_args''woo_related_products_limit' );

13 – EXCLUDE PRODUCTS FROM A PARTICULAR CATEGORY ON THE SHOP PAGE

1 /**
2  * Remove products from shop page by category
3  *
4  */
5 function woo_custom_pre_get_posts_query( $q ) {
6  
7     if ( ! $q->is_main_query() ) return;
8     if ( ! $q->is_post_type_archive() ) return;
9     
10     if ( ! is_admin() && is_shop() ) {
11  
12         $q->set( 'tax_query'array(array(
13             'taxonomy' => 'product_cat',
14             'field' => 'slug',
15             'terms' => array'shoes' ), // Don't display products in the shoes category on the shop page
16             'operator' => 'NOT IN'
17         )));
18     
19     }
20  
21     remove_action( 'pre_get_posts''custom_pre_get_posts_query' );
22  
23 }
24 add_action( 'pre_get_posts''woo_custom_pre_get_posts_query' );

14 – CHANGE SHOP COLUMNS NUMBER

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Change product columns number on shop pages
6  *
7  */
8 function woo_product_columns_frontend() {
9     global $woocommerce;
10
11     // Default Value also used for categories and sub_categories
12     $columns = 4;
13
14     // Product List
15     if ( is_product_category() ) :
16         $columns = 4;
17     endif;
18
19     //Related Products
20     if ( is_product() ) :
21         $columns = 2;
22     endif;
23
24     //Cross Sells
25     if ( is_checkout() ) :
26         $columns = 4;
27     endif;
28
29     return $columns;
30 }
31 add_filter('loop_shop_columns''woo_product_columns_frontend');

15 – DISABLE WOOCOMMERCE TABS

1 /**
2  * Remove product tabs
3  *
4  */
5 function woo_remove_product_tab($tabs) {
6
7     unset( $tabs['description'] );              // Remove the description tab
8     unset( $tabs['reviews'] );                  // Remove the reviews tab
9     unset( $tabs['additional_information'] );   // Remove the additional information tab
10
11     return $tabs;
12  
13 }
14 add_filter( 'woocommerce_product_tabs''woo_remove_product_tab', 98);

16 – REMOVE BREADCRUMB

1 /**
2  * Remove WooCommerce BreadCrumb
3  *
4  */
5 remove_action( 'woocommerce_before_main_content''woocommerce_breadcrumb', 20);

17 – RESTRICT SHIPPING COUNTRIES LIST

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Restrict shipping countries list
6  *
7  */
8 function woo_override_checkout_fields( $fields ) {
9
10     $fields['shipping']['shipping_country'] = array(
11         'type'      => 'select',
12         'label'     => __('My New Country List''woocommerce'),
13         'options'   => array('AU' => 'Australia')
14     );
15
16     return $fields;
17 }
18 add_filter( 'woocommerce_checkout_fields' 'woo_override_checkout_fields' );

18 – REPLACE “FREE!” PRODUCT STRING

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Replace "Free!" by a custom string
6  *
7  */
8 function woo_my_custom_free_message() {
9     return "This product is FREE!";
10 }
11
12 add_filter('woocommerce_free_price_html''woo_my_custom_free_message');

19 – HIDE ALL OTHER SHIPPING METHODS WHEN FREE SHIPPING IS AVAILABLE

1 // Hide ALL shipping options when free shipping is available
2 add_filter( 'woocommerce_available_shipping_methods','hide_all_shipping_when_free_is_available' , 10, 1 );
3  
4 /**
5 * Hide ALL Shipping option when free shipping is available
6 *
7 * @param array $available_methods
8 */
9 function hide_all_shipping_when_free_is_available( $available_methods ) {
10  
11     if( isset( $available_methods['free_shipping'] ) ) :
12         
13         // Get Free Shipping array into a new array
14         $freeshipping array();
15         $freeshipping $available_methods['free_shipping'];
16  
17         // Empty the $available_methods array
18         unset( $available_methods );
19  
20         // Add Free Shipping back into $avaialble_methods
21         $available_methods array();
22         $available_methods[] = $freeshipping;
23  
24     endif;
25  
26     return $available_methods;
27 }

20 – MAKE CHECKOUT “STATE” FIELD NOT REQUIRED

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Make "state" field not required on checkout
6  *
7  */
8  
9 add_filter( 'woocommerce_billing_fields''woo_filter_state_billing', 10, 1 );
10 add_filter( 'woocommerce_shipping_fields''woo_filter_state_shipping', 10, 1 );
11
12 function woo_filter_state_billing( $address_fields ) {
13     $address_fields['billing_state']['required'] = false;
14     return $address_fields;
15 }
16
17 function woo_filter_state_shipping( $address_fields ) {
18     $address_fields['shipping_state']['required'] = false;
19     return $address_fields;
20 }

21 – CREATE A COUPON PROGRAMATICALLY

1 $coupon_code 'UNIQUECODE'// Code
2 $amount '10'// Amount
3 $discount_type 'fixed_cart'// Type: fixed_cart, percent, fixed_product, percent_product
4                     
5 $coupon array(
6     'post_title' => $coupon_code,
7     'post_content' => '',
8     'post_status' => 'publish',
9     'post_author' => 1,
10     'post_type'     => 'shop_coupon'
11 );
12                     
13 $new_coupon_id = wp_insert_post( $coupon );
14                     
15 // Add meta
16 update_post_meta( $new_coupon_id'discount_type'$discount_type );
17 update_post_meta( $new_coupon_id'coupon_amount'$amount );
18 update_post_meta( $new_coupon_id'individual_use''no' );
19 update_post_meta( $new_coupon_id'product_ids''' );
20 update_post_meta( $new_coupon_id'exclude_product_ids''' );
21 update_post_meta( $new_coupon_id'usage_limit''' );
22 update_post_meta( $new_coupon_id'expiry_date''' );
23 update_post_meta( $new_coupon_id'apply_before_tax''yes' );
24 update_post_meta( $new_coupon_id'free_shipping''no' );

22 – CHANGE EMAIL SUBJECT LINES

1 /*
2  * Subject filters:
3  *   woocommerce_email_subject_new_order
4  *   woocommerce_email_subject_customer_procesing_order
5  *   woocommerce_email_subject_customer_completed_order
6  *   woocommerce_email_subject_customer_invoice
7  *   woocommerce_email_subject_customer_note
8  *   woocommerce_email_subject_low_stock
9  *   woocommerce_email_subject_no_stock
10  *   woocommerce_email_subject_backorder
11  *   woocommerce_email_subject_customer_new_account
12  *   woocommerce_email_subject_customer_invoice_paid
13  **/
14 add_filter('woocommerce_email_subject_new_order''change_admin_email_subject', 1, 2);
15  
16 function change_admin_email_subject( $subject$order ) {
17     global $woocommerce;
18  
19     $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
20  
21     $subject = sprintf( '[%s] New Customer Order (# %s) from Name %s %s'$blogname,$order->id, $order->billing_first_name, $order->billing_last_name );
22  
23     return $subject;
24 }

23 – ADD CUSTOM FEE TO CART

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Add custom fee to cart automatically
6  *
7  */
8 function woo_add_cart_fee() {
9
10     global $woocommerce;
11     
12     if ( is_cart() ) {
13         $woocommerce->cart->add_fee( __('Custom''woocommerce'), 5 );
14     }
15     
16 }
17 add_action( 'woocommerce_before_cart_table''woo_add_cart_fee' );

24 – CUSTOM ADDED TO CART MESSAGE

1 /**
2  * Custom Add To Cart Messages
3  * Add this to your theme functions.php file
4  **/
5 add_filter( 'woocommerce_add_to_cart_message''custom_add_to_cart_message' );
6 function custom_add_to_cart_message() {
7     global $woocommerce;
8  
9     // Output success messages
10     if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
11  
12         $return_to  = get_permalink(woocommerce_get_page_id('shop'));
13  
14         $message    = sprintf('<a href="%s">%s</a> %s'$return_to, __('Continue Shopping &rarr;''woocommerce'), __('Product successfully added to your cart.''woocommerce') );
15  
16     else :
17  
18         $message    = sprintf('<a href="%s">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;','woocommerce'), __('Product successfully added to your cart.''woocommerce') );
19  
20     endif;
21  
22         return $message;
23 }

25 – ADD PAYMENT METHOD TO ADMIN EMAIL

1 /**
2  * WooCommerce Extra Feature
3  * --------------------------
4  *
5  * Add payment method to admin new order email
6  *
7  */
8 add_action( 'woocommerce_email_after_order_table','woo_add_payment_method_to_admin_new_order', 15, 2 );
9
10 function woo_add_payment_method_to_admin_new_order( $order$is_admin_email ) {
11     if $is_admin_email ) {
12     echo '<p><strong>Payment Method:</strong> ' $order->payment_method_title .'</p>';
13     }
14 } 

 

*originally posted at http://www.wpexplorer.com/best-woocommerce-snippets-wordpress/