// ========================================================================= // REMOVE ADDITIONAL INFORMATION TAB // ========================================================================= add_filter( 'woocommerce_product_tabs', 'remove_additional_information_tab', 100, 1 ); function remove_additional_information_tab( $tabs ) { unset($tabs['additional_information']); return $tabs; } // ========================================================================= // ADD ADDITIONAL INFORMATION AFTER ADD TO CART // ========================================================================= add_action( 'woocommerce_single_product_summary', 'additional_info_under_add_to_cart', 35 ); function additional_info_under_add_to_cart() { global $product; if ( $product && ( $product->has_attributes() || apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) { wc_display_product_attributes( $product ); } }
WooCommerce
How to show custom message based on shipping class on single product page
// ========================================================================= // 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 );
Disable payment method for specific country in WooCommerce
// ========================================================================= // DISABLE COD PAYMENT METHOD FOR SPECIFIC COUNTRY - DE // ========================================================================= add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country_de' ); function payment_gateway_disable_country_de( $available_gateways ) { if ( is_admin() ) return; if ( isset( $available_gateways['authorize'] ) && WC()->customer->get_billing_country() <> 'DE' ) { unset( $available_gateways['authorize'] ); } else { if ( isset( $available_gateways['cod'] ) && WC()->customer->get_billing_country() == 'DE' ) { unset( $available_gateways['cod'] ); } } return $available_gateways; }
Add fee to specific payment gateway only for physical products
// ========================================================================= // ADD FEE TO SPECIFIC PAYMENT GATEWAY => COD - ONLY FOR PHYSICAL PRODUCT // ========================================================================= // Part 1: assign fee add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway' ); function add_checkout_fee_for_gateway() { foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { // Check if there are non-virtual products if ( ! $cart_item['data']->is_virtual() ) { $chosen_gateway = WC()->session->chosen_payment_method; if ( $chosen_gateway == 'cod' ) { // Note: edit "Fee" and "5" below to control Label and Fee Amount WC()->cart->add_fee( __('Utánvét díja', 'woocommerce'), 510 ); } } } } // Part 2: reload checkout on payment gateway change add_action( 'woocommerce_review_order_before_payment', 'refresh_checkout_on_payment_methods_change' ); function refresh_checkout_on_payment_methods_change(){ ?> <script type="text/javascript"> (function($){ $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() { $('body').trigger('update_checkout'); }); })(jQuery); </script> <?php }
Add custom out of stock message for specific product category
// ========================================================================= // ADD CUSTOM MESSAGE IF SPECIFIC PRODUCT CATEGORY OUT OF STOCK // ========================================================================= add_filter( 'woocommerce_get_availability', 'woo_custom_get_availability', 1, 2); function woo_custom_get_availability( $availability, $_product ) { global $product; //CATEGORY 1 if ( has_term( 'CATEGORY_NAME_1', 'product_cat', $product->get_id() ) ) { // Change In Stock Text if ( $_product->is_in_stock() ) { $availability['availability'] = __('Available!', 'woocommerce'); } // Change Out of Stock Text if ( ! $_product->is_in_stock() ) { $availability['availability'] = __('Jelenleg nincs kitűzve tréning időpont', 'woocommerce'); } return $availability; } //CATEGORY 2 else if ( has_term( 'CATEGORY_NAME_2', 'product_cat', $product->get_id() ) ) { // Change In Stock Text if ( $_product->is_in_stock() ) { $availability['availability'] = __('Available!', 'woocommerce'); } // Change Out of Stock Text if ( ! $_product->is_in_stock() ) { $availability['availability'] = __('Jelenleg nincs kitűzve tréning időpont', 'woocommerce'); } return $availability; } else{ // Change In Stock Text if ( $_product->is_in_stock() ) { $availability['availability'] = __('Készleten', 'woocommerce'); } // Change Out of Stock Text if ( ! $_product->is_in_stock() ) { $availability['availability'] = __('Elfogyott', 'woocommerce'); } return $availability; } }
WooCommerce free shipping over amount and hide other shipping methods
https://flexibleshipping.com/woocommerce-free-shipping-over-amount/
Disable Payment Gateway For Specific Product Category
//DISABLE COD PAYMENT FOR SPECIFIC PRODUCT CATEGORY function unset_payment_cod_for_online_trening( $available_gateways ) { global $woocommerce; $unset = false; $category_ids = array( 109 ); foreach ( $woocommerce->cart->cart_contents as $key => $values ) { $terms = get_the_terms( $values['product_id'], 'product_cat' ); foreach ( $terms as $term ) { if ( in_array( $term->term_id, $category_ids ) ) { $unset = true; break; } } } if ( $unset == true ) unset( $available_gateways['cod'] ); return $available_gateways; } add_action( 'woocommerce_available_payment_gateways', 'unset_payment_cod_for_online_trening', 5 );
Disable Payment Gateway For Specific Shipping Method
<?php //Disable Payment Gateway For Specific Shipping Method add_filter( 'woocommerce_available_payment_gateways', 'wpflames_gateway_disable_shipping_368' ); function wpflames_gateway_disable_shipping_368( $available_gateways ) { global $woocommerce; if ( !is_admin() ) { $chosen_methods = WC()-&gt;session-&gt;get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0]; //Free shipping if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'free_shipping' ) ) { unset( $available_gateways['cod'] ); } //Flat rate if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) { unset( $available_gateways['cheque'] ); } //Local pickup if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) { unset( $available_gateways['cod'] ); } //Cheque if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'flat_rate:2' ) ) { unset( $available_gateways['cheque'] ); } } return $available_gateways; }
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
add_action( 'woocommerce_email_before_order_table', 'add_online_trening_welcome_email_text', 10, 2 ); function add_online_trening_welcome_email_text( $order, $sent_to_admin ) { if ( ! $sent_to_admin ) { foreach( $order->get_items() as $item ) { $product = wc_get_product( $item['product_id'] ); // Use the name or product ID here if( $product->get_id() == 2767 ) { echo '<p style="text-align: center">A befizetést követően 1 munkanapon belül e-mailben megkapod a leírást, hogyan tudod elérni az online videókat. A számlád postán érkezik. Köszönöm, hogy a módszeremet választottad. Köszönöm, hogy tenni akarsz magadért.</p>'; } } } }
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?
//APPLY COUPON AUTOMATICALLY IF PRODUCT ID IS IN THE CART add_action( 'woocommerce_before_cart', 'wpninja_apply_matched_coupons' ); function wpninja_apply_matched_coupons() { $coupon_code = 'sapikupon'; if ( WC()->cart->has_discount( $coupon_code ) ) return; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { // this is your product ID $autocoupon = array( 1035 ); if( in_array( $cart_item['product_id'], $autocoupon ) ) { WC()->cart->add_discount( $coupon_code ); wc_print_notices(); } } }
How to add Taxonomy Filter to WooCommerce admin
add_filter( 'woocommerce_product_filters', 'wpninja_filter_by_custom_taxonomy_dashboard_products' ); function wpninja_filter_by_custom_taxonomy_dashboard_products( $output ) { global $wp_query; $output .= wc_product_dropdown_categories( array( 'show_option_none' => 'Filter by product tag', 'taxonomy' => 'product_tag', 'name' => 'product_tag', 'selected' => isset( $wp_query->query_vars['product_tag'] ) ? $wp_query->query_vars['product_tag'] : '', ) ); return $output; }
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'); ?>