[Tutorials] How to merge shopping cart and checkout pages

Huỷ thiết lập trang giỏ hàng trong cài đặt woocommerce

[Tutorials] How to merge shopping cart and checkout pages

Reasons for merging cart and checkout pages in woocommerce:

  • Website has few products or products of great value, customers only buy small quantity.
  • Simplify the ordering process for customers
  • Remove unnecessary fields that customers do not know

Step 1: Display the cart table above the checkout form in the checkout page

Copy and paste the following code into the functions.php file

add_action( 'woocommerce_before_checkout_form', 'bbloomer_cart_on_checkout_page_only', 5 );

function bbloomer_cart_on_checkout_page_only() {

if ( is_wc_endpoint_url( 'order-received' ) ) return;

echo do_shortcode('[woocommerce_cart]');

}
How to merge shopping cart and checkout pages
How to merge shopping cart and checkout pages

Step 2: Cancel setting up cart page in woocommerce settings

Go to Woocommerce > Setting > Advanced and remove the cart page in the Cart Page

Cancel setting up cart page in woocommerce settings
Cancel setting up cart page in woocommerce settings

Step 3: Remove the shopping cart page completely

You can go to Pages > All Pages, find the shopping cart page and delete it because it’s no longer needed

Step 4: Redirect to checkout page when cart is empty

In case you don’t want to show an empty checkout page if the user goes directly to this page or when the cart is empty. You can redirect this page to the homepage or products, or anywhere you want.

Copy and paste the following code into the functions.php file

add_action( 'template_redirect', 'bbloomer_redirect_empty_cart_checkout_to_home' );

function bbloomer_redirect_empty_cart_checkout_to_home() {
if ( is_cart() && is_checkout() && 0 == WC()->cart->get_cart_contents_count() && ! is_wc_endpoint_url( 'order-pay' ) && ! is_wc_endpoint_url( 'order-received' ) ) {
wp_safe_redirect( home_url() );
exit;
}
}

Step 5: Optimize the order form on the checkout page

For the default Woocommerce checkout page, the form has a lot of unnecessary fields. You can optimize these fields by using Plugin Checkout Field Editor (Checkout Manager) for WooCommerce

Or you can use the following code to put it in the file functions.php

// Remove unnecessary fields in the checkout page
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_last_name']); 
unset($fields['billing']['billing_address_2']); 
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_country']); 
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']); 
return $fields;
}

// Add a comment to the input box
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'Họ và tên';
$fields['billing']['billing_email']['placeholder'] = 'Email';
$fields['billing']['billing_address_1']['placeholder'] = 'Địa chỉ giao hàng';
$fields['billing']['billing_phone']['placeholder'] = 'Số điện thoại';
return $fields;
}

Good luck!

Trả lời

Email của bạn sẽ không được hiển thị công khai.