[英]can't get Woocommerce billing phone inside input field using jquery
I have a CF7 form and need to get Woocommerce billing phone number and display it inside phone field on page load using jQuery function.我有一个 CF7 表格,需要获取Woocommerce 计费电话号码,并使用 jQuery function 在页面加载时的电话字段中显示它。 I used this code in header area:我在 header 区域使用了这个代码:
// Get billing_phone_number On Quote Form
jQuery(document).ready(function(){
jQuery('#mk-order-tel').val("<?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>");
});
But it returns raw php code ( <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true )?>
) instead of phone number;但它返回原始 php 代码( <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true )?>
)而不是电话号码; when I use same php code directly in php files of Woocommerce it works correctly.当我直接在 Woocommerce 的 php 文件中使用相同的 php 代码时,它可以正常工作。 What should I do to run a php syntax through the jQuery?我应该怎么做才能通过 jQuery 运行 php 语法?
EDIT编辑
To escape of above issue, I tried to get data by using ajax but this method does not work too:为了避免上述问题,我尝试使用 ajax 获取数据,但这种方法也不起作用:
in functions.php:在函数中。php:
/* Get Customer Phone Number */
function my_ajax_handler(){
global $woocommerce;
$phone = get_user_meta( get_current_user_id(), 'billing_phone', true );
?>
<script type="text/javascript">
jQuery( function($){
jQuery('#mk-order-tel').val("<?php echo json_encode($phone); ?>");
});
</script>
<?php
}
add_action( 'wp_ajax_call_my_ajax_handler', 'my_ajax_handler' );
add_action( 'wp_ajax_nopriv_call_my_ajax_handler', 'my_ajax_handler' );
function my_quote_scripts() {
wp_enqueue_script( 'my-ajax-script', get_stylesheet_directory_uri() . '/js/quote.js', array('jquery') );
wp_localize_script( 'my-ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_quote_scripts' );
in quote.js:在quote.js:
jQuery.ajax({
type:"POST",
url: my_ajax_object.ajax_url,
data: { 'action': 'call_my_ajax_handler' }
})
What else can I do?我还可以做些什么?
This should work, you're getting an exception because of quotes这应该可行,由于引号,您会遇到异常
const tel = <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>;
jQuery(document).ready(function(){
jQuery('#tel').val(tel);
});
Or you can just replace single quotes to double或者您可以将单引号替换为双引号
jQuery(document).ready(function(){
jQuery('#tel').val("<?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>");
});
Simply using this code in functions.php of child theme can fetch customer phone and display inside input text field:只需在函数中使用此代码。子主题的 php 可以获取客户电话并在输入文本字段中显示:
/* Get Customer Phone Number On Quote Form */
function my_jquery_var() {
if ( $yourtel = get_user_meta( get_current_user_id(), 'billing_phone', true ) ) {
echo '<script type="text/javascript">
var yourTel = "' . $yourtel . '";
document.getElementById("mk-order-tel").value = yourTel;
</script>' . "\n";
}
}
add_action( 'wp_footer', 'my_jquery_var' );
Note : JavaScript should be in footer area.注意:JavaScript 应该在页脚区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.