简体   繁体   中英

Woocommerce - On page load, add product to cart and proceed to Checkout automatically

I would like to give a link to a person that allows them to land right on the checkout page with the product already in their cart.

I believe I will need to do two things:

  1. On page load, run the script to automatically add the product to the cart
  2. On product add to cart, run another script to send the person to the checkout page

I have two snippets of each but I am not sure if they will work together like this and I am not sure how to get this to work on a specific page (likely just a blank page that will barely show long enough to add to cart and send user to checkout page)

<?php
/*
 * goes in theme functions.php or a custom plugin
 **/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 21;
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if {( $_product->id == $product_id )
                    $found = true;}
            }
            // if product not found, add it
            if {( ! $found )
                WC()->cart->add_to_cart( $product_id );}
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

/**
 * Redirect users after add to cart.
 */
function my_custom_add_to_cart_redirect( $url ) {
    $url = WC()->cart->get_checkout_url();
    // $url = wc_get_checkout_url(); // since WC 2.5.0
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

IF there is a better way to do this too, let me know.

Thanks, Brian

You can actually do this without any additional code.

WooCommerce supports adding items to the cart via a ?add-to-cart URL parameter.

If you send someone to any page, followed by the URL parameter ?add-to-cart=21 it will automatically add the product with ID 21 to their cart. In this case, you can send them straight to the Checkout page.

This is what your custom URL would look like:

http://example.com/checkout/?add-to-cart=21

Doing it this way will be much quicker than running a script, and then redirecting to checkout.

I'll recommend to set up this functionality for a generic use case eg send users to example.com?get=21 where 21 is the product ID you want to add to cart on load. To follow this approach, your code will be something like

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {

    if ( ! is_admin() && isset( $_GET['get'] ) ) {
        $product_id = $_GET['get'];
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id ) {
                    $found = true;
                }
            }
            // if product not found, add it
            if ( ! $found ){
                WC()->cart->add_to_cart( $product_id );
            }
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }

        // Redirect users to checkout after add to cart
        wp_redirect( wc_get_checkout_url() );
        exit;
    }
}

You do not need the 'woocommerce_add_to_cart_redirect' filter and it's function.

Now, if you want to trigger this on a specific page only, say ID of that page is 55 , then the line

if ( ! is_admin() && isset( $_GET['get'] ) ) {

will become

if ( ! is_admin() && is_page( 55 ) ) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM