php 如何在woocommerce结帐中获取shipping_state字段的值?

xpcnnkqh  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(134)

我需要在woocommerce结帐中添加一个选择字段,根据所选省份的不同选择。我需要知道如何获取shipping_state的值,以便在此基础上显示与该省份对应的select。
我已经做了选择字段。

lrl1mhuk

lrl1mhuk1#

shipping_state值可以使用PHP中的WC()->customer对象获得。例如:

add_filter('woocommerce_checkout_fields', 'add_province_select_field');

function add_province_select_field($fields) {
  $shipping_state = WC()->customer->get_shipping_state();

  if ($shipping_state == 'CA') {
    $fields['shipping']['province_select'] = array(
      'type' => 'select',
      'label' => 'Province',
      'options' => array(
        'option1' => 'Option 1',
        'option2' => 'Option 2',
        'option3' => 'Option 3'
      ),
      'required' => true
    );
  } elseif ($shipping_state == 'NY') {
    $fields['shipping']['province_select'] = array(
      'type' => 'select',
      'label' => 'Province',
      'options' => array(
        'option4' => 'Option 4',
        'option5' => 'Option 5',
        'option6' => 'Option 6'
      ),
      'required' => true
    );
  } else {
    $fields['shipping']['province_select'] = array(
      'type' => 'select',
      'label' => 'Province',
      'options' => array(
        'default_option1' => 'Default Option 1',
        'default_option2' => 'Default Option 2',
        'default_option3' => 'Default Option 3'
      ),
      'required' => true
    );
  }

  return $fields;
}

参见:https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

相关问题