每个产品WooCommerce WordPress的PHP自定义结帐字段

时间:2019-02-20 08:03:22

标签: php wordpress woocommerce

因此,如果有人可以,我需要一些帮助:D 我需要为每个产品在Checkout中使用不同的自定义字段。我设法通过funtions.php中的php做到了这一点。到目前为止,还不错,但是我添加的所有字段都出现在随机的位置,这就是为什么我为这些字段添加了优先级,这是一个问题。由于优先级设置的原因,没有所有字段的产品结帐将获得空字段。我该如何解决? (请参见屏幕截图)

enter image description here

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_id = $cart_item['product_id'];
        if( $product_id == 775 ) {
            $fields['billing']['billing_masa'] = array(
                'label'     => __('Puteti lua 3-4 mese pe zi la intrvale egale de timp?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_intoleranta'] = array(
                'label'     => __('Aveti intoleranta la lactoza?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_intoleranta2'] = array(
                'label'     => __('Aveti alimente pe care nu le tolerati?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_variante'] = array(
                'label'     => __('Doriti sa primiti mai multe variante de alimente pentru a va alcatui masa dupa placul dumneavoastra?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_boli'] = array(
                'label'     => __('Suferiti de diabet, hipertensiune, afectiuni digestive?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_rutina'] = array(
                'label'     => __('Descrieti rutina alimentara zilnica de pana acum.', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
        }
        if( $product_id == 776 ) {
            $fields['billing']['billing_masa'] = array(
                'label'     => __('Puteti lua 3-4 mese pe zi la intrvale egale de timp?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_intoleranta'] = array(
                'label'     => __('Aveti intoleranta la lactoza?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_intoleranta2'] = array(
                'label'     => __('Aveti alimente pe care nu le tolerati?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_variante'] = array(
                'label'     => __('Doriti sa primiti mai multe variante de alimente pentru a va alcatui masa dupa placul dumneavoastra?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_boli'] = array(
                'label'     => __('Suferiti de diabet, hipertensiune, afectiuni digestive?', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_rutina'] = array(
                'label'     => __('Descrieti rutina alimentara zilnica de pana acum.', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_rutina2'] = array(
                'label'     => __('Descrieti o zi din viata dvs din punct de vedere alimentar.', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
            $fields['billing']['billing_istoric'] = array(
                'label'     => __('Istoricul dvs medical include interventii de natura digestiva (olecistectomie, litiaza biliara, rezectie gastrica)? Daca da, mentionati ca s-a realizat.', 'woocommerce'),
                'required'  => true ,
                'class'     => array('form-row-wide'),
                'clear'     => true
            );
        } 
    }
    return $fields;
}

/*
Priority
 */

function js_sort_checkout_fields( $fields ) {
    $fields['billing']['billing_masa']['priority'] = 111;
    $fields['billing']['billing_intoleranta']['priority'] = 112;
    $fields['billing']['billing_intoleranta2']['priority'] = 113;
    $fields['billing']['billing_variante']['priority'] = 114;
    $fields['billing']['billing_boli']['priority'] = 115;
    $fields['billing']['billing_rutina']['priority'] = 116;
    $fields['billing']['billing_rutina2']['priority'] = 117;
    $fields['billing']['billing_istoric']['priority'] = 118;
    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'js_sort_checkout_fields' );

1 个答案:

答案 0 :(得分:0)

您是否在模板中查找,也许里面有解决方案。我用这种方法解决了类似的问题。 最好的祝福。 奥利维尔·安杰洛兹·佩西(pulsy)