javascript - Embed a JS script WooCommerce on Thanyou page with Order data
问题描述
I need to create a script to send a query on the thank you page in woocommerce.
I am trying to do something similar for a shipping script But I nee to call an api with address and skus fired from the order thank you page. Can any of you help?
window.DeliverrApi.getShippingOptions({
destination: {
street1: "110 Sutter Street",
street2: "9th Floor",
zip: "94014",
city: "San Francisco",
state: "CA",
country: "US"
},
skus: [
"sku127"
],
sellerId: "deliverr-seller"
})
there data:
解决方案
Use the following to embed a JS for Deliverr in WooCommerce Order received page with the order details:
add_action( 'woocommerce_thankyou', 'deliverr_api_shipping_options_js' );
function deliverr_api_shipping_options_js( $order_id ) {
// Get the WC_Order Object instance from order id
$order = wc_get_order( $order_id );
$seller_id = 'deliverr-seller'; // ? !!! - to be defined
$skus = array(); // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product = $item->get_product(); // Get the WC_Product Object instance
$skus[] = $product->get_sku(); // Add each product sku to the array
}
?><script type="text/javascript">
jQuery( function($){
window.DeliverrApi.getShippingOptions({
destination: {
street1: "<?php echo $order->get_shipping_address_1(); ?>",
street2: "<?php echo $order->get_shipping_address_2(); ?>",
zip: "<?php echo $order->get_shipping_postcode(); ?>",
city: "<?php echo $order->get_shipping_city(); ?>",
state: "<?php echo $order->get_shipping_state(); ?>",
country: "<?php echo $order->get_shipping_country(); ?>"
}<?php if( ! empty($skus) ) { ?>,
skus: [
"<?php echo implode('", "', $skus); ?>"
]<?php }
if( ! empty($seller_id) ) { ?>,
sellerId: "deliverr-seller"
<?php } ?>
});
});
</script>
<?php
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
推荐阅读
- jenkins - 如何检查执行程序是否在 Jenkins 中运行
- android - 使用 Xamarin 无需单击即可拍照
- css - Angular flex 的问题
- go - 如何将 Terraform 字符串集的字符串表示形式转换为字符串切片
- javascript - 通过ajax响应将来自php的mysql结果放入json对象
- php - Laravel 8 - MS SQL - 查询生成器 - 使用 DB Raw。尝试使代码正确,使其像工作的 MSSQL 代码一样工作
- visual-studio - 如何区分消息是来自 ReSharper 还是 Visual Studio
- php - 如何在PHP的同一页面中的while循环之外传递值?
- java - Weblogic:logback线程没有被移除,然后war应用程序被从weblogic中移除
- node.js - ResponseError:Confluent_Schema_Registry - 错误,状态 400