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: Code snippets
Simple AJAX posts filter by category and publish date for WordPress
The first thing you need to do is to write some HTML for category and publish date select box. This can be added to a custom template or your home page index.php file.
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 |
<div class="filter-wrap"> <div class="category"> <div class="field-title">Category</div> <?php $get_categories = get_categories(array('hide_empty' => 0)); ?> <select class="js-category"> <option value="all">All</option> <?php if ( $get_categories ) : foreach ( $get_categories as $cat ) : ?> <option value="<?php echo $cat->term_id; ?>"> <?php echo $cat->name; ?> </option> <?php endforeach; endif; ?> </select> </div> <div class="date"> <div class="field-title">Sort by</div> <select class="js-date"> <option value="new">Newest</option> <option value="old">Oldest</option> </select> </div> </div> |
Now, when we have HTML ready, we also need to show some latest blog posts.
1 2 3 4 5 6 7 8 9 10 11 |
<div class="filtered-posts"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content-post' ); endwhile; endif; ?> </div> |
Create a file called content-post.php […]
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 […]
DevSlider – A simple responsive slider built for Developers
There were many times when client asked me to customize the current slider template and add new settings to the slider. There are many great sliders in WordPress plugins repository, but they are not extendable – you can’t add new settings to the slider and change the template or it’s very hard. This time I decided to spend a bit more time and create my own slider which you can extend easily.
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.