Woocommerce - 如果产品ID ==,添加过滤器以显示(或隐藏)自定义结帐字段

时间:2014-11-20 11:16:49

标签: php wordpress woocommerce checkout custom-fields

我创建了一个“my_custom_field”textarea,默认为billing_first_name,billing_address等。如果产品ID#在购物车中,我想隐藏此字段。 所以,我需要检查productID ==#,然后从结帐中删除my_custom_field。

否则(可能更好?),我可以检查productID ==#,并为该特定ID(或类别)创建自定义字段。 你有什么建议?

1 个答案:

答案 0 :(得分:0)

您可以尝试此操作,以适应您的自定义字段和产品ID

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields ( $fields ){

    if ( count( WC()->cart->get_cart() ) == 0 ) {
        return $fields;
    }

    foreach ( WC()->cart->get_cart() as $key => $item ) {
        if( in_array( $items[ 'product_id' ], array('1','2','3') ) ){
            unset( $fields[ 'my_custom_field' ] );
            break;
        }
    }

    return $fields;
}