2017-11-23 1 views
1

현재 WooCommerce를 사용하여 WordPress 전자 상거래 웹 사이트에서 작업하고 있습니다.선택시 사용자 정의 필드를 표시하는 제품 설정의 사용자 정의 확인란

제품 수정 페이지 일반 설정에서 맞춤 체크 박스를 만들었습니다.

단일 제품 페이지에 맞춤 텍스트 필드를 표시하는 코드 스 니펫이 있습니다.

이제이 사용자 지정 확인란을 제품에 대해 선택하면이 사용자 지정 텍스트 필드가 단일 제품 페이지에 표시됩니다.

코딩 내가 지금까지 가지고 :

<?php 
// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields'); 

// Save Fields 
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save'); 


function woocommerce_product_custom_fields(){ 
    global $woocommerce, $post; 
    echo '<div class="product_custom_field">'; 
    // Custom Product Checkbox Field 
    woocommerce_wp_checkbox(
     array(
      'id'   => '_custom_product_checkbox_field', 
      'placeholder' => 'Custom Product Checkbox Field', 
      'label'  => __('Custom Product Checkbox Field', 'woocommerce'), 
      'desc_tip' => 'true' 
     ) 
    ); 
echo '</div>'; 

} 

// Saving Values 
function woocommerce_product_custom_fields_save($post_id){ 
    // Custom Product Text Field 
    $woocommerce_custom_product_checkbox_field = $_POST['_custom_product_checkbox_field']; 
    if (!empty($woocommerce_custom_product_checkbox_field)) 
     update_post_meta($post_id, '_custom_product_checkbox_field', esc_attr($woocommerce_custom_product_checkbox_field)); 
} 
?> 

내가 가진 코드 :

가 WooCommerce 제품 대시 보드 내에서 체크 박스를 만들려면, 나는 functions.php 파일에 다음 코드를 입력 관련 사용자 지정 텍스트 상자 출력과 같이 functions.php 파일에 다음과 같이 입력합니다.

function add_engrave_text_field() { 
    if (is_single('product-url-a')) { 
     echo 'Enter your chosen letters: <span id="character_count"></span> 
     <div><table class="variations" cellspacing="0"> 
      <tbody> 
       <tr> 
        <td class="value"><label class="product-custom-text-label" for="custom_text">Custom Text</label></td> 
        <td class="value"> 
         <label><input type="text" class="product-counter" name="engrave_text" placeholder="Enter Your Custom Letters ..." maxlength="3" /></label>       
        </td> 
       </tr>        
      </tbody> 
     </table></div>'; 
    } 
} 
add_action('woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0); 

체크 박스를 선택했을 때만 사용자 지정 텍스트 상자 코딩이 출력되도록 다음 단계는 어떻게됩니까?

content-single-product에 사용자 지정 텍스트 상자의 코드를 넣을 수 있다는 것을 알고 있지만 사용자 지정 문자 등의 가격 책정을 다루는 많은 코딩 그룹의 일부이므로이 작업을 수행하지 않는 것이 이상적입니다. .

추가 질문 :

웹 사이트 5 개 제품 카테고리로 구성되어 있습니다. 이 카테고리 중 하나의 제품 만 사용자 정의 레터링을 허용합니다. 현재, 각 개별 제품에 위의 코딩을 수동으로 적용해야합니다. if (is_single('product-slug-a'))에 개별 슬러그를 삽입해야합니다. '제품 슬러그'를 변경할 수있는 방법이 있습니까? functions.php 파일에 코드를 한 번만 입력하면 제품 범주 중 하나의 제품에 대한 모든 제품이 호출됩니까?

답변

2

는 업데이트 :

나는 불필요한 코드를 제거하는 일을 단순화, 코드 비트를 재 방문했다. 체크 상자 설정 옵션을 선택한 경우에만이 "조각 영역"을 단일 제품 페이지에 표시하는 데 필요한 코드를 추가했습니다. (체크 박스 설정이없는 제품 카테고리에 대한) 최종 에서 마지막 질문 체크에 대한

. 어떤 플러그인 파일도

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add'); 
function product_custom_fields_add(){ 
    global $post; 

    echo '<div class="product_custom_field">'; 

    // Custom Product Checkbox Field 
    woocommerce_wp_checkbox(array(
     'id'  => '_engrave_text_option', 
     'desc'  => __('set custom Engrave text field', 'woocommerce'), 
     'label'  => __('Display custom Engrave text field', 'woocommerce'), 
     'desc_tip' => 'true' 
    )); 

    echo '</div>'; 
} 

// Save Fields 
add_action('woocommerce_process_product_meta', 'product_custom_fields_save'); 
function product_custom_fields_save($post_id){ 
    // Custom Product Text Field 
    $engrave_text_option = isset($_POST['_engrave_text_option']) ? 'yes' : 'no'; 
     update_post_meta($post_id, '_engrave_text_option', esc_attr($engrave_text_option)); 
} 

add_action('woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0); 
function add_engrave_text_field() { 
    global $post; 

    // If is single product page and have the "engrave text option" enabled we display the field 
    if (is_product() && get_post_meta($post->ID, '_engrave_text_option', true) == 'yes') { 

     ?> 
     <div> 
      <label class="product-custom-text-label" for="engrave_text"><?php _e('Engraving option:', 'woocommerce'); ?><br> 
       <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e('Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" /> 
      </label> 
     </div><br> 
     <?php 
    } 
} 

코드 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 : 여기

는 코드입니다.

테스트를 거쳐 작동합니다.

확인란 출력 및 저장 : Adding programatically data option tab to admin product pages Metabox

체크 박스 옵션이 제품 설정에 선택되어있는 경우는 다음과 같이 얻을 것이다 :

enter image description here


는이에 대한 작동 확인 제품 카테고리 또는 제품 태그 (설정 없음 확인란) :

add_action('woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0); 
function add_engrave_text_field() { 
    global $post; 

    // HERE set the term Ids, slug or name (or an array of values) 
    $categories = array('clothing', 'music'); 

    $taxonomy = 'product_cat'; // (For product tags use 'product_tag' instead) 

    // If is single product page and have the "engrave text option" enabled we display the field 
    if (is_product() && has_term($categories, 'product_cat', $post->ID)) { 

     ?> 
     <div> 
      <label class="product-custom-text-label" for="engrave_text"><?php _e('Engraving option:', 'woocommerce'); ?><br> 
       <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e('Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" /> 
      </label> 
     </div><br> 
     <?php 
    } 
} 

코드는 활성 자녀 테마 (또는 테마)의 function.php 파일 또는 모든 플러그인 파일에 있습니다.

테스트를 거쳐 작동합니다.

+0

좋습니다. 자세한 답변을 해 주셔서 감사합니다. 코드가 작동합니다. 그러나 한 가지 사소한 질문은 WooCommerce에서 '조각 (engrave)'이라는 용어의 기본값이 무엇인가라는 것입니다. '새기다'라는 모든 참조를 '사용자 정의'로 바꿀 때마다 사용자 정의 텍스트 상자가 제품 페이지에서 사라집니다. – Craig

+0

업데이트 된 코드를 이용해 주셔서 감사합니다. 이유는 모르겠지만 웹 사이트를 망가 뜨리는 것 같습니다. 내가 알아낼 수 있을지 알 겠어. – Craig

+0

죄송합니다. 단순한 것을 간과 할 경우. 필자가 오타를 입력하지 않았는지 확인하기 위해 코드를 복사하여'functions.php' 파일에 붙여 넣으면 흰색 화면이 나타납니다. – Craig

관련 문제