Showing posts with label WooCommerce. Show all posts
Showing posts with label WooCommerce. Show all posts

Sunday, 12 August 2018

Redirect to Checkout / custom page on Add to Cart button click – WooCommerce

There may be time when client would ask you to redirect user to checkout or cart page directly when users click on add to cart button.

For the similar situations WooCommerce provide predefined settings and filter hook which you can use to achieve the results.

To redirect the users, you can follow below steps:
1) I believe you have WordPress and WooCommerce already setup, now you can go to WooCommerce > Settings > Products tab.
2) Now look for "Add to cart behaviour" and there you will find the two settings: first is "Redirect to the cart page after successful addition" and second would be "Enable AJAX add to cart buttons on archives".
3) Now suppose if you want no redirection and want user to stay on the page without page reload, you can select the second checkbox "Enable AJAX add to cart buttons on archives" and you are done.
4) If you want users to redirect to cart page after user click on add to cart you can select the first option "Redirect to the cart page after successful addition" and you are done.
5) And if you have requirement to sent the user to checkout/custom page after user click on add to cart button, please uncheck both the options there follow the next step.
6) Now open your theme's functions.php file and copy/paste the below code.
function my_custom_add_to_cart_redirect( $url ) {  
 // for checkout page 
 $url = wc_get_checkout_url(); 
 
 // for custom/home page in our case.
 // $url = home_url(); 
 return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

Below is the video demonstration for above mentioned points.




Version at the time of demonstration:
1) WordPress: v4.9.8
2) Localserver: WampServer Version 2.5
3) WooCommerce: v3.4.4
4) Twenty Sixteen Theme: v1.5

Read more

Sunday, 24 September 2017

How to get the Average rating in WooCommmerce shop page?

Sometime there may a requirement where client can ask to show the average ratings for WooCommrce products on the shop page.

This can be achieve by making some customisation in the WooCommerce by following the below steps:
1) Create a folder name "woocommerce" in your active theme.
2) Now we need a file name "rating.php" which you need to copy from WooCommerce plugin (wp-content/plugins/woocommerce/templates/loop/rating.php); copy this file.
3) Create a loop folder inside the woocommerce folder in theme (Point number 1), paste in that folder and open this file in code editor.
4) Before starting to make any changes in file, go to your admin panel, click on WooCommerce on left side menu then click on "Status" (woocommerce>status) and at very bottom of the page there would be a section name "Templates", this section will show all files which are/which we have moved to theme woocommerce folder. please not if any file you have copy to woocommerce folder in theme is not showing there it means either you have included the non template files or have copy the file in wrong folder (which is not as per the guidelines of the WooCommerce).
5) Once file is verified in the status; then use the following code to show the "Average rating" in the same file.


echo $product->get_average_rating();


Save the file and go to your shop page.

Now you can style the same as per your own preference/theme design.



Version at the time of demonstration:
WordPress 4.8.2
WooCommerce 3.1.2



Read more

Friday, 14 July 2017

When user click on add to cart sent them directly to Cart page: WooCommerce

If you are using the WooCommerce plugin and looking for option when user as soon as click on "Add to Cart Button" the page should redirect to the Cart page.

You can achieve the same by selecting the following settings on the WooCommrce > settings > products > Display and in this page look for "Add to cart behaviour" and select "Redirect to the cart page after successful addition" option and save the settings and you are done.

Read more