简体   繁体   English

在 Woocommerce 中保存计费自定义字段

[英]Save billing custom field in Woocommerce

I added a custom field on my Woocommerce checkout page but I can't seem to save it after placing an order.我在我的 Woocommerce 结帐页面上添加了一个自定义字段,但下订单后我似乎无法保存它。 On my functions.php I added the following:在我的functions.php上,我添加了以下内容:

add_filter( 'woocommerce_checkout_fields' , 'dropdownBranch' );


function dropdownBranch($fields) {
     $fields['billing']['billing_branch'] = array(
    'label'       => __('Branch', 'woocommerce'),
    'placeholder' => _x('', 'placeholder', 'woocommerce'),
    'required'    => true,
    'clear'       => false,
    'type'        => 'select',
    'options'     => array(
        'branch-1' => __('Branch 1', 'woocommerce' ),
        'branch-2' => __('Branch 2', 'woocommerce' )
        )
    );
     return $fields;
}

How I'm currently saving it:我目前如何保存它:

function saveBranch( $order_id, $posted ) {
    if (isset($posted['billing']['billing_branch'])) {
        $branch = sanitize_text_field( $posted['billing']['billing_branch'] );
        update_order_meta( $order_id, 'billing_branch', $branch);
    }
}

add_action( 'woocommerce_checkout_update_order_meta', 'saveBranch', 10, 2 );

function displayValue($order_id) {
    $order = wc_get_order($order_id);
    $order_data = $order->get_data();
    
    $value = $order_data['billing']['billing_branch'];
}

Your saveBranch function is the issue.您的 saveBranch function 是问题所在。

add_action( 'woocommerce_checkout_update_order_meta', 'saveBranch', 10, 2 );
function saveBranch( $order_id ) {
    if (isset($_POST['billing']['billing_branch'])) {
        update_order_meta( $order_id, 'billing_branch', sanitize_text_field( $_POST['billing']['billing_branch'] ));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM