Javascript 从WebApi调用加载商业产品
我正在开发一个Wordpress页面,该页面使用Woocommerce作为支付方式。我在使用WebApi调用动态设置产品的价格、名称和描述时遇到问题 我的想法是,我将向我已经设置好的WebApi发送一个AJAX调用,该请求将返回一个包含我需要的所有信息的产品。这不是问题(我已经使用JS正确地获取了数据)。然而,经过一些研究,我现在明白了我应该从php调用WebApi(因为产品信息来自服务器端)。当产品页面加载时,我应该这样做,但是我没有找到关于如何设置的文档,也没有找到关于如何从php动态获取商业产品信息的文档 我已经能够从产品页面调用几个JS函数,但由于某些原因,我无法找到在加载时更改产品属性的方法。有办法做到这一点吗 下面是我的Javascript 从WebApi调用加载商业产品,javascript,php,wordpress,woocommerce,Javascript,Php,Wordpress,Woocommerce,我正在开发一个Wordpress页面,该页面使用Woocommerce作为支付方式。我在使用WebApi调用动态设置产品的价格、名称和描述时遇到问题 我的想法是,我将向我已经设置好的WebApi发送一个AJAX调用,该请求将返回一个包含我需要的所有信息的产品。这不是问题(我已经使用JS正确地获取了数据)。然而,经过一些研究,我现在明白了我应该从php调用WebApi(因为产品信息来自服务器端)。当产品页面加载时,我应该这样做,但是我没有找到关于如何设置的文档,也没有找到关于如何从php动态获取商
functions.php
代码,它引用了我的产品页面:
add_action('wp_enqueue_scripts', 'order_received_enqueue_js_script');
function order_received_enqueue_js_script() {
// Only on order received" (thankyou)
if( ! is_wc_endpoint_url('order-received') )
return;
$order_id = absint( get_query_var('order-received') ); // Get the order ID
$order = wc_get_order( $order_id ); // Get the WC_Order Object
// Only for processing orders
if ( ! is_a( $order, 'WC_Order') || ! $order->has_status( 'processing' ) ) {
return;
}
wp_enqueue_script(
'my-java', // Handle
get_stylesheet_directory_uri() . '/js/custom.js?V=2020.09.01v104', // URL for child or parent themes
array( 'jquery' ), // Dependencies
false, // Version
true // In footer
);
}
上面的代码将我的JS脚本排入WC产品的页面
add_action('wp_footer', 'order_received_js_script');
function order_received_js_script() {
// Only on order received" (thankyou)
if( ! is_wc_endpoint_url('order-received') )
return; // Exit
$order_id = absint( get_query_var('order-received') ); // Get the order ID
if( get_post_type( $order_id ) !== 'shop_order' ) {
return; // Exit
}
$order = wc_get_order( $order_id ); // Get the WC_Order Object
// Only for processing orders
if ( method_exists( $order, 'has_status') && ! $order->has_status( 'processing' ) ) {
return; // Exit
}
?>
<script>
// Once DOM is loaded
jQuery( function($) {
// Trigger a function (example)
purchase({'total':'<?php echo $order->get_total(); ?>',
'firstName':'<?php echo $order->get_billing_first_name(); ?>',
'lastName':'<?php echo $order->get_billing_last_name(); ?>',
'email':'<?php echo $order->get_billing_email(); ?>',
'companyName':'<?php echo $order->get_billing_company(); ?>'
});
});
</script>
<?php
}
add_action('wp_footer','order_received_js_script');
函数顺序_已接收_js_脚本(){
//仅在收到订单时使用”(谢谢)
如果(!is_wc_endpoint_url('order-received'))
return;//退出
$order\u id=absint(get\u query\u var('order-received');//获取订单id
如果(获取发布类型($order\u id)!='shop\u order'){
return;//退出
}
$order=wc\u get\u order($order\u id);//获取wc\u order对象
//仅用于处理订单
如果(方法_存在($order,'has_status')&&!$order->has_status('processing')){
return;//退出
}
?>
//加载DOM后
jQuery(函数($){
//触发函数(示例)
购买({'total':'',
“名字”:“,
“姓氏”:“,
“电子邮件”:“,
“公司名称”:”
});
});