Skip to content Skip to sidebar Skip to footer

Pass Woocommerce Order Id From Another Page To Javascript Variable On Current Page

Background We have changed the checkout process slightly in WooCommerce Usually, a user goes: Order Checkout --> Thank you Page (where Order ID appears) --> Upsell page Now i

Solution 1:

I would not use javascript but would use wc_get_customer_last_order( $customer_id ); on your upsell page to get the lastest order id

// Get user id$customer_id = get_current_user_id();

// Get last order$last_order = wc_get_customer_last_order( $customer_id );

// Get order id$order_id = $last_order->get_id();

echo$order_id;

// EDIT: if you like to use the order id as javascript variable, you can do this in the following way?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
    var order_id = <?phpecho$last_order->get_id(); ?>;
    console.log(order_id);
});
</script>
<?php

if you don't have access to the $customer_id (depending on whether guests can order without an account on your webshop?)

You could use a sql query instead to get the latest order ID

Post a Comment for "Pass Woocommerce Order Id From Another Page To Javascript Variable On Current Page"