WooCommerce sales confirmation script
On this page
Checkout script
To benefit from the analytical data Usizy can offer in relation to sales and conversion, you must send sales confirmations to Usizy.
To do so, please add the following code snippet, ticking the “only run on site front-end” option:
add_action('woocommerce_thankyou', function( $order_id ) {
$order = wc_get_order( $order_id );
if ( !$order->has_status('failed') or !$order->has_status('cancelled') ) {
// if order not failed or cancelled
printf('<script src="https://static.usizy.es/js/platform.min.js"></script>');
$product_ids = array();
$variation_ids = array();
$sizes = array();
$prices = array();
foreach ( $order->get_items() as $item_id => $item ) {
array_push($product_ids, $item->get_product_id());
array_push($variation_ids, $item->get_variation_id());
$variation_attr=wc_get_product_variation_attributes($item->get_variation_id());
if (array_key_exists('attribute_pa_size', $variation_attr)) {
array_push($sizes, $variation_attr['attribute_pa_size']);
}
$quantity = $item->get_quantity();
array_push($prices, $item->get_total() / $quantity);
for($i = 1; $i < $quantity; $i++){
array_push($product_ids, $item->get_product_id());
array_push($variation_ids, $item->get_variation_id());
if (array_key_exists('attribute_pa_size', $variation_attr)) {
array_push($sizes, $variation_attr['attribute_pa_size']);
}
array_push($prices, $item->get_total() / $quantity);
}
}
$product_ids = json_encode($product_ids);
$variation_ids = json_encode($variation_ids);
$sizes = json_encode($sizes);
$prices = json_encode($prices);
$currency = $order->get_currency();
$total_incl_taxes = $order->get_total();
$total_taxes = $order->get_total_tax();
$total_excl_taxes = $total_incl_taxes - $total_taxes;
$call = <<<EOT
<script>
uSizy.push({
'event':'CONFIRM',
'order_id':'$order_id',
'product_ids': $product_ids,
'variation_ids': $variation_ids,
'sizes': $sizes,
'currency':'$currency',
'total_no_vat':'$total_excl_taxes',
'total_vat':'$total_incl_taxes',
'prices_vat':$prices,
})
</script>
EOT;
printf($call);
}
});
Sales Webhook
To make sure all sales and sales cancellations are captured, the following webhook must be created to capture order updates in WooCommerce, as indicated below:
Please configure the webhook according to the following information:
- Name: Usizy Sales
- Status: Active
- Topic: Order updated
- Delivery URL:https://usizy.com/api/v1/woocommerce-orders-webhook
- Secret: leave empty
- API Version: WP REST API Integration v3
Once you have completed the configuration, click on Save.
The following message will appear as in the following example:
As a result, the Secret
field will be displayed.
Please make sure to send this secret to Usizy.