1
votes

In woocommerce, we allow to buy only one product at the time on our shop. We have very few products and no shop page (or archives pages). Quantity fields are disabled and customer is redirected to checkout on add to cart.

We need to customize some URLs to detect what has been added to cart (what has been bought). So I would need to propagate the product SKU Checkout and Order received URLs.

I was able, for example, to add the SKU in the THANK YOU URL with this code:

add_filter('woocommerce_get_checkout_order_received_url','override_return_url',10,2);
function override_return_url($return_url,$order){

    // create empty array to store url parameters in 
    $sku_list = array();

    // retrieve products in order
    foreach($order->get_items() as $key => $item)
    {
        $product = wc_get_product($item['product_id']);

        // get sku of each product and insert it in array 
        $sku_list['product_'.$item['product_id'] .'sku'] = $product->get_sku();
    }
    // build query strings out of the SKU array
    $url_extension = http_build_query($sku_list);

    // append our strings to original url
    $modified_url = $return_url.'&'.$url_extension;

    return $modified_url;

  }

But I need the SKU in the checkout URL too.

For information I have multiple kinds of Add to cart buttons on a product page:

Is it possible? How should I do?

1
Noone? Is my question clear? Please let me know if you need any more info. Thanks!Juan

1 Answers

1
votes

Your code is a bit outdated since Woocommerce 3… Instead you should use the following code.

You will have to disable any add to cart redirection by settings or by code. You will have to remove all your related code to avoid errors or malfunctions.

The code:

// Redirect to checkout on add to cart and append the sku to the url
add_action( 'woocommerce_add_to_cart_form_action', 'add_to_cart_sku_to_url', 20, 1 );
function add_to_cart_sku_to_url( $product_permalink ) {
    global $product;

    if( $product->get_sku() ) {
        return add_query_arg( 'sku', $product->get_sku(), wc_get_checkout_url() );
    }
    return $product_permalink;
}

// Order received: Append the product sku to the URL
add_filter( 'woocommerce_get_checkout_order_received_url', 'add_product_sku_to_return_url', 20, 2 );
function add_product_sku_to_return_url( $return_url, $order  ) {
    $items   = $order->get_items();
    $item    = reset($items);
    $product = $item->get_product();
    $sku      = $product->get_sku();

    if( ! empty($sku) )
        $return_url = add_query_arg( 'sku', $sku, $return_url );

    return $return_url;
}

// Add to cart shortcode ( Product ID is not needed )
if( ! function_exists('get_custom_add_to_cart') ) {
    function get_custom_add_to_cart( ) {
        if ( ! is_product() ) return;

        ob_start();

        global $product, $post;

        if ( ! is_a( $product, 'WC_Order' ) )
            $product = wc_get_product( get_the_id() );

        do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );

        return ob_get_clean();

    }
    add_shortcode( 'custom_add_to_cart', 'get_custom_add_to_cart' );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works


Specifically for your plugin you should also add this code:

// Redirect GET add to cart to checkout apending sku as query arg
add_action('template_redirect', 'get_add_to_cart_sku_to_checkout');
function get_add_to_cart_sku_to_checkout() {
    // Not on checkout page
    if( ( is_product() || is_404() ) && isset($_GET['add-to-cart']) ){
        $product_id = (int) $_GET['add-to-cart']; // Checkout Url
        if( $product_id > 0 ){
            $sku = get_post_meta( $product_id, '_sku', true );
            wp_redirect( add_query_arg( 'sku', $sku, wc_get_checkout_url() ) );
            exit();
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works