How to Make WooCommerce Refresh its cart-contents Widget

Posted by on Jul 15, 2016 in WooCommerce | 0 comments

I am not going to start with pointless intro about WooCommerce and how many sites WooCommerce powers. If you're reading this post you know for sure what it does so I will just dive in. In a recent update of our WooCommerce plugin which allows bulk product adding we've added Ajax add to cart so the clients can stay on the same page in case they want to add more products a few seconds later. This has to be enabled from the plugin's Settings which can be found in WP Admin > Settings > Orbisius Quick Order for WooCommerce. One of our customers & also great beta tester Annie (Hi Annie) noticed that the WooCommerce cart-contents widget wasn't refreshing i.e. the user adds one or more products to the shopping cart, the page correctly doesn't refresh but the widget that shows the current total didn't change at all. Same thing for mobile view as well. This was happening with the Storefront theme. I started digging into WooCommerce & Storefront theme and found the piece of code that was responsible for triggering and refreshing the so called fragments. WooCommerce makes an Ajax call and receives a JSON back . The JSON nicely includes with different fields that contain HTML code that can be used to replace different sections on the page. A theme or a plugin can/will handle the add to cart and make some (visual) changes. The code that reloads those fragments can be found here: \plugins\woocommerce\assets\js\frontend\cart-fragments.js To refresh WooCommerce widget you have to copy the code below var $fragment_refresh = { url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ), type: 'POST', success: function( data ) { if ( data && data.fragments ) { $.each( data.fragments, function( key, value ) { $( key ).replaceWith( value ); }); if ( $supports_html5_storage ) { sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) ); set_cart_hash( data.cart_hash ); if ( data.cart_hash ) { set_cart_creation_timestamp(); } } $( document.body ).trigger( 'wc_fragments_refreshed' ); } } }; ... and to trigger the event you do this: $.ajax( $fragment_refresh ); or jQuery.ajax( $fragment_refresh ); Note: if you have this snippet code in php you may have to rename $fragment_refresh to my_plugin_fragment_refresh so the dollar sign $ doesn't get interpreted as php variable. Ideal way It would have been awesome if we can trigger the fragments to reload with a simple custom jQuery event and not to have to copy and paste code above. $( document.body ).trigger( 'wc_reload_fragments' ); or $( document.body ).trigger( 'wc_force_reload_fragments'...

Read More

How to Programmatically Create/Update a Shipping Zone in WooCommerce

Posted by on Jul 7, 2016 in WooCommerce | 0 comments

With WooCommerce 2.6 you can have different shipping zones, File that is responsible for zone creation: /woocommerce/includes/class-wc-shipping-zone.php Here's how to create a shipping zone with php. Make sure the code is executed on or after init action to be sure that all the plugins are fully loaded. $zone_data = new stdClass(); $zone_data->zone_name = 'New Zone Name'; //$zone_data->zone_id = 12; // uncomment for update $zone_obj = new WC_Shipping_Zone( $zone_data ); $zone_obj->save(); $zone_id =...

Read More

How to Translate WooCommerce into Your Language

Posted by on May 25, 2016 in WooCommerce | 0 comments

There's tons of articles explaining what WooCommerce is and how much its market share is so I am going straight to get to the point. It seems after a certain version WooCommerce team has decided not to ship language packs with the plugin. You have been probably wondering why your site is in different language and WooCommerce keeps showing its screens in English i.e. ignoring the WPLANG (constant or settings option). The benefit of shipping the language packs separately is that WooCommerce will take less space and will be quicker to install and update. The language packs can be downloaded from the link below. After you download the selected language pack you need to unzip it into the following directory. Do make sure that the unzipped files are in that folder and not in a subfolder. YOUR_WP_DIR/wp-content/languages/plugins/ For example if you want to switch to the Bulgarian language here is how the files in the folder should look like i.e. not in a subfolder. YOUR_WP_DIR/wp-content/languages/plugins/ YOUR_WP_DIR/wp-content/languages/plugins/woocommerce-admin-bg_BG.po YOUR_WP_DIR/wp-content/languages/plugins/woocommerce-bg_BG.po YOUR_WP_DIR/wp-content/languages/plugins/   If you haven't changed the default WordPress language you can do so by going to: WP Admin > Settings > General > Site Language and pick the language from the dropdown menu. Here is a video how to do the steps.   Common issues / questions. woocommerce admin is still in English woocommerce fix WPLANG woocommerce ignores WPLANG woocommerce ignores $locale define('WPLANG', 'SOME_LANGUAGE'); - this probably no longer works. Can you set up a test/staging WordPress sites in seconds? Yes, with qSandbox you...

Read More

How to Get the Expiration Days of a Virtual and Downloadable WooCommerce Product Programmatically

Posted by on Sep 9, 2015 in WooCommerce | 0 comments

WooCommerce allows you to define download expiration (in days) e.g. how many days after an order is made the user is allowed to download the purchased product's file(s). This is a good mechanism to prevent people from downloading the file long after the order is made. If you need to get the download expiration days you can use the following snippet. $product_id = 123; $limit      = trim( get_post_meta( $product_id, '_download_expiry', true ) ); This snippet is from function: wc_downloadable_file_permission found in file:...

Read More

How to List Recently Completed WooCommerce Orders

Posted by on Oct 27, 2014 in WooCommerce | 0 comments

WooCommerce is a cool e-commerce plugin and powers many sites. Here is how to retrieve some of the recent WooCommerce orders. You can pull orders from today, last 7 days, last 30 days, current month or last year. // today $after_date = date( 'Y-m-d', strtotime('now') ); // Last 7 days $after_date = date( 'Y-m-d', strtotime('-7 days') ); // Last 30 days $after_date = date( 'Y-m-d', strtotime('-30 days') ); // This month $after_date = date( 'Y-m-01', strtotime('now') ); // This year $after_date = date( 'Y-01-01', strtotime('now') ); // 0 today $args = array( 'post_type' => 'shop_order', 'post_status' => 'publish', 'posts_per_page' => 250, // or -1 for all 'tax_query' => array( array( 'taxonomy' => 'shop_order_status', 'field' => 'slug', 'terms' => array('completed') ), ), ); $args['date_query'] = array( 'after' => $after_date, //'2012-04-01', 'inclusive' => true, ); $orders = get_posts($args); The result looks like this. array ( 0 => WP_Post::__set_state(array( 'ID' => 104, 'post_author' => '1', 'post_date' => '2014-10-12 02:06:00', 'post_date_gmt' => '2014-10-12 02:06:00', 'post_content' => '', 'post_title' => 'Order – October 12, 2014 @ 02:07 AM', 'post_excerpt' => '', 'post_status' => 'wc-completed', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_password' => '', 'post_name' => 'order-october-12-2014-0207-am', 'to_ping' => '', 'pinged' => '', 'post_modified' => '2014-10-12 02:07:32', 'post_modified_gmt' => '2014-10-12 02:07:32', 'post_content_filtered' => '', 'post_parent' => 0, 'guid' => '', 'menu_order' => 0, 'post_type' => 'shop_order', 'post_mime_type' => '', 'comment_count' => '1', 'filter' => 'raw', )), ) Related: Can you set up a test/staging WordPress sites in seconds? Yes, with qSandbox you...

Read More