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

kiayqfof  于 2023-10-15  发布在  PHP
关注(0)|答案(2)|浏览(111)

我想根据产品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的每个字段。
任何帮助将不胜感激。

pcrecxhr

pcrecxhr1#

您的代码尝试根据购物车中的项目确定当前产品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添加每个字段。

cidc1ykv

cidc1ykv2#

在你的代码中有一些错误,不必要的和缺失的东西:
请改用以下重新访问的代码:

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文件中(或插件中)。测试和作品。

相关问题