Home > database >  Passing product details from an order to jQuery/javascript on WooCommerce checkout
Passing product details from an order to jQuery/javascript on WooCommerce checkout

Time:10-12

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

CodePudding user response:

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.

add_action('woocommerce_checkout_create_order_line_item', 'is_express_delivery',  1, 4);
function is_express_delivery($item, $cartItemKey, $values, $order)
{
    $itemDetails = [
        'productId' => $item->get_product_id(),
        'variationId' => $item->get_variation_id(),
        'itemPrice' => $item->get_total(),
        'itemQuantity' => $item->get_quantity(),
        'itemTotal' => $item->get_total() * $item->get_quantity(),
    ];

    // your work with this array
}

Code is tested and works - place it in your child theme's function.php file.

CodePudding user response:

  • woocommerce_checkout_order_processed contains not 1 but 3 arguments
  • An order usually consists of several products, so $product = wc_get_product( $order_id ); won't work

Use: How to make Google Chrome JavaScript console persistent? to view the results in the console log.

So you get:

function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
    // Initialize
    $product_ids = array();
    $prices = array();
    
    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        // Get the WC_Product Object
        $product = $item->get_product();
        
        // Product ID
        $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
        
        // Price
        $product_price = $product->get_price();
        
        // Push to array
        $product_ids[] = $product_id;
        $prices[] = $product_price;
    }
    ?>
    <script>
    jQuery(function($) {
        // jQuery variables
        var product_ids = <?php echo json_encode( $product_ids ); ?>;
        var prices = <?php echo json_encode( $prices ); ?>;
        
        // Log
        console.log( product_ids );
        console.log( prices );
    });
    </script>
    <?php
}
add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );
  • Related