WooCommerce is fast becoming the most popular e-commerce software on the web. We’ve started getting into it in a big way, offering training days and where you can learn more about it. To help those we can’t train face to face, we decided to put together a bumper resource.
Hide products that are packages or subscription which belong to a page or has its own page by adding this into your functions.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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() ) { $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the membership category on the shop page . For multiple category , separate it with comma. 'operator' => 'NOT IN' ))); } remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); } |
Customise the number of rows and the number of products in each row with this little snippet. Change the number of related products displayed within your WooCommerce Shop by adding the following code to the end of your functions.php (Important: “?>” should be the last bit of code within functions.php)
1 2 3 4 |
// Redefine woocommerce_output_related_products() function woocommerce_output_related_products() { woocommerce_related_products(4,2); // Display 4 products in rows of 2 } |
Add the code into custom.css.
1 |
.count {display:none !important} |
(adsbygoogle = window.adsbygoogle || []).push({}); By default, virtual-downloadable orders are marked as ‘completed’ after successful payment, but if you want to be able to automatically mark a virtual order as complete upon payment (for instance in the case of a site which takes donations where no further action is required) you can use the following code in your functions.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
add_filter( 'woocommerce_payment_complete_order_status', 'virtual_order_payment_complete_order_status', 10, 2 ); function virtual_order_payment_complete_order_status( $order_status, $order_id ) { $order = new WC_Order( $order_id ); if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) { $virtual_order = null; if ( count( $order->get_items() ) > 0 ) { foreach( $order->get_items() as $item ) { if ( 'line_item' == $item['type'] ) { $_product = $order->get_product_from_item( $item ); if ( ! $_product->is_virtual() ) { // once we've found one non-virtual product we know we're done, break out of the loop $virtual_order = false; break; } else { $virtual_order = true; } } } } // virtual order, mark as completed if ( $virtual_order ) { return 'completed'; } } // non-virtual order, return original status return $order_status; } |
Add the following to your functions.php file replacing ‘my button text’ with whatever you want it to say.
1 2 3 4 5 6 7 8 |
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1 add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 + function woo_custom_cart_button_text() { return __( 'My Button Text', 'woocommerce' ); } |
6.- Change the ‘add to cart’ text on product archives
Add bellow code in your functions.php file.
1 2 3 4 5 6 7 8 |
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1 add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 + function woo_custom_cart_button_text() { return __( 'My Button Text', 'woocommerce' ); } |
Use this snippet to simply disable the form on the cart/checkout pages, but leave coupon use enabled otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // hide coupon form everywhere function hide_coupon_field( $enabled ) { if ( is_cart() || is_checkout() ) { $enabled = false; } return $enabled; } add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' ); ?> |
Add this in your theme/child themes functions.php file replacing the web address with the URL of your choice.
1 2 3 4 |
add_filter( 'woocommerce_breadcrumb_home_url', 'woo_custom_breadrumb_home_url' ); function woo_custom_breadrumb_home_url() { return 'http://woothemes.com'; } |
Copy and paste this code in your theme functions.php file and swap out the currency code and symbol with your own.
1 2 3 4 5 6 7 8 9 10 11 12 |
add_filter( 'woocommerce_currencies', 'add_my_currency' ); function add_my_currency( $currencies ) { $currencies['ABC'] = __( 'Currency name', 'woocommerce' ); return $currencies; } add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2); function add_my_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case 'ABC': $currency_symbol = '$'; break; } return $currency_symbol; } |
Set a custom to add to cart URL to redirect to by adding the following code to the file functions.php.
1 2 3 4 5 |
add_filter('add_to_cart_redirect', 'custom_add_to_cart_redirect'); function custom_add_to_cart_redirect() { return get_permalink(get_option('woocommerce_checkout_page_id')); // Replace with the url of your choosing } |
Use the below code in your functions.php file from current activated theme folder. It will display the product’s weight on the product archive page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php add_action( 'woocommerce_after_shop_loop_item', 'rs_show_weights', 9 ); function rs_show_weights() { global $product; $weight = $product->get_weight(); if ( $product->has_weight() ) { echo '<div class="product-meta"><span class="product-meta-label">Weight: </span>' . $weight . get_option('woocommerce_weight_unit') . '</div></br>'; } } ?> |
If you still need any help, feel free to add your comments in below comment section. I will be happy to help. Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool WordPress tutorials and also don’t forget to subscribe my blog to get all future tutorials directly in your mail box.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: how to change the add to cart text, how to Hide the coupon form, how to mark your order automatically as complete, Web development, wordpress