Foreach 循环在找到单个项目后停止 运行

Foreach loop stops running after finding a single item

我有一个条件字段移除器,它可以很好地处理 id,但是当我尝试 运行 它与类别一起使用时,它会在得到结果后停止。

我已将此 tutorial 用作指南:

<?php
    /*=============Conditional field remover===========*/
    //Cart check
    function wc_ninja_product_is_in_the_cart() {
        /*p1 products*/
        $p1_cat = array( 'metal', 'wood');
        /*p2 products*/
        $p2_cat = array('bubblewrap');

    // Products currently in the cart
    $cart_ids = array();
    $cart_categories = array();
    //Declaring var
    $p1 = false;
    $p2 = false;
    //creating an array
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $cart_product = $values['data'];
        $cart_ids[] = $cart_product->id;
    }
    //filling array 
    foreach( $cart_ids as $id ) {
        $products_categories = get_the_terms( $id, 'product_cat');
    }
    //filling category
    foreach ($products_categories as $products_category)    {
        //Check
        if (in_array($products_category->slug, $p1_cat)) {
            $p1 = true;//Result
        }
        if (in_array($products_category->slug, $p2_cat)) {
            $p2 = true;//Result
        }
        $cart_categories[] = $products_category->slug;
    }
    //Returns result to function
    return array($p1, $p2);
}
//Field Remover
function wc_ninja_remove_checkout_field( $fields ) {
    //Gets value from function and places them in the array !PHP 7 reverses order!
    list($p1, $p2) = wc_ninja_product_is_in_the_cart();
    var_dump(wc_ninja_product_is_in_the_cart());
    //If check came back false it will remove the field in question
    if ( $p1 == false ) {
        //removes Field domain name
        unset( $fields['billing']['Field_1'] );
    }
    if ( $p2 == false ) {
        //Removes Field office_emails
        unset( $fields['billing']['Field_2'] );
    }
    //Return unset result
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );

如果添加了两个产品,上面的代码将只显示 Field_1(在测试用例中,金属盒 cat=>金属和包装 material cat=>bubblewrap,我已经检查了拼写。我做的其他事情是 var_dump 数组,它给了我:[0]=> string(5) "metal"

我试图让数组通过 i++;它确实将位置更改为 [x],其中 x 是产品数量 -1。正如我之前提到的,我制作了一个使用 ID 的版本,该 ID 在大商店中工作正常但不实用,所以我知道作品 return 数组工作正常。

我没有完全按照教程进行操作,过早地关闭了一个 foreach 循环,结果只 运行 执行了一次检查。本来应该是这样的,但我在//填充类别上方有一个 },导致循环中断。

foreach( $cart_ids as $id ) {
    $products_categories = get_the_terms( $id, 'product_cat');
    //filling category
    foreach ($products_categories as $products_category)    {

UPDATE:

This code is working, I have tested it.
Where is the code that you are using to create that 2 checkout custom fields?
I think that is your main problem Here. And the tutorial you are using is a little outdated and complicated for nothing. Here is the official WooCommerce tutorial for checkout fields

You should update your question whith that missing code and tell clearly what you want to do with that 2 checkout custom fields (and give some details about them).

要使您的字段移除器功能适用于类别,您应该使用 has_term() 功能作为条件,这样:

// Checkout Field Remover
add_filter( 'woocommerce_checkout_fields' , 'custom_cats_remove_checkout_field' );
function custom_cats_remove_checkout_field( $fields ) {
    // Initialising variables
    $cat1_found = false;
    $cat2_found = false;
    
    // Iterating throuh each cart item
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Cart item_id
        $cart_item_id = $cart_item['data']->id;
        
        // For first categories group
        if(has_term( array( 'metal', 'wood'), 'product_cat', $cart_item_id)){
            $cat1_found = true;
        }
        
        // For the second category
        if(has_term( 'bubblewrap', 'product_cat', $cart_item_id)){
            $cat2_found = true;
        }
    }
    //If check came back false it will remove the field in question
    if ( $cat1_found ) {
        // Removes Field domain name
        unset( $fields['billing']['Field_1'] );
    }
    if ( $cat2_found ) {
        //Removes Field office_emails
        unset( $fields['billing']['Field_2'] );
    }
    //Return unset result
    return $fields;
}

代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。