php - 在 WooCommerce 中取消设置有条件的自定义结帐字段

标签 php wordpress woocommerce checkout custom-fields

在 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(不是经典的)中取消设置自定义结帐字段?

最佳答案

您不能取消设置自定义结帐字段,因为它没有设置在数组中,就像默认的经典 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 文件中或任何插件文件中。

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

关于php - 在 WooCommerce 中取消设置有条件的自定义结帐字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45894606/

相关文章:

php - PHP 中的裸字符串是否有任何合法用途?

php - 如何通过 AJAX 传递的 URL 向 mysql 请求数据

php - 如何以编程方式获取/设置产品价格?

javascript - 如何使用 wordpress 网站的引导轮播删除移动版本中的第一张幻灯片

mysql - 组合来自 MYSQL 的选择值,然后细化落在某个范围内的值

php - 如何定位除 WooCommerce 感谢页面之外的所有页面

php - 如何让 yii2 ActiveForm 忽略以前提交的值?

php - Windows 7 64 位上的 Xampp 安装错误

php - 带有子页面的 Wordpress 自定义帖子类型

html - Woocommerce 使用 CSS 在悬停时更改产品图片