How To Skip Cart Page And Add On The Checkout Page

Here we learn about How to skip the cart page and add on the checkout page.

Custom code copied and paste into your child theme’s functions.php file.

1. Remove add to cart button on product archive (shop) and single product page for each product.

<?php
function remove_add_to_cart_button(){
  remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30 );
}
add_action('init','remove_add_to_cart_button');
?>

The following action:

init(initialize) – Fires after WordPress has finished loading but before any headers are sent.

remove_action – This Hook Removes a function from a specified action hook.

add_action – This Hooks a function on to a specific action.

syntax :

<?php add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 ); ?>

The following parameter:

$tag – (string)(Required) Name of the action to which function is hooked.

$function_to_add – (callable)(Required) The function you wish to be called.

$priority – (int)(Optional) It used for specify the order and particular action are executed.

$accepted_args – (int)(Optional) The function accepts with number of arguments.

2. Add a new button that links to the checkout page for each product.

<?php
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart'); // Shop page 
add_action('woocommerce_single_product_summary','replace_add_to_cart'); // Product Details Page
function replace_add_to_cart() {
  global $product;
  $link = $product->get_permalink();
  
  // get the current post/product ID
  $current_product_id = $product->id;

  // get the product based on the ID
  $product = wc_get_product( $current_product_id );

  // get the "Checkout Page" URL
  $checkout_url = WC()->cart->get_checkout_url();
  
  echo do_shortcode( '<a href="'. $checkout_url.'?add-to-cart='.$current_product_id.'"  class="single_add_to_cart_button button alt">Checkout</a>' );
}
?>

3. Remove the product from the checkout page.

<?php
add_filter ('woocommerce_cart_item_name', 'cart_removed_product' , 10, 3 );
function cart_removed_product( $product_title, $cart_item, $cart_item_key ) {
  /* Checkout page check */
  if (  is_checkout() ) {
    /* Get Cart of the user */
    $cart = WC()->cart->get_cart();
    foreach ( $cart as $cart_key => $cart_value ){
       if ( $cart_key == $cart_item_key ){
        $product_id = $cart_item['product_id'];
        $_product   = $cart_item['data'] ;
        
        /* Step 1 : Add delete icon */
        $return_value = sprintf(
          '<a href="%s" class="remove" title="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
          esc_url( WC()->cart->get_remove_url( $cart_key ) ),
          __( 'Remove this item', 'woocommerce' ),
          esc_attr( $product_id ),
          esc_attr( $_product->get_sku() )
        );
        
        /* Step 2 : Add product name */
        $return_value .= '&nbsp; <span class = "product_name" >' . $product_title . '</span>' ;
        
        return $return_value;
      }
    }
  }
}
?>

OUTPUT:

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories