2016-08-02 2 views
0

모든 스토어 사용자 정의 게시 유형에 대해 WooCommerce 제품에 재고 수준 필드를 만들고 싶습니다. 나는 이미 스토어 사용자 정의 게시물 유형을 만들고 3 개의 스토어를 추가했습니다. 누군가가 상점을 추가 할 때마다 상점 레벨의 재고를 확인할 수 있도록 "상점의 재고 레벨"필드를 자동으로 추가하려고합니다.모든 사용자 정의 유형에 대한 워드 프레스 추가 필드

재고 수량 아래의 제품 -> 재고 -> 오른쪽에 사용자 정의 필드를 넣으려고합니다.

나는이 시도했다 :

 $post_type = 'store'; 
     $tax = 'show-topic'; 
     $inv_arg_terms = get_terms(array('orderby' => 'id', 'order' => 'ASC')); 
     if ($inv_arg_terms) { 
      $args = array(
       'post_type' => $post_type, 
       'post_status' => 'publish', 
       'posts_per_page' => - 1, 
       'orderby' => 'title', 
       'order' => 'ASC' 
       ); // END $args 
      $my_query = null; 
      $my_query = new WP_Query($args); 
      if ($my_query->have_posts()) { 
       while ($my_query->have_posts()) : $my_query->the_post(); 

        add_action('woocommerce_product_options_inventory_product_data', 'wc_inventory_stock_product_field'); 
        function wc_inventory_stock_product_field() { 
         woocommerce_wp_text_input(array('id' => 'stock_level_' . the_title(), 'class' => 'short wc_input_stock', 'label' => __('Stock Level at ' . the_title(), 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')')); 
        } 

        add_action('save_post', 'wc_cost_save_product'); 
        function wc_cost_save_product($product_id) { 

         // stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature 
         if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce')) 
          return; 

         // If this is a auto save do nothing, we only save when update button is clicked 
         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
          return; 
         if (isset($_POST['stock_level_' . the_title()])) { 
          if (is_numeric($_POST['stock_level_' . the_title()])) 
           update_post_meta($product_id, 'stock_level_' . the_title(), $_POST['cost_price']); 
         } else delete_post_meta($product_id, 'stock_level_' . the_title()); 
        } 
       endwhile; 
      } // END if have_posts loop 
      wp_reset_query(); 
     } // END if $inv_arg_terms 

과 나는이있어 :

Fatal error: Call to undefined function get_userdata() in .../wp-includes/query.php on line 4758

나는 가능한 생각하고 무엇입니까? 어떻게해야합니까?

감사합니다. 얻을 수있는 모든 도움에 감사드립니다.

답변

0

마지막으로 작동하도록했습니다. 코드는 다음과 같습니다.

add_action('woocommerce_product_options_inventory_product_data', 'wc_stock_product_field'); 

add_action('woocommerce_process_product_meta', 'wc_stock_save_product'); 

function wc_stock_product_field() { 

    global $woocommerce, $post; 

$post_type = 'store'; 
$args = array(
    'post_type' => $post_type, 
    'post_status' => 'publish', 
    'posts_per_page' => - 1, 
    'orderby' => 'id', 
    'order' => 'ASC', 
    'caller_get_posts' => 1 
    ); // END $args 

$store_query = null; 
$store_query = new WP_Query($args); 

if ($store_query->have_posts()) { 
    while ($store_query->have_posts()) : $store_query->the_post(); 
     woocommerce_wp_text_input( 
      array( 
       'id' => get_the_id() , 
       'class' => 'short wc_input_stock', 
       'label' => __('Stock Level @ ' . get_the_title(), 'woocommerce'))); 
    endwhile; 
} // END if have_posts loop 
wp_reset_query(); 

} 

function wc_stock_save_product($product_id) { 

// stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature 
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce')) 
    return; 

// If this is a auto save do nothing, we only save when update button is clicked 
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
    return; 

    $post_type = 'store'; 
    $args = array(
     'post_type' => $post_type, 
     'post_status' => 'publish', 
     'posts_per_page' => - 1, 
     'orderby' => 'id', 
     'order' => 'ASC', 
     'caller_get_posts' => 1 
     ); // END $args 

    $store_query = null; 
    $store_query = new WP_Query($args); 

    if ($store_query->have_posts()) { 
     while ($store_query->have_posts()) : $store_query->the_post(); 
      if (isset($_POST[get_the_id()])) { 
       if (is_numeric($_POST[get_the_id()])) 
        update_post_meta($product_id, get_the_id(), $_POST[get_the_id()]); 
      } else delete_post_meta($product_id, get_the_id()); 
     endwhile; 
    } // END if have_posts loop 
    wp_reset_query(); 


} 
관련 문제