My 25 Best WooCommerce Snippets For WordPress

By WP Saviour •  Updated: 03/02/21 •  3 min read

Events flies… It’s been 6 months since I started to work for WooThemes as a WooCommerce full time help technician. All through these 6 months I learnt fairly a bit on WooCommerce and I coded fairly a bit (be taught fairly a bit!) of WooCommerce related snippets. Actually, WooCommerce is totally extremely efficient software program and easily expandable. It has many hooks that may be utilized for virtually each little factor, and that’s what make WooCommerce so good. This is a guidelines of 25 Best WooCommerce Snippets For WordPress, I wrote or tailor-made in the midst of the earlier months; all these snippets need to be pasted inside the options.php file inside your theme folder:

1 – Add Price Type to WooCommerce Admin E mail

add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 ); function add_payment_method_to_admin_new_order( $order, $is_admin_email ) { if ( $is_admin_email ) { echo '
Price Approach: ' . $order->payment_method_title . '

'; } }

2 – Up-sells merchandise per internet web page / per line

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 ); if ( ! function_exists( 'woocommerce_output_upsells' ) ) { function woocommerce_output_upsells() { woocommerce_upsell_display( 3,3 );    }
}

3 – Take away product courses from retailer internet web page

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); function custom_pre_get_posts_query( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) { $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'self-discipline' => 'slug', 'phrases' => array( 'color', 'style', 'spices', 'vanilla' ), 'operator' => 'NOT IN' ))); } remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); }

4 – Quickly translate any string

add_filter('gettext', 'translate_text');
add_filter('ngettext', 'translate_text'); function translate_text($translated) { $translated = str_ireplace('Choose and chance', 'Select', $translated); return $translated;
}

5 – Exclude a category from the WooCommerce class widget

add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' ); function woo_product_cat_widget_args( $cat_args ) { $cat_args['exclude'] = array('16'); return $cat_args;
}

6 – Add a personalized self-discipline to a product variation

add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 ); function variable_fields( $loop, $variation_data ) { ?> 

WP Saviour