2014-10-16 7 views
0

아래 코드는 제품 ID 117이 장바구니에 있는지 확인합니다. 그럴 경우 추가 체크 아웃 필드가 표시됩니다.Woocommerce 변수 체크 아웃 필드의 변수 ID

변수 ID를 확인하는 대신 제품 ID를 확인하는 대신이 코드를 변환하는 방법을 알아 내려고합니다. 두 개의 변수가있는 두 개의 제품이 있습니다. 양식 필드를 볼 수있는 변수 ID는 7509 및 7529입니다. 이러한 변수를 선택하면이 필드를 채울 수있는 것으로 생각할 수있는 모든 것을 시도했습니다.

이 코드는 내가 제공 할 수있는 모든 도움을 주셔서 감사합니다 것 http://wordimpress.com/create-conditional-checkout-fields-woocommerce/

/** 
* Add the field to the checkout 
**/ 
add_action('woocommerce_after_order_notes', 'wordimpress_custom_checkout_field'); 

function wordimpress_custom_checkout_field($checkout) { 

//Check if Book in Cart (UPDATE WITH YOUR PRODUCT ID) 
$book_in_cart = wordimpress_is_conditional_product_in_cart(117); 

//Book is in cart so show additional fields 
if ($book_in_cart === true) { 
    echo '<div id="my_custom_checkout_field"><h3>' . __('Book Customization') . '</h3><p style="margin: 0 0 8px;">Would you like an inscription from the author in your book?</p>'; 

    woocommerce_form_field('inscription_checkbox', array(
     'type' => 'checkbox', 
     'class' => array('inscription-checkbox form-row-wide'), 
     'label' => __('Yes'), 
    ), $checkout->get_value('inscription_checkbox')); 

    woocommerce_form_field('inscription_textbox', array(
     'type' => 'text', 
     'class' => array('inscription-text form-row-wide'), 
     'label' => __('To whom should the inscription be made?'), 
    ), $checkout->get_value('inscription_textbox')); 

    echo '</div>'; 
} 

} 

/** 
* Check if Conditional Product is In cart 
* 
* @param $product_id 
* 
* @return bool 
*/ 
function wordimpress_is_conditional_product_in_cart($product_id) { 
//Check to see if user has product in cart 
global $woocommerce; 

//flag no book in cart 
$book_in_cart = false; 

foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
    $_product = $values['data']; 

    if ($_product->id === $product_id) { 
     //book is in cart! 
     $book_in_cart = true; 

    } 
} 

return $book_in_cart; 

} 

에서 발견되었다. 미리 감사드립니다.

답변

1

, 여러 variation_id의 배열로 전달할 확인하려면

if ($_product->id === $product_id) {

if ($_product->variation_id === $product_id) {

에 변경 한 다음 이전에 시도한 함수는 호출 함수에 1 (true)을 전송하므로 작동하지 않습니다.

wordimpress_is_conditional_product_in_cart(7509 || 7529) // This is incorrect, see @Howlin's answer for the correct way

+0

잘 했어! 와우, 정말 고마워. 코드에 in_array를 추가해야만했습니다. 고마워요! –

3

in_array을 사용해야합니다.

그래서 제품 ID 배열을 전달할
$book_in_cart = wordimpress_is_conditional_product_in_cart(117); 

변경.
$book_in_cart = wordimpress_is_conditional_product_in_cart(array(117,113)); 

는 제품 ID 배열 인 경우

if ($_product->id === $product_id) { 

확인할 변경.

if (in_array($_product->id, $product_id)) { 

장바구니의 제품이 배열에있는 경우 이러한 변경 사항을 적용하면 추가 필드가 표시됩니다. 당신은했듯이

모든 필요한 데이터는 장바구니에 저장됩니다
+0

제품 ID가 아닌 변수 ID를 찾으려고합니다. 나는 2 개의 변이를 가진 2 개의 제품이있다. 각 제품의 두 번째 변수에 추가 필드가 필요합니다. 변수 ID는 7509와 7529입니다. –

+0

도움을 주셔서 감사합니다. Howlin! –