Skip to Content
Menu

woocommerce_order_data()

Update visitor data after WooCommerce checkout.

PHP April 27, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

The order ID is passed by WooCommerce and is handled automatically. This function requires Nebula Visitor DB option to be enabled.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Ecommerce.php on line 182.

    No Hooks

    This function does not have any filters or actions available. Request one?

    Note: This function contains 1 to-do comment.

    PHP
            public function woocommerce_order_data($order_id){
                $order = new WC_Order($order_id);
    
                echo '<script>';
                echo 'gtag("set", "user_properties", {
                    woocommerce_customer : "Order Received"
                });';
    
                $product_items = array(); //Prep this for the GA payload
                $index = 0;
                foreach ( $order->get_items() as $item_id => $item ){ //Loop through all of the items in the order
                    $variation = wc_get_product($item->get_variation_id()); //If no variation, this will appear the same as the product itself
    
                    $item_variant = '';
                    if ( !empty($variation) ){
                        $item_variant = $variation->get_name();
                    }
    
                    $product_items[] = array(
                        'item_id' => $item->get_product_id(),
                        'item_name' => $item->get_name(),
                        'currency' => 'USD',
                        'item_variant' => $item_variant,
                        'price' => $item->get_meta('_price', true),
                        'quantity' => $item->get_quantity(),
                        'index' => $index,
                    );
    
                    $index++;
                }
    
                echo 'gtag("event", "purchase", {
                    event_category: "Ecommerce",
                    event_action: "Purchase",
                    event_label: "Order ID: ' . $order->get_id() . ' (' . $order->get_total() . ')",
                    transaction_id: "' . $order->get_id() . '",
                    value: ' . $order->get_total() . ', //Grand total price
                    tax: ' . $order->get_total_tax() . ',
                    shipping: ' . $order->get_shipping_total() . ',
                    currency: "' . $order->get_currency() . '",
                    items: ' . json_encode($product_items) . '
                });';
    
                echo '</script>';
    
                if ( $this->get_option('hubspot_portal') ){
                    $order = new WC_Order($order_id);
                    ?>
    
                    <?php if ( 1==2 ): //@todo "Nebula" 0: See if this script tag will break anything! If this can't be done here, try the above "woo_custom_ga_events" function... but can order details be accessed from that page? ?>
                    <script>
                        <?php //Don't use nv() here because this is being included with the initial Hubspot data before the pageview is sent ?>
                        _hsq.push(["identify", {
                            role: 'Customer',
                            email: '<?php echo $order->billing_email; ?>',
                            firstname: '<?php echo $order->billing_first_name; ?>',
                            lastname: '<?php echo $order->billing_last_name; ?>',
                            full_name: '<?php echo $order->billing_first_name . ' ' . $order->billing_last_name; ?>',
                            street_full: '<?php echo $order->billing_address_1 . ' ' . $order->billing_address_2; ?>',
                            city: '<?php echo $order->billing_city; ?>',
                            state: '<?php echo $order->billing_state; ?>',
                            zipcode: '<?php echo $order->billing_postcode; ?>',
                            country: '<?php echo $order->billing_country; ?>',
                            phone: '<?php echo $order->billing_phone; ?>',
                        }]);
                    </script>
                    <?php endif; ?>
                    <?php
                }
            }
    

    Override

    This function can not be short-circuited with an override filter. Request one?