// ========================================================================= // Custom Text on Product Page based on Shipping Class // ========================================================================= function display_shipping_class_on_product_page() { $product = wc_get_product(); $shipping_class = $product->get_shipping_class(); switch ( $shipping_class ) { case 'your_shipping_class_1': echo ''; break; case 'your_shipping_class_2': echo ''; break; } } add_action( 'woocommerce_single_product_summary', 'display_shipping_class_on_product_page', 10 );
WooCommerce
Disable payment method for specific country in WooCommerce
Add fee to specific payment gateway only for physical products
Add custom out of stock message for specific product category
WooCommerce free shipping over amount and hide other shipping methods
Disable Payment Gateway For Specific Product Category
Disable Payment Gateway For Specific Shipping Method
If product is out of stock echo something – WooCommerce product single
//Echo something if product is out of stock add_action( 'woocommerce_share', 'add_out_of_stock_custom_message', 5 ); function add_out_of_stock_custom_message() { global $product; if ( ! $product->is_in_stock() ) { echo ''; } }
How to send custom email based on product ID in WooCommerce
Disable Barion if shipping method is free shipping
//Disable Payment Gateway For Specific Shipping Method add_filter( 'woocommerce_available_payment_gateways', 'wpninja_gateway_disable_shipping_326' ); function wpninja_gateway_disable_shipping_326( $available_gateways ) { global $woocommerce; if ( !is_admin() ) { $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0]; if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'free_shipping' ) ) { unset( $available_gateways['barion'] ); } } return $available_gateways; }
How to apply coupon when specific product ID in the cart?
How to add Taxonomy Filter to WooCommerce admin
Search by product SKU
/** * Search by product SKU */ function wpninja_product_search_sku( $join, $query ) { if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) { return $join; } global $wpdb; $join .= " LEFT JOIN {$wpdb->postmeta} wpninja_post_meta ON {$wpdb->posts}.ID = wpninja_post_meta.post_id "; return $join; } add_filter( 'posts_join', 'wpninja_product_search_sku', 10, 2 ); /** * Modify the search query with posts_where. */ function wpninja_product_search_where( $where, $query ) { if ( ! $query->is_main_query() || is_admin() || ! is_search() || ! is_woocommerce() ) { return $where; } global $wpdb; $where = preg_replace( "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "({$wpdb->posts}.post_title LIKE $1) OR (wpninja_post_meta.meta_key = '_sku' AND wpninja_post_meta.meta_value LIKE $1)", $where ); return $where; } add_filter( 'posts_where', 'wpninja_product_search_where', 10, 2 );
Show SKU above the product title
//Show SKU above the product title function fdesign_show_sku(){ global $product; echo '<small>Cikkszám:</small> <small>' . $product->get_sku() .'</small>'; } add_action( 'woocommerce_before_single_product_summary', 'fdesign_show_sku', 5 );
Show Product Search in Header
//Show Product Search in Header function new_nav_menu_items($items, $args) { $my_product_search = get_product_search_form( $echo ); if($args->theme_location == 'main_navigation'){ $items = $items . $my_product_search; } return $items; } add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
How to fix WooCommerce Terms and Conditions link on Checkout Page
jQuery(function($){ $( "a.woocommerce-terms-and-conditions-link" ).unbind( "click" ); $( "body" ).on('click', 'a.woocommerce-terms-and-conditions-link', function( event ) { $(this).attr("target", "_blank"); window.open( $(this).attr("href")); return false; }); });
Add Product to WooCommerce Cart Automatically
add_action( 'template_redirect', 'snippet_add_product_to_cart' ); function snippet_add_product_to_cart() { // select ID $product_id = 21874; //check if product already in cart if ( WC()->cart->get_cart_contents_count() == 0 ) { // if no products in cart, add it WC()->cart->add_to_cart( $product_id ); } }
Add Product to Cart When Visiting a Specific Page ID
add_action( 'wp', 'add_product_to_cart_on_page_id_load' ); function add_product_to_cart_on_page_id_load() { // product ID to add to cart $product_id = 21874; // page ID to target if ( is_page( 19473 ) ) { WC()->cart->empty_cart(); WC()->cart->add_to_cart( $product_id ); } }
Add custom field to single product page
//ADD CUSTOM FIELD TO SINGLE PRODUCT BEFORE ADD TO CART BUTTON add_action( 'woocommerce_single_variation', 'add_custom_field_before_addtocart', 5 ); function add_custom_field_before_addtocart() { include('woo/custom-field.php'); }
<?php the_field('custom'); ?>
Add custom text below the table of email notification
add_action('woocommerce_email_after_order_table', 'add_email_shipping_text'); function add_email_shipping_text(){ echo ''; }