아래 코드는 제품 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;
}
에서 발견되었다. 미리 감사드립니다.
잘 했어! 와우, 정말 고마워. 코드에 in_array를 추가해야만했습니다. 고마워요! –