Move cart.php and cart-totals.php to checkout page - php

To reduce the number of redirects on my site, i added the cart page on the same page as the checkout. and used this code to do it:
function cart_on_checkout_page_only() {
if ( is_wc_endpoint_url( 'order-received' ) ) return;
// add woocomerce cart shortcode on checkout page
echo do_shortcode('[woocommerce_cart]');
}
// Cart and Checkout same page
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page_only', 5 );
The problem is in the layout specifically, on devices it is too far from the order items as the order total is at the bottom of the page near the checkout button. I got a lot of complaints because people are too lazy to scroll down to see the coupon application in the cart at the bottom of the page
so I made the following modification:
I copied the woocommerce plugin files woocommerce/template/cart/cat.php and cart-totals.php
and pasted it into my theme in themes/mytheme/template/cart/custom-cart.php
and adapted it in the code above:
function cart_on_checkout_page_only() {
if ( is_wc_endpoint_url( 'order-received' ) ) return;
// add custom cart template on checkout page
get_template_part('template/cart/custom-cart');
}
// Cart and Checkout same page
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page_only', 5 );
my code did the job, but I believe there should be a simpler and more elegant way to make this kind of change. it's possible to make more simpler?
by the way I'm using a child storefront theme.

Related

Remove mini cart menu from sidebar and redirect to checkout page

I'm using theme called Blance on my site, but want to remove right sidebar mini cart widget which appears when you add some product to cart. I tried this:
Disabled "Redirect to the cart page after successful addition" and "Enable AJAX Buy Now buttons on archives" from WooCommerce settings.
After that added this function into functions.php file:
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 10, 1 );
function add_to_cart_checkout_redirection( $url ) {
return wc_get_checkout_url();
}
But seems that products still appears in the right sidebar cart after pressing Buy Now button. How to override that, and redirect to checkout directly?

How to remove woocommerce added cart items and redirect to checkout?

I have a Woocommerce form to 'Add Funds'. It has an amount input field ($20, $30 ...etc.) and a submit button that redirects to cart page with the input amount as total.
Redirect to checkout is working, but the cart items are not getting removed if a user abandons the cart and tries to order again.
I tried numerous solutions for the redirect to checkout, but only one worked.
Working solution for redirect to checkout:
WooCommerce - Skip cart page redirecting to checkout page
Solutions not working for redirect to checkout:
https://wordpress.stackexchange.com/questions/267071/redirect-to-woocommerce-checkout-after-adding-to-cart-item-already-in-cart
Woocommerce add to cart button redirect to checkout
N.B. I have added the working and not working solutions for redirect
to checkout because it may provide an insight as to why the empty cart
solutions are not working.
Incase of emptying cart before adding a new product, none of the solutions are working:
https://gist.github.com/viniciusrtf/b49403b5f87dcd7699c1
https://hungred.com/how-to/empty-woocommerce-cart-adding-item/
https://wordpress.stackexchange.com/questions/267071/redirect-to-woocommerce-checkout-after-adding-to-cart-item-already-in-cart
Using Woocommerce 3.2.6 and WordPress 4.9.2
First you will need in WooCommerce Settings > Products > Display in "Add to cart behaviour" to enabled the checkbox : Redirect to the cart page after successful addition
Then you will need the 3 following hooked function:
1) Empty cart before add-to-cart (if cart is not empty)
add_filter( 'woocommerce_add_to_cart_validation', 'one_cart_item_at_the_time', 10, 3 );
function one_cart_item_at_the_time( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty())
WC()->cart->empty_cart();
return $passed;
}
2) Add-to-cart redirection to checkout:
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 10, 1 );
function add_to_cart_checkout_redirection( $url ) {
return wc_get_checkout_url();
}
3) Skip cart page redirecting to checkout:
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
if( is_cart() )
wp_redirect( wc_get_checkout_url() );
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works (with Woocommerce 3.2.6 and WordPress 4.9.2 on Storefront theme).

change WooCommerce to a none commercial wish list

I designed a WooCommerce template for a customer. Now he wants to remove prices and visitors only add what they want to the list. So there will be no payment method. s:
- I can't change plugin or database. All data must remain how they are.
- I need to remove prices from all over the site. where ever they are. Cart, wishlist, single page & etc.
- I need to change payment method to something like submit list.
- at the end after submit list page there must be a contact info page.
please help.
You can hook into wc_price and remove the prices there
function my_filter_wc_price( $price ){
return '';
}
add_filter( 'wc_price', 'my_filter_wc_price' );
Then you can hook into parse_query and redirect the checkout page to the contact page.
function my_parse_query(){
if( is_page( wc_get_page_id( 'checkout' ) ) ){
wp_redirect( '/contact' );
exit;
}
}
add_action( 'parse_query', 'my_parse_query' );
That will stop people from being able purchasing things. Just hiding the price with CSS will not. They will still be able to craft URL's to add to cart, and find the cart / checkout pages.
since I found no other way I decided to do it width CSS and translation.
I removed all prices by display:none;
and I translated all the strings which are about shop and price.

Disabling cart page

I would like to disable the WooCommerce cart functionality by all means.
My shop has only a single product, so cart is doesn't really needed.
My desired flow is Click on buy button -> go to checkout page.
Incase user goes back and redo the same process, checkout page will not show 2 products in summary buy just 1.
Any tips on how to achieve this smoothly ?
Thanks,
You will Need 4 code snippets:
1) Disabling quantities buttons (on product page):
add_filter( 'woocommerce_is_sold_individually', '__return_true' );
2) Add-to-cart validation, allowing just one product in cart:
add_action( 'woocommerce_add_to_cart_validation', 'check_product_is_in_cart' );
function check_product_is_in_cart() {
WC()->cart->empty_cart();
return true;
}
3) Checkout redirect customer when your product is added to cart (with modern syntax):
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
// OR ALSO:
// return get_permalink(get_option('woocommerce_checkout_page_id'));
}
The code comme from this answer (with the correct new syntax): Woocommerce add to cart button redirect to checkout
4) Redirect Cart page to Checkout page (in case of):
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
function skip_cart_page_redirection_to_checkout() {
if(is_cart()){
wp_redirect(WC()->cart->get_checkout_url());
// OR ALSO:
// wp_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
exit; // This is mandatory with wp_redirect()
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
Disabling redirect to Cart on add-to-cart action and Ajax add-to-cart on Shop page and archives pages (optional)
You can also disable some settings in WooCommerce > Settings > Products > Display (tab).
Optionally keep that 2 options disabled (and save settings):
If you need to skip the cart page the easiest way it is to go to the Woocommerce->Settings->Checkout and set Cart Page to "Checkout" page
Or use this snippet
add_filter('add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
function themeprefix_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
Don't forget to disable Ajax Add to cart and Redirect to the cart page after successful addition in Woocommerce->Settings->Products->Display
If you use the first method you can check the Redirect to the cart page after successful addition.
For the limit one item in checkout use this snippet.
In this way, the customer only can buy one item, if the customer goes to another product and tries to buy it, the cart will be cleaned and the last item added.
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return $cart_item_data;
}

WooCommerce - Skip cart page redirecting to checkout page

Our website is kohsamuitour.net. I have added custom code to skip the cart page on checkout, which works for all sales. This code:
function wc_empty_cart_redirect_url() {
return 'https://www.kohsamuitour.net/all-tours/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
Now that does the job, but we also have a possibility to check booking availability. That can be found on the pages of private charters, i.e. this one: https://www.kohsamuitour.net/tours/kia-ora-catamaran/ .
Here the customer is being redirected to the cart, where I don't want that to happen as this is not a sale.
How can I make sure the 'Check booking availability' is also redirected to the checkout straight away?
You can skip cart definitively, redirecting customers to checkout page when cart url is called.
To achieve this use this code snippet, that should do the trick:
// Function that skip cart redirecting to checkout
function skip_cart_page_redirection_to_checkout() {
// If is cart page, redirect checkout.
if( is_cart() )
wp_redirect( WC()->cart->get_checkout_url() );
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.
Edit: Since WooCommerce 3 replace wp_redirect( WC()->cart->get_checkout_url() ); by:
wp_redirect( wc_get_checkout_url() );

Categories