Juan
Juan

Reputation: 163

Woocommerce - Display SKU in order-received page URL

I do marketing, and I need to know if a customer has purchased a specific product so I can remarket them.

So, "show this ad, except to those that have visited the order-received page" doesn't work for me, because I need to remarket several products individualizing the marketing campaigns.

I was able to add the SKU in the ADD-TO-CART/CHECKOUT page following this tutorial: https://www.themelocation.com/how-to-add-sku-to-product-url-in-woocommerce/

But the SKU does not propagate to the thank you page (order-received), I receive these parameters only: order-received/286/?key=wc_order_5b5f46b2ec536

(286 is the order number, not the SKU).

This is the outcome I would need: order-received/286/?key=wc_order_5b5f46b2ec536?SKU=[PRODUCT-SKU]

Is there any way to achieve this? Display the SKU in the order-received page?

Thanks!

Upvotes: 1

Views: 1457

Answers (1)

Juan
Juan

Reputation: 163

Soooo... I was able to find how to do this. This works for single-product.

Add this piece of code in your functions.php file (remember to place it in your child-theme so it doesn't get replaced when there is a woocommerce upgrade).

This will add the SKU in the order-received URL.

//ADD SKU IN THANK YOU URL
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();

// retrive 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; 

}

Hope it helps!

Upvotes: 1

Related Questions