skip to Main Content

I’m using this simple code to add a ‘buy now’ button on a single product page.

add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' ); 
function add_content_after_addtocart() {
    // get the current post/product ID
    $current_product_id = get_the_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();
    
    // run only on simple products
    if( $product->is_type( 'simple' ) ) {
        echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="mongo_single_add_to_cart_button button">Buy Now</a>';
    }
}

This code effectively redirects to the checkout page and add the product to the cart, but I want to add two little features to it:

  1. After clicking on the button, I want it to clear the cart before taking the action.

  2. After adding the product to the cart, I want it to redirect users to ‘/checkout’ page. Right now it sends users on ‘checkout/?add-to-cart=3122’, which means that any refresh on the checkout page adds 1 product on the cart automatically.

Any advice?

2

Answers


  1. Here is code for clear cart before add item

    add_filter( 'woocommerce_add_to_cart_validation', 'ji_remove_cart_item_before_add_to_cart', 20, 3 );
    function ji_remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
    if( ! WC()->cart->is_empty() )
        WC()->cart->empty_cart();
    return $passed;}
    
    1. After adding the product to the cart, I want it to redirect users to ‘/checkout’ page. Right now it sends users on ‘checkout/?add-to-cart=3122’, which means that any refresh on the checkout page adds 1 product on the cart automatically.

      add_filter( ‘woocommerce_add_to_cart_redirect’, ‘ji_redirect_checkout_after_add_to_cart’ );

      function ji_redirect_checkout_after_add_to_cart() {
      return wc_get_checkout_url();
      }

    Login or Signup to reply.
  2. Instead of using the add-to-cart param in your url, which will cause the product to be added (but also perform other actions in WooCommerce), you can use a custom url for your button and the template_redirect action hook

    That way you get rid of the built-in functionality in WooCommerce and you can perform your own custom actions based on the GET parameters

    So you get:

    // Add new/extra button
    function action_woocommerce_after_add_to_cart_button() {
        global $product;
        
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            // Run only on simple products
            if ( $product->is_type( 'simple' ) ) {
                // Get product ID
                $product_id = $product->get_id();
    
                // Get permalink
                $permalink = $product->get_permalink();
    
                // Output url
                echo '<a href="' . $permalink . '?product_id=' . $product_id . '&redirect_checkout=true" class="mongo_single_add_to_cart_button button">'. __ ( 'Buy Now', 'woocommerce' ) . '</a>';
            }
        }
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10 );
    
    // Redirect
    function action_template_redirect() {
        // Determines whether the current request is for an administrative interface page
        if ( is_admin() ) return;
    
        // Returns true when viewing a single product
        if ( ! is_product() ) return;
    
        // Get params
        if ( isset( $_GET['product_id'] ) && isset( $_GET['redirect_checkout'] ) ) {
            // Get param 1
            $product_id = $_GET['product_id'];
    
            // Get param 2
            $boolean = $_GET['redirect_checkout'];
    
            // WC Cart
            if ( WC()->cart ) {
                // 1. Empty cart
                WC()->cart->empty_cart();
    
                // 2. Add to cart
                WC()->cart->add_to_cart( $product_id );
    
                // 3. Redirect
                // When true
                if ( $boolean ) {
                    // Gets the url to the checkout page
                    $checkout_url = wc_get_checkout_url();
    
                    // Performs a safe (local) redirect
                    wp_safe_redirect( $checkout_url );
                    exit;
                }
            }
        }
    }
    add_action( 'template_redirect', 'action_template_redirect' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search