Woocommerce - Add filter to display (or hide) custom checkout field if product ID == #

心已入冬 提交于 2019-12-12 03:16:07

问题


I created a "my_custom_field" textarea, as the default billing_first_name, billing_address, etc..now I'd like to hide this field if product id # is in cart. So, I need to check if productID == #, and so remove my_custom_field from the checkout.

Otherwise (maybe better?), I could check if productID == #, and create a custom field for that specific ID (or maybe categories). What do you suggest?


回答1:


You can try this, to adapt with your custom field and product IDs

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;
}


来源:https://stackoverflow.com/questions/27038232/woocommerce-add-filter-to-display-or-hide-custom-checkout-field-if-product-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!