2016-12-23 6 views
1

배열의 제품이 카트 필드의 값과 일치하면 array1_ids를 고객의 카트와 비교하려고합니다. x_name이 없으면이 필드는 없어야합니다. 이 없어야합니다. 내 카트 및 array1_ids에서 값을 가져옵니다. 그러나 array_intersect를 통해 값을 가져 오면 NULL이 반환되어 항상 true를 반환합니다.Array_intersect가 NULL을 반환합니다.

function wc_ninja_product_is_in_the_cart() { 
/*array 1*/ 
$array1_ids = array('1', '3', '5');//field that should 
/*array 2*/ 
//$micro_ids = array('2', '4');//fields that shouldnt come back 

    // Products currently in the cart 
    $cart_ids = array(); 
    $cart_categories = 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 (! array_intersect($array1_ids, $cart_ids)) { 
     echo "true: " , implode(';',$cart_ids);;//bug fixing 
     return true; 
    } else { 
     return false; 
     echo "false: " , implode(';',$cart_ids);;//bug fixing 
    } 
} 
//Field Remover 
function wc_ninja_remove_checkout_field($fields) { 
    if (! wc_ninja_product_is_in_the_cart()) { 
     //removes Field x_name 
     unset($fields['billing']['x_name']); 
    } 
    return $fields; 
} 
add_filter('woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field'); 
+0

가 있는지를인가'$ card_ids'는 요소가 ? – Jerodev

+0

array_intersect()를 통해 실행하지 않으면 모든 것이 값을 반환합니다. – user7329477

답변

1

이 작동합니다 :

$hasSpecialProduct= false;  
foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
    $cart_product = $values['data']; 
    if (in_array($cart_product->id, $array1_ids)) { 
     $hasSpecialProduct = true; 
    } 
    $cart_ids[] = $cart_product->id; 
} 

// If one of the special products are in the cart, return true. 
if ($hasSpecialProduct) { 
    echo "true: " , implode(';',$cart_ids);;//bug fixing 
    return true; 
} else { 
    return false; 
} 

당신이 마지막 부분을 짧게 만들 수 있습니다 여기에

내 코드입니다

return $hasSpecialProduct; 
+1

대단히 감사합니다. 어쨌든 스택 오버 플로우에 대해 새로운 것이므로 upvote 할 수 없습니다. 어쨌든 당신의 대답을 받아 들였습니다. – user7329477