I am trying to catch productId and price from WooCommerce checkout, but not able to figure out why its not working.
I created a hook which I placed in functions.php
(Astra theme)
add_action( 'woocommerce_checkout_order_processed', 'is_express_delivery', 1, 1 );
function is_express_delivery( $order_id ){
$order = new WC_Order( $order_id );
$product = wc_get_product( $order_id );
$productId = $product->get_id();
$price = $product->get_price();
?>
<script type="text/javascript">
var clicky_goal = { id: ""<?php echo $productId ?>"", revenue: ""<?php echo $price ?>"" };
</script>
<?php
}
Basically, I want to catch productId and price on cart submission and send it to tracking tool
2
Answers
You can use the
woocommerce_checkout_create_order_line_item
hook. It’s run for each order item, at the moment an order is created.If you’re working with variations, you might need that ID as well.
Code is tested and works – place it in your child theme’s function.php file.
woocommerce_checkout_order_processed
contains not 1 but 3 arguments$product = wc_get_product( $order_id );
won’t workUse: How to make Google Chrome JavaScript console persistent? to view the results in the console log.
So you get: