【问题标题】:Woocommerce - Add filter to display (or hide) custom checkout field if product ID == #Woocommerce - 添加过滤器以显示(或隐藏)自定义结帐字段,如果产品 ID == #
【发布时间】:2014-11-20 11:16:48
【问题描述】:

我创建了一个“my_custom_field”文本区域,作为默认的 billing_first_name、billing_address 等。现在如果产品 ID # 在购物车中,我想隐藏此字段。 因此,我需要检查 productID == #,然后从结帐中删除 my_custom_field。

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

【问题讨论】:

    标签: php wordpress woocommerce checkout custom-fields


    【解决方案1】:

    您可以试试这个,以适应您的自定义字段和产品 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 2020-02-22
      • 2023-03-19
      • 2022-11-05
      • 2021-06-11
      • 2019-01-21
      相关资源
      最近更新 更多