My 25 Best WooCommerce Snippets For WordPress Part 2

By WP Saviour •  Updated: 02/26/21 •  8 min read

Only a few days previously I wrote a publish the place I listed 25 of primarily probably the most useful WooCommerce snippets for wordPress I benefit from and in addition you had been many people to ask for further, so proper this second i decided to supply some further snippets for WooCommerce! Proper right here we go!

1 – Alternate WooCommerce default PayPal emblem


carry out paypal_checkout_icon() { return 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_betalen_met_paypal_nl.jpg';}
add_filter( 'woocommerce_paypal_icon', 'paypal_checkout_icon' );

2 – Alternate default product placeholder image


add_action( 'init', 'custom_fix_thumbnail' ); carry out custom_fix_thumbnail() { add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src'); carry out custom_woocommerce_placeholder_img_src( $src ) { $upload_dir = wp_upload_dir(); $uploads = untrailingslashit( $upload_dir['baseurl'] ); $src = $uploads . '/2012/07/thumb1.jpg'; return $src; }
}

3 – Take away “Merchandise” from breadcrumb


carry out woo_custom_filter_breadcrumbs_trail ( $path ) { foreach ( $path as $okay => $v ) { if ( strtolower( strip_tags( $v ) ) == 'merchandise' ) { unset( $path[$k] ); break; } } return $path;
} add_filter( 'woo_breadcrumbs_trail', 'woo_custom_filter_breadcrumbs_trail', 10 );

4 – Empty cart


carry out my_empty_cart(){ world $woocommerce; $woocommerce->cart->empty_cart(); }
add_action('init', 'my_empty_cart');

5 – Mechanically add product to cart on go to


carry out add_product_to_cart() { if ( ! is_admin() ) { world $woocommerce; $product_id = 64; $found = false; if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if ( $_product->id == $product_id ) $found = true; } if ( ! $found ) $woocommerce->cart->add_to_cart( $product_id ); } else { $woocommerce->cart->add_to_cart( $product_id ); } }
}
add_action( 'init', 'add_product_to_cart' );

6 – Add a custom-made foreign exchange / picture

add_filter( 'woocommerce_currencies', 'add_my_currency' ); carry out add_my_currency( $currencies ) { $currencies['ABC'] = __( 'Foreign exchange title', 'woocommerce' ); return $currencies;
} add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2); carry out add_my_currency_symbol( $currency_symbol, $foreign exchange ) { swap( $foreign exchange ) { case 'ABC': $currency_symbol = '$'; break; } return $currency_symbol;
}

7 – Change add to cart button textual content material


carry out woo_custom_cart_button_text() { return __('My Button Textual content material', 'woocommerce');
}
add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text'); carry out woo_archive_custom_cart_button_text() { return __( 'My Button Textual content material', 'woocommerce' );
}
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );

8 – Redirect subscription add to cart to checkout internet web page


carry out custom_add_to_cart_redirect( $url ) { $product_id = (int) $_REQUEST['add-to-cart']; if ( class_exists( 'WC_Subscriptions_Product' ) ) { if ( WC_Subscriptions_Product::is_subscription( $product_id ) ) { return get_permalink(get_option( 'woocommerce_checkout_page_id' ) ); } else return $url; } else return $url; }
add_filter('add_to_cart_redirect', 'custom_add_to_cart_redirect');

This snippet requires the Subscriptions plugin.

9 – Redirect to checkout internet web page after add to cart


carry out add_to_cart_checkout_redirect() { wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) ); die();
}
add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 11 );

10 – CC all emails

 carry out woo_cc_all_emails() { return 'Bcc: [email protected]' . "rn";
}
add_filter('woocommerce_email_headers', 'woo_cc_all_emails' );

11 – Ship an e mail when a model new order is achieved with coupons used

 carry out woo_email_order_coupons( $order_id ) { $order = new WC_Order( $order_id ); if( $order->get_used_coupons() ) { $to = '[email protected]'; $subject = 'New Order Completed'; $headers = 'From: My Title ' . "rn"; $message = 'A model new order has been achieved.n'; $message .= 'Order ID: '.$order_id.'n'; $message .= 'Coupons used:n'; foreach( $order->get_used_coupons() as $coupon) { $message .= $coupon.'n'; } @wp_mail( $to, $subject, $message, $headers ); }
}
add_action( 'woocommerce_thankyou', 'woo_email_order_coupons' );

12 – Change related merchandise amount

 carry out woo_related_products_limit() { world $product; $args = array( 'post_type' => 'product', 'no_found_rows' => 1, 'posts_per_page' => 6, 'ignore_sticky_posts' => 1, 'orderby' => $orderby, 'post__in' => $related, 'post__not_in' => array($product->id) ); return $args;
}
add_filter( 'woocommerce_related_products_args', 'woo_related_products_limit' );

13 – Exclude merchandise from a particular class on the shop internet web page

 carry out woo_custom_pre_get_posts_query( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; if ( ! is_admin() && is_shop() ) { $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'self-discipline' => 'slug', 'phrases' => array( 'footwear' ), 'operator' => 'NOT IN' ))); } remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); }
add_action( 'pre_get_posts', 'woo_custom_pre_get_posts_query' );

14 – Change retailer columns amount


carry out woo_product_columns_frontend() { world $woocommerce; $columns = 4; if ( is_product_category() ) : $columns = 4; endif; if ( is_product() ) : $columns = 2; endif; if ( is_checkout() ) : $columns = 4; endif; return $columns;
}
add_filter('loop_shop_columns', 'woo_product_columns_frontend');

15 – Disable WooCommerce tabs

 carry out woo_remove_product_tab($tabs) { unset( $tabs['description'] ); unset( $tabs['reviews'] ); unset( $tabs['additional_information'] ); return $tabs; }
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tab', 98);

16 – Take away breadcrumb

 remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);

17 – Prohibit supply worldwide places report


carry out woo_override_checkout_fields( $fields ) { $fields['shipping']['shipping_country'] = array( 'variety' => 'select', 'label' => __('My New Nation Report', 'woocommerce'), 'decisions' => array('AU' => 'Australia') ); return $fields; } add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

18 – Alternate “Free!” product string


carry out woo_my_custom_free_message() { return "This product is FREE!";
} add_filter('woocommerce_free_price_html', 'woo_my_custom_free_message');

19 – Conceal ALL totally different supply methods when Free Transport is accessible

add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 ); carry out hide_all_shipping_when_free_is_available( $available_methods ) { if( isset( $available_methods['free_shipping'] ) ) : $freeshipping = array(); $freeshipping = $available_methods['free_shipping']; unset( $available_methods ); $available_methods = array(); $available_methods[] = $freeshipping; endif; return $available_methods;
}

20 – Make checkout “state” self-discipline not required

 add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 10, 1 );
add_filter( 'woocommerce_shipping_fields', 'woo_filter_state_shipping', 10, 1 ); carry out woo_filter_state_billing( $address_fields ) { $address_fields['billing_state']['required'] = false; return $address_fields;
} carry out woo_filter_state_shipping( $address_fields ) { $address_fields['shipping_state']['required'] = false; return $address_fields;
}

21 – Create a coupon programatically

$coupon_code = 'UNIQUECODE';$amount = '10';$discount_type = 'fixed_cart'; $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon'
); $new_coupon_id = wp_insert_post( $coupon ); update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'certain' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

22 – Change e mail subject traces


add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2); carry out change_admin_email_subject( $subject, $order ) { world $woocommerce; $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $subject = sprintf( '[%s] New Purchaser Order (# %s) from Title %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name ); return $subject;
}

23 – Add custom-made worth to cart


carry out woo_add_cart_fee() { world $woocommerce; if ( is_cart() ) { $woocommerce->cart->add_fee( __('Personalized', 'woocommerce'), 5 ); } }
add_action( 'woocommerce_before_cart_table', 'woo_add_cart_fee' );

24 – Personalized added to cart message


add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' );
carry out custom_add_to_cart_message() { world $woocommerce; if (get_option('woocommerce_cart_redirect_after_add')=='certain') : $return_to = get_permalink(woocommerce_get_page_id('retailer')); $message = sprintf('%s %s', $return_to, __('Proceed Shopping for →', 'woocommerce'), __('Product effectively added to your cart.', 'woocommerce') ); else : $message = sprintf('%s %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product effectively added to your cart.', 'woocommerce') ); endif; return $message;
}

25 – Add price method to admin e mail


add_action( 'woocommerce_email_after_order_table', 'woo_add_payment_method_to_admin_new_order', 15, 2 ); carry out woo_add_payment_method_to_admin_new_order( $order, $is_admin_email ) { if ( $is_admin_email ) { echo '
Value Methodology: ' . $order->payment_method_title . '

'; } }

gp-5358176 as-5946549

WP Saviour

I am a WordPress specialist. My mission is to help you create beautiful websites with ease!