2014-03-19 71 views
0

我有一個插件在特定的日期(稱爲WooCommerce有限的交易)隱藏產品。該插件將產品的可見性設置爲隱藏。我想要做的就是將產品設置爲草稿,因爲如果隱藏可見性,產品仍然顯示在搜索結果中並且可以直接訪問。您可以在下面找到將其設置爲「隱藏」的插件代碼。是否可以添加一條也將其設置爲「草稿」的線?WordPress的/ WooCommerce設置後/產品草案

update_post_meta($post->ID, '_visibility', 'hidden'); 

例如(我知道這是行不通的)類似這樣的東西:

update_post_meta($post->ID, 'status', 'draft'); 

功能(更新)

public function check_single() 
{ 
    global $post; 

    if ($this->is_enabled($post->id) && !$this->is_available($post->id)) { 

     // Set visibility to hidden 
     $hide = get_option('tp_hide_expired'); 
     if ($hide == 'yes') { 
      update_post_meta($post->ID, '_visibility', 'hidden'); 
      wp_update_post(array(
       'ID' => $post->ID, 
        'post_status' => 'draft', 
      )); 
     } 

     // Remove the add to cart button 
     $disable = get_option('tp_disable_cart'); 
     if ($disable == 'yes') { 
      remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 
      add_action('woocommerce_single_product_summary', array('WC_Limited_Deals', 'custom_add_to_cart'), 30); 
     } 
    } 
} 

回答

1

只需使用wp_update_post

http://codex.wordpress.org/Function_Reference/wp_update_post

wp_update_post(array(
    'ID' => $post->ID, 
    'post_status' => 'draft', 
)); 
+0

嘗試過,但沒有奏效。它仍然將隱藏的可見性設置爲隱藏,但它沒有將其狀態設置爲草稿。也許會發生這種情況,因爲它是一個有趣的商業產品(不知道如果woo商業產品與WordPress的帖子是一樣的)。 – manosim

+0

你在哪裏試圖使用這個?請粘貼更大的代碼片段。如果使用正確,上面的代碼片段應該可以工作。 –

+0

對不起,延遲迴復。用代碼更新。 – manosim