Posted on 5 Comments

The Ultimate Guide to WooCommerce Flat rate Shipping

The Ultimate Guide to WooCommerce Flat rate Shipping 1

WooCommerce flat rate shipping is the easiest shipping method to set up. In this post, let’s find out how to setup flat rate shipping in WooCommerce and see how it fits to your business model.

What is flat rate shipping?

If you have sent a package via post office before, you should be familiar with flat rate shipping. As the name suggests, flat rate shipping impose an uniform shipping price on your package as long as it doesn’t exceed a certain weight or size.

For example, the shipping cost for packages less than 10 kg and 1 cubic meter is $10 per package.

The same idea applies to WooCommerce flat rate shipping.

How to setup flat rate shipping in WooCommerce

By default, WooCommerce has three shipping methods

  • flat rate shipping
  • local pickup
  • free shipping

In order to use one of those method, you need to create a shipping zone first then in the zone, you can add one or more shipping methods, including flat rate.

how to add flat rate shipping to shipping zone in woocommerce

 

After creating one WooCommerce flat rate shipping method as in the above image, you can configure the rate, tax of that shipping method by clicking on the Edit link under “Flat rate”:

configure flat shipping rate and tax

 

As you can notice, there is a toggle button right below the Enabled title. You can enable or disable the shipping method by turn that button on or off.

Can I have multiple flat rate shipping setup?

There is no limit on number of shipping method per zone so you definitely can have multiple flat rate shipping setup, in one zone. You may wonder, why do I need multiple flat rate shipping for one zone?

One good example is you provide standard shipping at a lower rate and priority shipping at higher rate. At the cart page, customers can choose which shipping method they want to use. If they need the products to be delivered fast, they will choose the priority shipping method. Otherwise, they can choose the standard shipping method to save money on shipping.

Let’s set that up:

adding multiple flat rate shipping for one zone

Now, on the cart page, the customers will see all the shipping options available:

flat rate shipping options on cart page

How to add flat rate shipping per item using shipping classes

At this point, you should be comfortable with adding flat rate that applies to all items. What if you want to set a different shipping rate for some particular items? For example, I have a store selling computer hardware. While keyboards, mice can be shipped without much care, monitor requires special packaging and handling. I would like to charge $50 more for monitors. How can I set that up?

First we need to add a shipping class. Adding shipping class in WooCommerce is simple. Let’s go to WooCommerce->Settings->Shipping->Shipping classes and add one:

add a shipping class in woocommerce

Now, the next step would be to go the monitor and assign it the “monitor” shipping class:

assign shipping class to product

Finally, let’s configure the shipping rate for “monitor” shipping class in our shipping methods.

Let’s go to WooCommerce->Settings->Shipping->Shipping zones and edit our zones. Let edit all the enabled flat rate shipping method in that zone and add the additional fee accordingly:

add shipping cost to shipping class

 

Now if I go to the cart, I can see that the shipping cost is higher:

shipping cost updated after using shipping classes

If you remember, the standard shipping was $10 and the priority shipping was $30. Since we added $30 for the “monitor” shipping class, the shipping cost updated accordingly.

WooCommerce flat rate shipping by quantity

If you notice that we have 2 items in cart here. The shipping cost for the “monitor” only calculated once. What if you want to charge the additional shipping cost PER ITEM?

That’s easy, let’s go back to our shipping methods and edit each of them then enter this:

using formula to calculate flat rate shipping cost

Now, if I go to the cart page, surely I’ll see the shipping cost is calculated for all items in cart:

cart udpated after using formula

Hide flat rate shipping if free shipping is enabled

You may want to reward the customers by offering free shipping to all order exceeds $300. In this case, you also want to hide other flat rate shipping methods. What can you do to achieve this?

You’ll need to enter some code. Don’t worry, it won’t be hard.

First thing first, you need to create a child theme if you haven’t got one. Why do you need a child theme? How to create one? Find all the answers here

Now you have your child theme ready. Let’s go to its functions.php file and paste the following code:

function bc_hide_shipping_when_free_is_available( $rates ) {
  $free = array();
  foreach ( $rates as $rate_id => $rate ) {
    if ( 'free_shipping' === $rate->method_id ) {
      $free[ $rate_id ] = $rate;
      break;
    }
  }
  return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'bc_hide_shipping_when_free_is_available', 100 );
add code to functions.php to hide other shipping methods when free shipping available

Click on update file and let’s go to the cart page, you’ll see that if free shipping is available, all other methods are hidden:

other shipping methods hidden when free shipping available

Conclusion

As you can see, flat rate shipping at first seems to be rigid and hard to customize. However, when combine with shipping classes and free shipping method, you can offer very flexible shipping options for your customers. Using WooCommerce shipping classes enables you to set the max shipping fee for an order, set the shipping fee based on the quantity and the total of the cart.

Posted on 3 Comments

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 2

Working with WooCommerce brings me a lot of joy. If you are a developer, I hope the experience I’m sharing below will be helpful to you.

How to add a simple product to cart using ajax

Adding a simple product via ajax is very simple. From any page, you send the following request, via POST or GET:

{
add-to-cart: product_id,
quantity: x
}

So, for example, I have my product here which has ID 30:

woocommerce-simple-product

Now, if I want to add this product to cart, I simply append the following data to any URL of my website:

?add-to-cart=30&quantity=2

In this example, I’m going to add two products to cart.

For example, I append the string above to the cart url, which makes the full URL like so:

http://localhost/wordpress/cart/?add-to-cart=30&quantity=2

If I open that URL in my browser, I will be redirected to cart page and the product will be in cart.

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 3

Cool, isn’t it 🙂

Now, simple product is simple. How about variable products? I requires a bit more work.

 

How to add a variable product to cart using ajax

With variable product, you need to find the variation ID to add the exact product you need to cart. If you use the parent product ID, you may see an error, which is not surprised at all. Let me show you.

I have this product:

woocommerce variable product

As you can tell from the product page, it’s a variable product (select boxes to select the variations, price in range).

Now, the product id is 31. If I execute the following URL:

http://localhost/wordpress/cart/?add-to-cart=31&quantity=2

You’ll see this error:

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 4

It is not hard to understand, right? Since the product is a variable product, you will need to find the variation ID.

Where to find the variation ID? Let’s do an inspection, shall we?

If you right click on one of the select boxes (to select variation) and click on inspect (on Chrome), you’ll see this:

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 5

As you can see, our select box is inside a form. That form has an attribute called data-product_variations . This attribute contains the JSON data of all variations of our product. Let’s extract the data using jQuery.

extract variable product data in woocommerce

as you can see that, I select the form with the selector .variations_form.cartif you know jQuery or CSS selector, this part shouldn’t be strange to you.

I was able to extract the JSON string from the attribute data-product_variations and turned that into javascript object using JSON.parse.

Now, you can see that the data is an array contains 4 elements. Since my product has two attributes “Color” and “Size”, it’s not surprising that there are 4 variations.

Let’s expand one of the 4 variations and see what we have:

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 6

There are a tons of data and surely you’ll see the variation_id is 150.

That’s all we need to add this product to cart.

Now, let’s navigate to this URL:

http://localhost/wordpress/cart/?add-to-cart=150&quantity=2

Sure enough, the product is added to cart.

successfully add variable product to cart using custom URL

Interesting, right?

Now, you are able to add simple and variation products to cart, which is great. However, if you notice the cart at the navigation bar. It’s not updating if you send the requests via ajax. How can you fix that?

How to add products to cart and update the cart widget(at the navigation bar)

To add the products to cart and then update the cart widget, you’ll need a different approach. At least, you need a different URL to send the request to.

Are you ready?

You still can send the request to any URL of your site, however, this time, you need a new parameter:

?wc-ajax=add_to_cart

Let’s open the browser’s console and enter the following data:

jQuery.post('http://localhost/wordpress/cart/?wc-ajax=add_to_cart', {product_id: 150, quantity: 10}, function(response){ console.log(response); })

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 7

We are going to print out the response from the server. It contains some very useful data that let us update the cart.

Now, hit enter then wait a second and this is what we got back:

The Ultimate Guide To Adding Products To Cart Using Ajax In WooCommerce 8

The most important piece of data is the fragments. It is an array of objects. The key of each object is the css selector of cart-related element and the value of that key is the HTML content of that element.

Updating the cart now should be trivial. Consider it’s your homework 😉

Conclusion

As you can see, adding WooCommerce products via ajax is quite simple. With the knowledge you gain from this post (hopefully), I hope you can use it and make some awesome applications.

 

 

 

Posted on 5 Comments

How To Add Custom CSS/Javascript In Elementor Free Version

How To Add Custom CSS/Javascript In Elementor Free Version 9

If you are using the free version of Elementor, you’ll notice that adding custom css to the page isn’t supported. Elementor says that it’s a feature of the pro version.

custom css in elementor free version
custom css in elementor free version

What can you do? Are you going to upgrade? Well, if you can, I recommend you to do so. It’s worth the price.

However, if you just need to add some CSS and not quite ready to upgrade yet, let me show you a walk around.

Adding custom CSS in Elementor free version

Now, let’s find the HTML element and drag it into your page:

How To Add Custom CSS/Javascript In Elementor Free Version 10

Now, you’ll see the HTML on your page look like this:

How To Add Custom CSS/Javascript In Elementor Free Version 11

It doesn’t look very attractive. However, you don’t need to worry about that. It will not show up on your final page (the page that your visitors see).

Now, let’s hover your cursor over the HTML element, you will see the edit icon appears:

How To Add Custom CSS/Javascript In Elementor Free Version 12

Then, you’ll see the HTML box appears on the left of your screen:

How To Add Custom CSS/Javascript In Elementor Free Version 13

While it says HTML code, there is nothing prevent us to enter CSS code 😉

Let’s enter some CSS code:

<style>
body p {
    color: red;
}
    
</style>

And you’ll see, my page has red text:

How To Add Custom CSS/Javascript In Elementor Free Version 14

Now, update your page and see the changes.

Conclusion

Some of you may say that, putting style tag inside the body is not recommended. Well, that’s a thing of the past. HTML5 is finally allow the style tag in the body. If you don’t mind reading length specification page, here you go.

You don’t limit to put just css in the HTML box. You can put HTML (of course) and also javascript code. This enable some very interesting feature that you cannot do with the default elements provided by Elementor.

Posted on 2 Comments

Finally A Browser Without Cache – Web Developer Dream!

Finally A Browser Without Cache - Web Developer Dream! 15

If you are reading this post (not by accident), I can safely assume that you share with me the frustration with browser cache. For normal users, browser cache is great since it speeds up website loading, save bandwidth…

But for web developers, it’s something different:

Finally A Browser Without Cache - Web Developer Dream! 16

Well, imagine you have changed some CSS to your button, you reload your browser, nothing changed!

Despite the fact that you have:

  • Open developer tools, check clear browser cache
  • Installed the best cache browsing addon/extensions you can find and clicked the clear cache button hundred times

Still, your new style doesn’t show.

Browser cache is to blame.

If you feel the pain, read on.

I understand why browser makers implement caching. But, I wondered if there was a browser that doesn’t  have cache so I don’t have to spend hours working on my code, wonder why it isn’t working only to realize that there is nothing wrong with my code but browser’s cache?

The browser without cache didn’t exist, until now

I tried to find that browser but it didn’t exists (during my search). So, accidentally, I think of selenium. It’s a browser automation tool that let us do a lot of cool things with browsers. (SEO guys are more familiar with this concept). It turned out, selenium is the solution to my problem.

I ended up creating a small javafx app to let me spawn browsers without cache to load my development websites.

And it worked perfectly.

Let me show you how it works.

How does the browser without cache work

This is the application:

Main interface

Finally A Browser Without Cache - Web Developer Dream! 17

Settings interface

Finally A Browser Without Cache - Web Developer Dream! 18

As you can see that, on the main interface, you can enter your URL, select the browser you want to spawn and click on start browser. You have two choices of browser, Chrome or Firefox.

On the settings panel, you need to set the path to Chrome driver and Firefox driver. What are they? You can download Chrome driver here:

Download Chrome driver

And Gecko driver (Firefox) here:

Download gecko driver

Make sure you select the right version for your OS.

The favorite URL box is where you can enter list of URLs that you want to appear on the box on the left. Later, you can just double click on those URLs to open the browser.

Where to download the app

You can download the app here:

Download no browser cache app

If you want to develop further, feel free to get the source code here:

https://github.com/datmt/NO-FUCKING-CACHE-BROWSER

Questions and suggestions are welcome.

Posted on 3 Comments

How To Add Extra Text To WooCommerce Price Field Without Using A Plugin

woocommerce customize price field

By default, WooCommerce only shows the price of the product on the product page (a long with the currency symbol). In most case, this info is enough. However, there are cases you want to add some extra details to the price field. For example in the case below, you want to add “per kg” after the price:

woocommerce add extra text to price field

If you think that you can enter text into the price field of the product, you will be surprised to find out that is not possible. We can only input number in the price field of the product. This is for a good reason.

So, what can you do?

The easy way to add extra HTML content to WooCommerce price field, without using a plugin

Let me show you how to achieve this. First, you will need to create a child theme. If you don’t know how, read my post on how to create child theme here. It’s short and to the point.

Why do you need a child theme? It’s because we are going to add some custom code and using child theme is the best way to add additional code to your WordPress website.

Now, let’s go to Appearance->Theme Editor and select the functions.php file of your child theme. Make sure it’s the active theme.

Here is the code we are going to add at the end of the file:

add_filter( 'woocommerce_get_price_html', 'binary_carpenter_add_extra_price_content', 10, 2 );
add_filter( 'woocommerce_get_variation_price_html', 'binary_carpenter_add_extra_price_content', 10, 2 );

function binary_carpenter_add_extra_price_content($price, $product)
{
  return ($price) . " per kg";	
}

Or, what is it look like on my site:

adding custom code to add extra content to woocommerce price field

And that’s done! That’s how I added the “per kg” text after the price. Make sure you don’t forget the dot (.). Without it, the code will not work.

As you can see, you can add the text before or after the price. It’s all depend on you.

Conclusion

This is a quick method to add additional text to the price field on your WooCommerce product page. This method is suitable for a quick fix or a store that have few products, or that all the products share a common way to calculate the price. The text you add here will appear on EVERY product page so be careful when you use this method.

If you want a per product approach, it’s probably best to use a plugin. If you need for advanced functions that the plugin I mentioned doesn’t cover, please let me know.

Thanks for your interest in the post. Have a good day!