【问题标题】:save woocommerce billing custom field in database在数据库中保存 woocommerce 计费自定义字段
【发布时间】:2015-04-22 07:57:18
【问题描述】:

我在 woocommerce 计费字段中添加了一个自定义字段。请检查以下代码

add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );

function custom_woocommerce_billing_fields( $fields ) {
   $fields['duplicate_billing_address'] = array(

        'type'          => 'checkbox',
        'label'     => __('Duplicate Billing Address to Shipping Address', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => true
     );

    return $fields;
}

如何将此自定义字段值保存在数据库中。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您需要保存woocommerce_checkout_update_order_meta 挂钩。您可以查看我在custom WooCommerce checkout fields 上的教程了解更多详情。

    // save the extra field when checkout is processed
    function kia_save_extra_checkout_fields( $order_id, $posted ){
        if( isset( $posted['duplicate_billing_address'] ) ) {
            update_post_meta( $order_id, '_duplicate_billing_address', 'yes' );
        } else {
             update_post_meta( $order_id, '_duplicate_billing_address', 'no' );
        }
    
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
    

    【讨论】:

    • 但是在这里,我在编辑帐单地址页面 (test.addpronetwork.com/flexotech-eCommerce/my-account/edit-address/billing/) 上添加了这个自定义字段,而不是在结帐页面 (test.addpronetwork.com/flexotech-eCommerce/my-account/edit-address/billing/) 上。 addpronetwork.com/flexotech-eCommerce/checkout/).. 我认为您的代码将在结帐页面上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2015-06-01
    • 1970-01-01
    • 2016-05-21
    • 2011-06-10
    • 2023-03-05
    相关资源
    最近更新 更多