1
votes

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!

1
SKU/model does not usually propagate in ecommerce order URLs because of the many-to-one relationship (eg: multiple products, one order). Suggest querying the DB with the order ID to get what you need for output. - Mavelo
@Mavelo thanks a lot, no wonder why I could not find anything about this. Do you know what unique parameter can I add in the order-received URL? It could be [product-name], [product-id] - Juan
@jrswgtr, An user cannot add more than one product to the cart so it's not really an issue. But I don't mind using the ID if possible, any kind of individualization would be great! If the URL ends up being order-received/286/?key=wc_order_5b5f46b2ec536?[product-id] it's fine to me as it is unique. - Juan
I guess you could combine all SKUs (comma in this example) from the order details and append them to the URL like &sku=ABC123,DEF456,GHI789. Then split the string by the delimiter and loop through them on the success page. Even though users cannot order multiple items in your configuration, doesn't mean you should limit your installation with poor planning. This alternative method would work for that purpose, but you're really better off with a DB query. - Mavelo
@Mavelo I don't know how to query the DB. I am a marketer, not a dev. I thought there could be a way to add the id, name or SKU to the thank you URL - Juan

1 Answers

1
votes

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!