如何根据产品 ID 显示自定义 WooCommerce 计费字段

问题描述 投票:0回答:2

我想根据产品 ID 在结帐页面上显示/隐藏自定义字段。因此,我使用以下代码:

function display_custom_checkout_fields_based_on_product( $fields ) {
    $product_ids_participating_location = array( 5503 );
    $product_ids_toxicology = array( 5453, 5454, 5455 );
    $product_ids_artificial_intelligence = array( 5453, 5454, 5455, 5496 );

    $current_product_id = 0;

    if ( WC()->cart && WC()->cart->get_cart() ) {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product = $cart_item['data'];
            if ( $product && is_a( $product, 'WC_Product' ) ) {
                $current_product_id = $product->get_id();
                break;
            }
        }
    }

    unset( $fields['billing_participating_location'] );
    unset( $fields['billing_toxicology'] );
    unset( $fields['billing_artificial_intelligence'] );

    if ( in_array( $current_product_id, $product_ids_participating_location ) ) {
        $fields['billing_participating_location'] = array(
            'label'    => 'Participating Location',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    if ( in_array( $current_product_id, $product_ids_toxicology ) ) {
        $fields['billing_toxicology'] = array(
            'label'    => 'Toxicology',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    if ( in_array( $current_product_id, $product_ids_artificial_intelligence ) ) {
        $fields['billing_artificial_intelligence'] = array(
            'label'    => 'Artificial Intelligence',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'display_custom_checkout_fields_based_on_product' );

但它没有按预期工作。

我只想显示定义数组中相关产品 ID 的每个字段。

任何帮助将不胜感激。

php wordpress woocommerce hook-woocommerce woocommerce-theming
2个回答
0
投票

您的代码尝试根据购物车中的商品确定当前的产品 ID。这种方法假设购物车中一次只能有一种产品。如果您的购物车中有多个产品,则只会使用第一个产品的产品 ID。要处理多个产品,您应该收集数组中的所有产品 ID,然后应用条件逻辑。

这是代码的修改版本:

function display_custom_checkout_fields_based_on_product( $fields ) {
    $product_ids_participating_location = array( 5503 );
    $product_ids_toxicology = array( 5453, 5454, 5455 );
    $product_ids_artificial_intelligence = array( 5453, 5454, 5455, 5496 );

    $current_product_ids = array();

    if ( WC()->cart && WC()->cart->get_cart() ) {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product = $cart_item['data'];
            if ( $product && is_a( $product, 'WC_Product' ) ) {
                $current_product_ids[] = $product->get_id();
            }
        }
    }

    // Remove fields by default
    unset( $fields['billing_participating_location'] );
    unset( $fields['billing_toxicology'] );
    unset( $fields['billing_artificial_intelligence'] );

    // Check if fields should be added based on current product IDs
    if ( array_intersect( $current_product_ids, $product_ids_participating_location ) ) {
        $fields['billing_participating_location'] = array(
            'label'    => 'Participating Location',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    if ( array_intersect( $current_product_ids, $product_ids_toxicology ) ) {
        $fields['billing_toxicology'] = array(
            'label'    => 'Toxicology',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    if ( array_intersect( $current_product_ids, $product_ids_artificial_intelligence ) ) {
        $fields['billing_artificial_intelligence'] = array(
            'label'    => 'Artificial Intelligence',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'display_custom_checkout_fields_based_on_product' );

这样,它会收集 $current_product_ids 数组中的所有当前产品 ID,然后使用 array_intersect 来确定是否应根据产品 ID 添加每个字段。


0
投票

您的代码中存在一些错误、不必要和遗漏的内容:

请改用以下重新访问的代码:

add_filter( 'woocommerce_checkout_fields', 'display_custom_checkout_fields_based_on_cart_items' );
function display_custom_checkout_fields_based_on_cart_items( $fields ) {
    $participating_location_ids  = array( 5503 );
    $toxicology_ids              = array( 5453, 5454, 5455 );
    $artificial_intelligence_ids = array( 5453, 5454, 5455, 5496 );

    $item_ids = array(); // Initializing

    // Collect cart items product IDs
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $item_ids[] = $cart_item['product_id'];

        if ( $cart_item['variation_id'] > 0 ) {
            $item_ids[] = $cart_item['variation_id'];
        }
    }

    // Check Ids to display 'billing_participating_location' field
    if ( count( array_intersect( $item_ids, $participating_location_ids ) ) > 0 ) {
        $fields['billing']['billing_participating_location'] = array(
            'label'    => 'Participating Location',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }
    // Check Ids to display 'billing_toxicology' field
    if ( count( array_intersect( $item_ids, $toxicology_ids ) ) > 0 ) {
        $fields['billing']['billing_toxicology'] = array(
            'label'    => 'Toxicology',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }
    // Check Ids to display 'billing_artificial_intelligence' field
    if ( count( array_intersect( $item_ids, $artificial_intelligence_ids ) ) > 0 ) {
        $fields['billing']['billing_artificial_intelligence'] = array(
            'label'    => 'Artificial Intelligence',
            'required' => true,
            'class'    => array( 'form-row-wide' ),
            'clear'    => true,
        );
    }

    return $fields;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.