Posted on 9 Comments

How To Change Add To Cart, View Products, Select Options Button Text In WooCommerce

How To Change Add To Cart, View Products, Select Options Button Text In WooCommerce 1

WooCommerce provides default text for their add to cart buttons. Here are the default text for all types of products in WooCommerce:

  1. Simple product – add to cart
  2. Variable product – select options
  3. Grouped product – View products

While those default text make sense, you often want to change it to something more attractive to boost your sales.

If you know PHP, you can easily change the text as you like. You can stop reading the post now. However, if you don’t know how to edit the WooCommerce functions, this post is for you.

Good news is, we are not going to mess with WooCommerce code today. That’s too risky. We are going to use a plugin that I created specifically for this purpose.

Let’s download the ultimate add to cart button customizer for WooCommerce plugin here

Now, we are going to change the WooCommerce button text for each case:

How to change add to cart text for simple product

Now, I assume that you have installed the plugin and activated it. If you click on Ultimate ATC Button->Configure buttons link on the left, you’ll see this interface:

ultimate add to cart configure buttons
ultimate add to cart configure buttons

Now you can see that at the top left section, there are three text boxes. If you want to change the add to cart text on simple products, simply add your desired text in the first box. For example, I’ll use the text “buy now”.

Make sure you scroll down and click on save changes.

Now, if I view the shop page or the product page of a simple product, I’ll see “Buy Now” instead of the default Add To Cart.

How To Change Add To Cart, View Products, Select Options Button Text In WooCommerce 2

It’s simple, isn’t it?

Well, now you can guess how we can change the text for variable and grouped products.

How to change “select options”, “view products” on variable and grouped products

At this point, you will not need more hand-holding. I’ll change the text for variable and grouped products as below:

How To Change Add To Cart, View Products, Select Options Button Text In WooCommerce 3

After clicking on save changes, I see the following results:

How To Change Add To Cart, View Products, Select Options Button Text In WooCommerce 4 How To Change Add To Cart, View Products, Select Options Button Text In WooCommerce 5

Conclusion

As you can see, changing the button text for your WooCommerce product can be very easy using the plugin. I hope you enjoy it. The plugin has a tons more options for you to fully customize your buttons.

 

 

Posted on Leave a comment

WooCommerce Developer Cheat Sheet

This post is for WordPress/WooCommerce developer only. If you have problem implementing what I’m going to show you, please drop me a message.

How to get WooCommerce attribute label

There is a function for this purpose:

wc_attribute_label($attribute_name)

For example, the attribute name is pa_color, your name for this attribute is Color. The code above outputs “Color”

 

How to get WooCommerce Add to cart form

So currently, I’m developing a product table plugin for woocommerce. Initially, I used custom HTML code for the add to cart button. However, since I have another plugin that add styles to the add to cart button, I would love to just get the add to cart form from WooCommerce and use the styling from the custom add to cart plugin.

So, the question is how to get the Add to cart form output by WooCommerce?

It turned out, you can use this code to get the content of the form and store it in a variable:

ob_start();
                if ($this->productType == 'variable')
                {
                    wc_get_template( 'single-product/add-to-cart/variable.php', array(
                        'available_variations' => $this->product->get_available_variations(),
                        'attributes'           => $this->product->get_variation_attributes(),
                        'selected_attributes'  => $this->product->get_default_attributes()
                    ) );

                } else
                {
                    wc_get_template( 'single-product/add-to-cart/simple.php', array(

                    ) );

                }


                $data = ob_get_contents();

                ob_get_clean();

 

How to send add to cart request via ajax call and update the cart

In shop page, you will see that WooCommerce support ajax add to cart. When the customers click on add to cart button, the product is added to cart and the cart (normally at the top of the page/navigation) get updated. How can you do that?

First, you need to send an ajax request to the current url and add this:

?wc-ajax=add_to_cart

Then the parameters are:

{
product_id: id,
product_sku: "",
quantity: x
}

If it is a variable product, you need to include attribute data:

{
product_id: id,
product_sku: "",
quantity: x,
attribute_1: attribute_1_value,
attribute_2: attribute_2_value
}

Make sure you replace attribute_1/attribute_2 with actual attribute name.

Then, send the ajax request. If your request pass WooCommerce validation, the product will be added to cart and you will get this response:

wc-ajax-response

As you can see, it passes all the HTML needed and also the selectors for us to update the cart. At this point, you need to update the cart by replacing the HTML content for each elements returned in fragments.

Here is the sample code I have on my plugin:

$.post(window.location.href + "?wc-ajax=add_to_cart" , {data object here}, function(response){

    
    //update the cart
    try {
        let data = (response);
        _.each(data.fragments, function(content, index){
           $(index).html(content);
        });

    } catch (e) {
        console.log(e);

    }
});

If you now jQuery, the code above should be familiar. I use underscore to loop through all items in fragments and update HTML content for each element returned by WooCommerce.

 

Posted on 1 Comment

How To Hide “Have a coupon? Click here to enter your code” On WooCommerce Checkout page

Coupon is a great way to make your customers happy. It is recommended that you use coupons when applicable. From my experience, having a coupon, even with a small discount can make the buying experience more pleasant. However, there are some stores, websites don’t use coupon. Take a consultant for example, she provides service at fix price per hour and normally, don’t offer discount. If she uses WooCommerce, on her checkout page, her customers see the message: “Have a coupon? Click here to enter your code” anyway. So, how can she remove that message?

have-coupon-message-on-checkout-page

How To Hide “Have a coupon? Click here to enter your code” on your checkout page

Some people may suggest you use CSS to hide the div that contains the message. While this method does the job, I would not recommend it. The reason is using CSS only hides the message, not remove it completely. In addition, WooCommerce has an option to disable that message, why don’t we use that?

So, to disable the message, you can go to WooCommerce->General and scroll down to the Enable Coupon section. You’ll see there is a checkbox there to let you enable and disable the use of coupons:

disable the use of coupons

As you can guess now, if you want to hide the message “Have a coupon? Click here to enter your code”, you should uncheck the checkbox “Enable the use of coupon codes”.

Next, scroll to the bottom and click on Save changes and you are done.

Conclusion

So, using coupons can be very effective for stores. However, you may not find it is applicable for your product. If you want to disable the use coupon on the checkout page, WooCommerce supports that by default and you can enable and disable it in WooCommerce settings.

Posted on 3 Comments

How To Get Product Type In WooCommerce

When developing plugins for WooCommerce, sometimes, knowing the product’s type is very important. The funny thing about WooCommerce is if you create a product from the class “WC_Product” and run:

$product->get_type();

The result is always simple.

This sounds confusing, doesn’t it? I feel the same way.

Get WooCommerce Product Type Using WC_Product_Factory

Since version 3.0, there is a convenient static method in WC_Product_Factory class that helps you get the type of product quickly:

WC_Product_Factory::get_product_type($product_id);

If the product is valid, you will get the product type as string (simple, variable…)

If the above method doesn’t work, let’s continue.

Getting product type using taxonomy

If you want the get_type method to return the correct type of the product, you need to create the product with the appropriate class. For example, for the get_type function to return ‘variable’, you need to create the product with the following syntax:

$product = new WC_Product_Variable($product_id);

However, how do you know what class to use when first creating the product?

The answer lies in the following code:

$terms = get_the_terms($this->product->get_id(), 'product_type');
$product_type = (!empty($terms)) ? sanitize_title(current($terms)->name) : 'simple';

As you can see from the code, it gets the taxonomy ‘product_type’ of the product and return the taxonomy name if exist, otherwise, it returns ‘simple’.

So, if you have the same problem, this is the code to use.

Posted on 1 Comment

How To Set Woocommerce Custom Shipping Cost Per Product

How To Set Woocommerce Custom Shipping Cost Per Product 6

The goal

Learn how to set custom shipping cost per product in WooCommerce.

The method

We are going to use WooCommerce shipping class to set different shipping cost per product.

What is a WooCommerce shipping class

This is the definition of shipping class on WooCommerce documentation. I think it’s clear enough:

Shipping classes can be used to group products of similar type and used by some shipping methods, such as Flat Rate Shipping, to provide different rates to different classes of product.

For example, for a specific shipping zone, when you select a shipping method, inside the shipping method, you can set many shipping classes. Each class can have different cost. This makes it very easy for us to set different shipping cost for each product.

How to set custom shipping cost per product using shipping classes

This is our setup: We have two products, a hat and a vase. A vase requires a more careful shipping method because it’s fragile while a hat can use less secure shipping method. Now, we are going to create two shipping classes:

  1. Fragile product shipping
  2. Normal product shipping

As you can guess, the vase will use the first class, the hat will use the second.

Add shipping classes

Let’s go to WooCommerce->Settings->Shipping class and add these two classes:

add new shipping class

Let click on the “Add shipping class” at the bottom right. You will see there are input boxes appear for you to enter the details. Don’t worry if you don’t see the place to enter shipping cost. We’ll do it later in shipping zones.

How To Set Woocommerce Custom Shipping Cost Per Product 7

Now click on Save Shipping classes and you are done.

Our next step is to create a zone and add shipping methods (if you haven’t got any).

Add shipping zone and shipping method

Let’s click on Shipping zones and click on Add shipping zone. You’ll see an interface appears to enter the zone details. In my example, I use Asia as my zone but you can use any other zones if you like.

add shipping zone

Now click on Add shipping method.

We are going to use the flat rate method.

Now, click on the edit link under the flat rate method:

How To Set Woocommerce Custom Shipping Cost Per Product 8

setting cost for shipping class

Here you can see that there are settings to enter Shipping class costs. In this example, I set 20 for the Fragile product shipping class and 5 for Normal product shipping.

Now, click on Save changes, click Save changes again on the add shipping zone screen and you are done with setting up shipping classes.

Set shipping class per product

Finally, we are going to set the shipping class for our products.

setting shipping class

Let’s go to the product data area (right under the product description area) and click on shipping. When you click on the “Shipping class” drop down, you’ll see the two options we’ve just created. Select Fragile product shipping for the vase and Normal product shipping for the hat.

Now, let’s add vase and hat to cart and go to the cart page. You’ll see the shipping cost is added accordingly:

shipping cost for vase
Shipping cost for vase

shipping cost for hat
Shipping cost for hat

shipping cost for both
shipping cost for both

Conclusion

As you can see, using shipping class can let us setup some flexible shipping for our product. There is no limit of number of shipping classes you can create so you can easily have many shipping options.