使用订单数据在 Thanyou 页面上嵌入 JS 脚本 WooCommerce
Embed a JS script WooCommerce on Thanyou page with Order data
我需要创建一个脚本来在 woocommerce 的感谢页面上发送查询。
我正在尝试为运输脚本做类似的事情,但我需要调用一个 api,地址和 skus 从订单感谢页面中触发。大家能帮忙吗?
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"
})
有资料:
使用以下代码在 WooCommerce 订单接收页面中嵌入 Deliverr 的 JS,其中包含订单详细信息:
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
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我需要创建一个脚本来在 woocommerce 的感谢页面上发送查询。
我正在尝试为运输脚本做类似的事情,但我需要调用一个 api,地址和 skus 从订单感谢页面中触发。大家能帮忙吗?
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"
})
有资料:
使用以下代码在 WooCommerce 订单接收页面中嵌入 Deliverr 的 JS,其中包含订单详细信息:
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
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。