2017-11-23 9 views
1

私は現在、WooCommerceを使用してWordPress eコマースウェブサイトで作業しています。チェックしたときにカスタムフィールドを表示する製品設定のカスタムチェックボックス

商品編集ページの一般設定でカスタムチェックボックスを作成しました。

また、単一の商品ページにカスタムテキストフィールドを表示するコードスニペットがあります。

このカスタムチェックボックスを選択して、このカスタムテキストフィールドを1つの商品ページに表示することをお勧めします。

コーディング私がこれまで持っている:

<?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'))「product-slug-a」を変更する方法はありますか?コードをfunctions.phpファイルに一度登録すると、製品カテゴリの1つに含まれるすべての製品に対して呼び出されますか?

答えて

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

あなたの詳細な答えをありがとう。コードは機能します。しかし、1つの軽微な問題は、WooCommerceで「彫刻」という言葉が何らかのデフォルトになっているということですか? 「彫刻」のすべての参照を「カスタム」と置き換えるたびに、カスタムテキストボックスは製品ページから消えます。 – Craig

+0

更新されたコードをありがとうございます。理由は分かりませんが、それはウェブサイトを破壊するようです。私はそれを把握することができるかどうか見てみましょう。 – Craig

+0

私は単純なものを見落としている場合は申し訳ありません。入力ミスがないように、コードをコピーして 'functions.php'ファイルに貼り付けるだけで、白い画面が表示されます。 – Craig

関連する問題