Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active September 28, 2020 20:35
Show Gist options
  • Save xlplugins/2b6f264b5f119fad485a438b46282c82 to your computer and use it in GitHub Desktop.
Save xlplugins/2b6f264b5f119fad485a438b46282c82 to your computer and use it in GitHub Desktop.
Add custom javascripts or php action on WooCommerce Thank you page after order completes
<?php
/**
* This is a PHP code, so should be wrapped inside PHP
* Like we added here.
* If PHP is already open then don't include above <?php open tag
*/
/**
* Adding custom javascripts or php action on WooCommerce Thank you page
* Works with WooCommerce 3.0 or above
*/
add_action( "woocommerce_thankyou", "xlwcty_thank_you_script", 20 );
if ( ! function_exists( 'xlwcty_thank_you_script' ) ) {
function xlwcty_thank_you_script( $order_id ) {
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order instanceof WC_Order ) {
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
/**
* full list methods and property that can be accessed from $order object
* https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html
*/
?>
<script type="text/javascript">
// write custom action here
</script>
<?php
}
}
}
}
/**
* This code execute inside head tag
*/
add_action( "wp_head", "xlwcty_thank_you_header_script", 20 );
if ( ! function_exists( 'xlwcty_thank_you_header_script' ) ) {
function xlwcty_thank_you_header_script() {
if ( function_exists( 'is_order_received_page' ) && is_order_received_page() && isset( $_GET['order_id'] ) ) {
$order_id = $_GET['order_id'];
$order = wc_get_order( $order_id );
if ( $order instanceof WC_Order ) {
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
/**
* full list methods and property that can be accessed from $order object
* https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html
*/
?>
<script type="text/javascript">
// write custom action here
</script>
<?php
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment