如果 billing_address_2 在下訂單之前包含 10 的值,我需要禁用下訂單按鈕。
我在想我們如何直接從現場而不是從帳戶中獲得它,而是在用戶選擇選項時從現場獲得。該欄位現在設定為下拉選擇,以便人們可以從選項中進行選擇。
到目前為止,我有這段代碼,但不幸的是沒有想要的結果。我想我離讓它作業不遠了。任何建議?
到目前為止的代碼:
add_filter('woocommerce_order_button_html', 'disable_place_order_button_html', 10, 2);
function disable_place_order_button_html( $button ) {
// Get the field data
$address_fields = $_POST['billing_address_2'] );
// If the billing_address 2 contains 10 we disable button
if( $address_fields == '10 ' ) {
$style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important; text-align:center;"';
$text = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
$button = '<a '.$style.'>' . $text . '</a>';
}
return $button;
}
billing_address_2 下拉代碼
add_filter( 'woocommerce_default_address_fields' , 'address_field_dropdown' );
function address_field_dropdown( $address_fields ) {
$location_array = array(
'Location 1' => '1',
'Location 2' => '2',
'Location 3' => '3',
'Location 4' => '4',
'Location 5' => '5',
'Location 6' => '6',
'Location 7' => '7',
'Location 8' => '8',
'Location 9' => '9',
'Location 10' => '10 ',
);
$address_fields['address_2']['label'] = 'Po?et os?b';
$address_fields['address_2']['type'] = 'select';
$address_fields['address_2']['options'] = $location_array;
return $address_fields;
}
uj5u.com熱心網友回復:
要在從下拉選單中選擇所需的值時實時應用,您可以使用 jQuery
所以你得到:
function action_wp_footer() {
// Only on checkout
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery( function($) {
// Function
function place_order_button() {
// Compare
if ( $( '#billing_address_2' ).val() == 'Location 10' ) {
$( '#place_order' ).prop( 'disabled', true );
$( '#place_order' ).css({ 'background-color': 'silver', 'color': 'white', 'cursor': 'not-allowed' });
} else {
$( '#place_order' ).prop( 'disabled', false );
$( '#place_order' ).removeAttr( 'style' );
}
}
// On change
$( document ).on( 'change', function() {
place_order_button();
});
// Ajax
$( document ).ajaxComplete( function () {
place_order_button()
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'action_wp_footer', 10, 0 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/345937.html
標籤:php WordPress的 求购 查看 自定义字段
上一篇:PHP-是過去的日期
