I’ve tried many currency plugins for WooCommerce, but none of them worked well. Some of them were not compatible with coupons, WooCommerce Subscriptions or poorly coded, so I decided to write a tiny WordPress plugin to change the price and currency based on the billing country. What it does? 1. If the billing country is […]
Category Archives: WooCommerce
Hide Uncategorized category from WooCommerce pages
Couldn’t find any working snippet in Google, so I decided to write it myself. This snippet will hide the Uncategorized category from front-end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function remove_uncategorized_category( $terms, $taxonomy, $query_vars, $term_query ) { if ( is_admin() ) return $terms; if ( $taxonomy[0] == 'product_cat' ) { foreach ( $terms as $k => $term ) { if ( $term->term_id == get_option( 'default_product_cat' ) ) { unset( $terms[$k] ); } } } return $terms; } add_filter( 'get_terms', 'remove_uncategorized_category', 10, 4 ); |
Allow backorders for certain products in WooCommerce
Using this code snippet you can allow backorders for certain products.
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 |
/** * Returns whether or not the product can be backordered. * * @param $backorders_allowed * @param $product_id * @param $product * @return bool */ function woocommerce_product_backorders_allowed( $backorders_allowed, $product_id, $product ) { return true; } /** * Return the stock status. Should be 'onbackorder' for backorders. * * @param $stock_status * @param $product * @return string */ function woocommerce_product_get_stock_status( $stock_status, $product ) { return 'onbackorder'; } add_filter( 'woocommerce_product_backorders_allowed', 'woocommerce_product_backorders_allowed', 10, 3 ); add_filter( 'woocommerce_product_get_stock_status', 'woocommerce_product_get_stock_status', 10, 2 ); |
How to add a custom field to WooCommerce checkout page
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 |
/* * Add a referanse field to the checkout. */ function prefix_checkout_add_referanse_field( $fields ) { $fields['billing']['billing_referanse'] = array( 'label' => __( 'Referanse', 'woocommerce' ), 'placeholder' => '', 'required' => false, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; } add_filter( 'woocommerce_checkout_fields' , 'prefix_checkout_add_referanse_field' ); /** * Display field referanse value on the order edit page. */ function prefix_order_display_referanse_field( $order ) { $referanse_meta_field = get_post_meta( $order->get_id(), '_billing_referanse', true ); if ( $referanse_meta_field ) echo '<p><strong>'. __('Referanse') .':</strong> ' . esc_attr( get_post_meta( $order->get_id(), '_billing_referanse', true ) ) . '</p>'; } add_action( 'woocommerce_admin_order_data_after_shipping_address', 'prefix_order_display_referanse_field', 10, 1 ); |
How to modify the WooCommerce API orders response?
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 |
/* * Add a referanse field to the Order API response. */ function prefix_wc_rest_prepare_order_object( $response, $object, $request ) { // Get the value $referanse_meta_field = ( $value = get_post_meta($object->get_id(), '_billing_referanse', true) ) ? $value : ''; $response->data['referanse'] = $referanse_meta_field; return $response; } add_filter( 'woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3 ); /* * Legacy API * Add a referanse field to the Order API response. */ function prefix_wc_api_order_response( $order_data ) { // Get the value $referanse_meta_field = ( $value = get_post_meta($order_data['id'], '_billing_referanse', true) ) ? $value : ''; $order_data['referanse'] = $referanse_meta_field; return $order_data; } add_filter( 'woocommerce_api_order_response', 'prefix_wc_api_order_response', 10, 1 ); |
How to modify the WordPress REST API posts response
The WordPress REST API is a great thing, but in most cases you need to extend it. For example, if you use Advanced Custom Fields and you need to get them via the API, then you need to modify the response. Here’s a quick example…
How to create a callback URL in WooCommerce
I was working on a payment gateway for WooCommerce and had to create a special URL that will then load the specified class method. Sometimes the information does not reach all parties e.g. if you close the browser and the status of the payment between the payment’s and merchant’s server remains unclear. The WooCommerce API […]
WooCommerce REST API – Import products from JSON
I had a task to create a PHP script to import simple and variable products from JSON file using the WooCommerce REST API. Thought it might be worth sharing with others because I couldn’t find much information about products import from JSON using the API.
How to add custom product sorting options in WooCommerce
By default, WooCommerce comes with 6 sorting options. In this example, I’ll add an extra option to sort by sale status. However, if your sorting needs are more advanced, then you can hook into pre_get_posts instead of woocommerce_get_catalog_ordering_args.