• Resolved shawaj

    (@shawaj)


    I am trying to pass the correct values to the twitter pixel on the order received / thank you page but am getting an error.

    Here is the code I am using:

    <?php if ( is_product() ) { $_product = wc_get_product( $product_id ); $google_total_value=$_product->get_price();} elseif ( is_cart() || ( is_checkout() && !is_order_received_page() ) ) { $google_total_value=WC()->cart->total; } elseif ( is_order_received_page() ) { $google_total_value = $order->get_total() ;} ?>
    
    <?php if ( is_product() ) { $twitter_quantity = 1 ;} elseif ( is_cart() || ( is_checkout() && !is_order_received_page() ) ) { $twitter_quantity = WC()->cart->get_cart_contents_count() ; } elseif ( is_order_received_page() ) { $twitter_quantity = $order->get_item_count() ;} ?>

    If I replace $order->get_item_count() and $order->get_total() with just standard numbers it works perfectly so I think the conditionals are working fine but I just can’t understand why it is not displaying the correct values?

    In the debug log I have seen the following error:

    [20-Apr-2016 12:22:57 UTC] PHP Fatal error: Call to a member function get_total() on string in httpdocs/wp-content/themes/mystile-child/footer.php on line 82

    Thanks

    https://www.ads-software.com/plugins/woocommerce/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter shawaj

    (@shawaj)

    also tried using $order = new WC_Order($order_id); and $order = wc_get_order ( $order_id ); in front of that code, to no effect.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    In the snippet above, $order is not defined. Nor is $order_id. You need to look at how to grab that in context. I don’t know where your code is to advise.

    Thread Starter shawaj

    (@shawaj)

    This is in the footer.php for our theme.

    I have tried using $order = new WC_Order($order_id); and $order = wc_get_order ( $order_id ); in front of that code above, to no effect. It always returns 0 for both.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Because $order_id does not exist there.

    Thread Starter shawaj

    (@shawaj)

    so I have to declare $order_id somewhere in footer.php?

    Where do I populate it from?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Maybe you can get it from the URL/querystring? Try absint( $_GET['order'] )

    Thread Starter shawaj

    (@shawaj)

    so it won’t work trying to get it from woocommerce directly?

    It displays the order id on the same page higher up in the table?

    Cheers

    Thread Starter shawaj

    (@shawaj)

    I have tried with $order_id set to an order number and the rest of the code works perfectly.

    Unfortunately absint( $_GET['order'] ) did not work – would having permalinks on effect this?

    But it looks like if I can correctly grab the order number from anywhere then it will all be working.

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Where do you think WooCommerce gets the ID?

    You can also try getting it from a global:

    global $wp;
    echo $wp->query_vars['order-received'];
    Thread Starter shawaj

    (@shawaj)

    No idea why absint( $_GET['order'] ) was not working, but the global method worked fine.

    Thanks very much for all of your help!

    Thread Starter shawaj

    (@shawaj)

    Marked as resolved

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘$order->get_item_count() and ->get_total() not working on order received page?’ is closed to new replies.