2017-02-08 4 views
1

カスタムフィールド、カスタムタブとそれの内容とWooCommerceカスタム製品です:バックエンドの製品タブでカスタムフィールドのラベル名を取得

私はこのタブで最初のテキストフィールドをサンプリングしています。 目標は、これらのフィールドの「ラベル」プロパティを取得することです。

function launch_product_tab_content() { 
    global $post; 
    ?><div id='launch_contents' class='panel woocommerce_options_panel'><?php 
     ?><div class='options_group'><?php 
      woocommerce_wp_text_input(array(
       'id'   => '_text_announced', 
       'label'   => __('Announced(Global)', 'woocommerce'), 
       'desc_tip'  => 'true', 
       'description' => __('Year and Month it was announced global', 'woocommerce'), 
       'type'   => 'text', 
      )); 

      woocommerce_wp_text_input(array(
       'id'   => '_text_announced_ph', 
       'label'   => __('Announced(Philippines)', 'woocommerce'), 
       'desc_tip'  => 'true', 
       'description' => __('Year and Month it was announced global', 'woocommerce'), 
       'type'   => 'text', 
      )); 

      woocommerce_wp_text_input(array(
       'id'   => '_text_availability_ph', 
       'label'   => __('Availability(Philippines)', 'woocommerce'), 
       'desc_tip'  => 'true', 
       'description' => __('Schedule date of availability in the Philippines', 'woocommerce'), 
       'type'   => 'text', 
      )); 
     ?></div> 
    </div><?php 
} 
add_action('woocommerce_product_data_panels', 'launch_product_tab_content'); 

これは、それが製品エディタのページでどのように見えるかのWordpressのカスタム製品である:今

WooCommerce Custom Tab with Custom Text Field

、ACFを使用して、私はこのコードを使用:

<?php 
    $field_key = "_text_announced"; 
    $post_id = $post->ID; 
    $field = get_field_object($field_key, $post_id); 
echo $field['label'] . ': ' . $field['value']; 


         ?> 

も試しましたecho var_dump($field);

enter image description here

誰かが、WooCommerceプロジェクトがACFオブジェクトにバインドされていないと言いましたか?そのため、私はACF経由でWooCommerceオブジェクトにアクセスできません。あなたの考え。

ありがとうございます!私が持っている

+0

何をしたいですか?申し訳ありませんが、達成したいことを理解できません。 –

+1

ここにACFはどこに入っていますか?私はそれが使われているのを見ていない。 – staypuftman

+0

@RaunakGupta WooCommerceのカスタムテキストフィールドの "label"プロパティにAdvanced Custom Fieldsを使ってアクセスしたいだけです。 –

答えて

6

UPDATE(保存して、ラベル名を取得するWORKIG溶液)

はあなたのラベル名を持つ隠し入力プログラムのフィールドを追加し、あなたのコードでは、いくつかの変更を行います。データの保存/送信時には、も自動的にラベル名を保存します。すべてのプラグインファイルでも

// ADDING A TAB TO WOOCOMMERCE PRODUCT DATA METABOX 
add_filter('woocommerce_product_data_tabs', 'launch_product_tab_content_tab' , 99 , 1); 
function launch_product_tab_content_tab($product_data_tabs) { 
    $product_data_tabs['launch'] = array(
     'label' => __('Launch', 'my_text_domain'), 
     'target' => 'launch_contents', 
    ); 
    return $product_data_tabs; 
} 

// ADDING A FIELDS INSIDE THE TAB IN WOOCOMMERCE PRODUCT DATA METABOX 
add_action('woocommerce_product_data_panels', 'launch_product_tab_content'); 
function launch_product_tab_content() { 
    global $woocommerce, $post; 

    // Setting here your labels 
    $label_text_announced  = __('Announced(Global)', 'woocommerce'); 
    $label_text_announced_ph = __('Announced(Philippines)', 'woocommerce'); 
    $label_text_availability_ph = __('Availability(Philippines)', 'woocommerce'); 

    ?> 
    <div id='launch_contents' class='panel woocommerce_options_panel'> 
     <div class='options_group'> 
    <?php 

     woocommerce_wp_text_input(array(
      'id'   => '_text_announced', 
      'label'   => $label_text_announced, 
      'desc_tip'  => 'true', 
      'description' => __('Year and Month it was announced global', 'woocommerce'), 
      'type'   => 'text', 
     )); 

     woocommerce_wp_text_input(array(
      'id'   => '_text_announced_ph', 
      'label'   => $label_text_announced_ph, 
      'desc_tip'  => 'true', 
      'description' => __('Year and Month it was announced global', 'woocommerce'), 
      'type'   => 'text', 
     )); 

     woocommerce_wp_text_input(array(
      'id'   => '_text_availability_ph', 
      'label'   => $label_text_availability_ph, 
      'desc_tip'  => 'true', 
      'description' => __('Schedule date of availability in the Philippines', 'woocommerce'), 
      'type'   => 'text', 
     )); 

     // Addind hidden imputs fields for your labels 
     echo '<input type="hidden" id="text_announced_label" name="text_announced_label" value="'.$label_text_announced.'" /> 
     <input type="hidden" id="text_announced_ph_label" name="text_announced_ph_label" value="'.$label_text_announced_ph.'" /> 
     <input type="hidden" id="text_availability_ph_label" name="text_availability_ph_label" value="'.$label_text_availability_ph.'" />'; 

    ?> 
     </div> 
    </div> 
    <?php 
} 

// SAVING THE FIELDS DATA from THE TAB IN WOOCOMMERCE PRODUCT DATA METABOX 
add_action('woocommerce_process_product_meta', 'save_launch_product_tab_content'); 
function save_launch_product_tab_content($post_id){ 

    // Saving the data with the hidden data labels names 

    if(isset($_POST['_text_announced'])){ 
     update_post_meta($post_id, '_text_announced', $_POST['_text_announced']); 
     update_post_meta($post_id, '_text_announced_label', $_POST['text_announced_label']); 
    } 

    if(isset($_POST['_text_announced_ph'])){ 
     update_post_meta($post_id, '_text_announced_ph', $_POST['_text_announced_ph']); 
     update_post_meta($post_id, '_text_announced_ph_label', $_POST['text_announced_ph_label']); 
    } 

    if(isset($_POST['_text_availability_ph'])){ 
     update_post_meta($post_id, '_text_availability_ph', $_POST['_text_availability_ph']); 
     update_post_meta($post_id, '_text_availability_ph_label', $_POST['text_availability_ph_label']); 
    } 

} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに行くか:

はここで完全なコードです。

一度提出(SAVED)すべてのデータは、現在の製品ID(偶数ラベル名)、あなたはこのデータベース・テーブル(製品のIDで何を得る怒鳴る見るためwp_postmetaテーブルに設定されています)ここで99です:

enter image description here

をだから今、あなたのラベル名と対応するデータ値を取得することができます...

今ここで

そのプロセスを自動化し、配列内のこれらの値を設定する関数:

function get_label_and_value($product_id, $meta_key){ 
    // As the meta_key of the label have the same slug + '_label' we get it here 
    $key_label = $meta_key . '_label'; 

    // Getting the values 
    $meta_value = get_post_meta($product_id, $meta_key, true); 
    $label_name = get_post_meta($product_id, $key_label, true); 

    // Setting this data in an array: 
    $result = array('label' => $label_name, 'value' => $meta_value); 

    // Returning the data array 
    return $result; 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに行くのか、また、任意のプラグインファイルに保存します。

今、私たちはすべてのPHPファイルでこの機能を使用することができます。

<?php 
    // The product ID 
    $product_id = $product_id; 

    // The field key 
    $field_key = "_text_announced"; 

    // Using our function 
    $field1 = get_label_and_value($product_id, $field_key); 

    // Displaying the data (just as you expected to do) 
    echo $field1['label'] . ': ' . $field1['value']; 

?> 

をそして、あなたが取得します:

Announced(Global): April 2016 

だからここにACFの必要はありません

このコードをされますテストされ、動作します...

+0

こんにちは、これを削除しないでください。私はまだこれに復帰します –

+0

まだ目標は、カスタムプロパティ、カスタムテキスト入力で、woocommerceカスタム製品で宣言されたラベルプロパティを取得することです。私は後でそれらの値を取得するので。 ACFを介してそれらを取得することを望んでいました。 –

+1

@RJRamirezどこにも格納されていないため、関数やメソッドによってラベルを取得することはできません。使用しているカスタムコードでは、対応するフィールドにのみ定義されたラベルが表示されます。あなたのラベルはちょうど表示されていますが、商品データフィールドを送信しても、どこにも保存されません。 – LoicTheAztec

関連する問題