在WooCommerce中取消有条件的自定义结账字段

3
在 WooCommerce 结账表单中,我已经自定义了一些字段。但是,如果产品属于特定的类别,则不想显示这些字段。我已经实现了这部分内容。
接下来,我想取消一些自定义结账字段,但是我无法弄清楚如何做到这一点。取消普通的结账字段很容易,例如我使用以下代码:
unset( $fields['billing']['billing_company'] );

然而对于自定义的结账字段,它无法正常工作。以下是我设置此自定义字段的方式:

    function company_details_section($checkout)
{
    woocommerce_form_field('delegate_1_name', array(
        'type' => 'text',
        'class' => array(
            'my-field-class form-row-wide delegateExists'
        ) ,
        'label' => __('Full name') ,
    ) , $checkout->get_value('delegate_1_name'));
}

由于这属于自己的部分,而不是账单、运送或常规的WooCommerce额外字段,我找不到删除它的方法。
我已经尝试了以下操作:
unset( $fields['delegate_1_name'] );
unset( $fields['additional']['delegate_1_name'] );
unset( $fields['billing']['delegate_1_name'] );
unset( $fields['shipping']['delegate_1_name'] );
unset( $fields['company_details_section']['delegate_1_name'] );

这是我正在使用的条件函数来取消设置字段:

function wc_ninja_product_is_in_the_cart() {
    // Add your special product IDs here
    $ids = array( '45', '70', '75' );;

    // Products currently in the cart
    $cart_ids = array();

    // Find each product in the cart and add it to the $cart_ids array
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $cart_product = $values['data'];
        $cart_ids[]   = $cart_product->id;
    }

    // If one of the special products are in the cart, return true.
    if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
        return true;
    } else {
        return false;
    }
}

以下是取消普通结账字段的功能代码:

function wc_ninja_remove_checkout_field( $fields ) {
    if ( ! wc_ninja_product_is_in_the_cart() ) {
        unset( $fields['billing']['billing_company'] );
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );

如何在WooCommerce中取消自定义结帐字段(不是经典字段)?

你在什么时候取消它们? - inarilo
请参见上面的编辑。 - RD2411
你尝试过打印输出 $fields 吗? - inarilo
1个回答

1

无法取消自定义结帐字段,因为它不像默认的经典 WooCommerce 结帐字段一样被设置在数组中。

因此,您应该直接在自定义字段创建代码中使用条件函数。我还稍微修改了您现有的代码:

// Conditional function: If one of the special products is in cart return true, else false
function is_in_cart() {
    // Add your special product IDs here
    $ids = array( 45, 70, 75 );

    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if( in_array( $cart_item['data']->get_id(), $ids ) )
            return true;
    }
    return false;
}

add_filter( 'woocommerce_checkout_fields' , 'remove_checkout_fields', 10, 1 );
function remove_checkout_fields( $fields ) {
    if( ! is_in_cart() )
        unset($fields['billing']['billing_company']);

    return $fields;
}

add_action( 'woocommerce_after_order_notes', 'company_details_section', 10, 1 );
function company_details_section( $checkout ){
    // Here your conditional function
    if( is_in_cart() ){

        echo '<div id="my_custom_checkout_field"><h3>' . __('Company Details') . '</h3>';

        woocommerce_form_field( 'delegate_1_name', array(
            'type' => 'text',
            'class' => array( 'my-field-class form-row-wide delegateExists' ),
            'label' => __('Full name') ,
        ) , $checkout->get_value( 'delegate_1_name' ) );

        echo '</div>';
    }
}

代码放在您的活动子主题(或主题)的function.php文件中,也可以放在任何插件文件中。

已测试并适用于所有WooCommerce版本,自2.5.x以来。


谢谢,我会尝试一下并告诉你我的进展。我曾经认为使用现有的方法不可能实现,所以非常感谢。 - RD2411
1
工作得很好,抱歉回复慢了,最近一直忙于这个网站,再次感谢! - RD2411

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接