仅在Woocommerce中删除虚拟产品的订单备注结帐字段

时间:2018-09-26 09:00:41

标签: php wordpress woocommerce cart checkout

在Woocommerce中,我使用以下代码来禁用除“订购单”和“其他信息”标题之外的所有结帐字段。

如果购物车仅包含虚拟产品,则除了“名字”,“姓氏”和“电子邮件”字段外,不应显示其他任何字段。

代码:

add_filter( 'woocommerce_checkout_fields' , 'simple_virtual_checkout' );
function simple_virtual_checkout( $fields ) {
    $only_virtual_products = true;
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    // Check if there are non-virtual products
    if ( ! $cart_item['data']->is_virtual() ) $only_virtual_products = false; 
    }
    if( $only_virtual_products ) {
    // remove billing fields
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_phone']);
    add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

    // remove shipping fields 
    unset($fields['shipping']['shipping_first_name']);    
    unset($fields['shipping']['shipping_last_name']);  
    unset($fields['shipping']['shipping_company']);
    unset($fields['shipping']['shipping_address_1']);
    unset($fields['shipping']['shipping_address_2']);
    unset($fields['shipping']['shipping_city']);
    unset($fields['shipping']['shipping_postcode']);
    unset($fields['shipping']['shipping_country']);
    unset($fields['shipping']['shipping_state']);
    }
    return $fields;
}

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您只需要将此行添加到函数中即可:

unset($fields['order']['order_comments']);

所以您的代码将是:

add_filter( 'woocommerce_checkout_fields' , 'simple_virtual_checkout', 20, 1 );
function simple_virtual_checkout( $fields ) {
    $only_virtual_products = true;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check for non-virtual products
        if ( ! $cart_item['data']->is_virtual() ) 
            $only_virtual_products = false; 
    }

    if( $only_virtual_products ) {
        // remove billing fields
        unset($fields['billing']['billing_company']);
        unset($fields['billing']['billing_address_1']);
        unset($fields['billing']['billing_address_2']);
        unset($fields['billing']['billing_city']);
        unset($fields['billing']['billing_postcode']);
        unset($fields['billing']['billing_country']);
        unset($fields['billing']['billing_state']);
        unset($fields['billing']['billing_phone']);
        add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

        // remove shipping fields 
        unset($fields['shipping']['shipping_first_name']);    
        unset($fields['shipping']['shipping_last_name']);  
        unset($fields['shipping']['shipping_company']);
        unset($fields['shipping']['shipping_address_1']);
        unset($fields['shipping']['shipping_address_2']);
        unset($fields['shipping']['shipping_city']);
        unset($fields['shipping']['shipping_postcode']);
        unset($fields['shipping']['shipping_country']);
        unset($fields['shipping']['shipping_state']);
        unset($fields['shipping']['shipping_state']);

        // remove order notes
        unset($fields['order']['order_comments']);
    }
    return $fields;
}

代码进入活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

  

在Woocommerce结帐页面中,默认情况下,没有“其他信息”标题
  因此,这当然与您的主题或插件进行的一些自定义有关。