2017-03-16 39 views
-1

WooCommerce中有一项功能可以从商店中隐藏缺货产品。但是,只有当我使用本地WooCommerce简码时,此功能才有效,如果其他插件正用于在网站的其他部分显示产品,则此功能无效。已发货产品垃圾

因此,我认为捣毁产品将是一个更好的方法来摆脱缺货产品的好处。 我不希望永久删除以防万一我想使工作更轻松并在将来恢复这些产品,但如果没有其他方式,我会欢迎它。

我只是在学习一点PHP。如果您有任何想法,请告知我。

回答

0

如WordPress的所有的东西都被视为后不同 post_type,所以每当一个柱/产品更新​​是 调用。

function wh_trashOutOfStockProduct($post_id, $post, $update) { 

    $post_type = get_post_type($post_id); 

    // If this isn't a 'product' post, don't update it. 
    if ('product' != $post_type) 
     return; 
    $product = wc_get_product($post_id); 
    //if product is Out of Stock trash it 
    if (!$product->is_in_stock()) { 
     wp_trash_post($post_id); 
    } 
} 

add_action('save_post', 'wh_trashOutOfStockProduct', 10, 3); 

代码放在function.php文件的活跃儿童主题(或主题)的。或者也可以在任何插件php文件中使用。


修订
一次使用功能

add_action('wp', 'wh_trashAllProductOnce'); 

function wh_trashAllProductOnce() 
{ 
    $params = [ 
     'posts_per_page' => -1, 
     'post_type' => 'product' 
    ]; 
    $wc_query = new WP_Query($params); 

    if ($wc_query->have_posts()) : 
     while ($wc_query->have_posts()) : 
      $wc_query->the_post(); 
      $product_id = get_the_ID(); 
      $product = wc_get_product($product_id); 
      //if product is Out of Stock trash it 
      if (!$product->is_in_stock()) 
      { 
       wp_trash_post($product_id); 
      } 
     endwhile; 
     wp_reset_postdata(); 
    else: 
     _e('No Products'); 
    endif; 
} 

添加上面的代码在活动主题functions.php文件并运行自己的网站只有一次,然后删除上面的代码,它将会把所有缺货产品都废弃掉。


修订

如果你想保持代码为更长的时间然后让减少查询和执行的时间。

add_action('admin_init', 'wh_trashAllProductOnce'); 

function wh_trashAllProductOnce() { 
    $current_user = wp_get_current_user(); 
    //if loggedin user does not have admin previlage 
    if (!user_can($current_user, 'administrator')) { 
     return; 
    } 
    $params = [ 
     'posts_per_page' => -1, 
     'post_type' => 'product', 
     'post_status' => 'publish' 
    ]; 
    $wc_query = new WP_Query($params); 
    if ($wc_query->have_posts()) : 
     while ($wc_query->have_posts()) : 
      $wc_query->the_post(); 
      $product_id = get_the_ID(); 
      $product = wc_get_product($product_id); 
      //if product is Out of Stock trash it 
      if (!$product->is_in_stock()) { 
       wp_trash_post($product_id); 
      } 
     endwhile; 
     wp_reset_postdata(); 
    endif; 
} 

管理员登录加入上面的代码在活动主题functions.php文件,每当到仪表板并访问任何后端页面此功能将触发。

请注意:建议只使用上述任何一种方法的 一个时间,或当你只需要取消注释功能使用 ,然后再发表评论吧。

希望这有助于!

+0

谢谢您的帮助,但它并没有垃圾的产品。我在想这里可能有问题“if(!$ product-> is_in_stock())”。我知道一些C#转移知识我会认为这将是IF产品=“outofstock”然后垃圾 –

+0

@Ryon:你想垃圾所有现有的outofstock产品?你有简单的产品或变量产品?和'is_in_stock()'是一个返回'bool'的方法。 –

+0

所有现有产品也应该被删除。所有产品都很简单。 –

0
add_filter('woocommerce_debug_tools', 'tools'); 
/** 
* Tools we add to WC 
* @param array $tools 
* @return array 
*/ 
function tools($tools) { 
    $tools['delete_products'] = array(
     'name'  => __('Delete OutofStock Products','your-text-domain'), 
     'button' => __('Delete OutofStock products','your-text-domain'), 
     'desc'  => __('This tool will delete OutofStock .', 'your-text-domain'), 
     'callback' => array($this, 'delete_products') 
    ); 
    return $tools; 
} 
/** 
* Delete OutofStock Products 
*/ 
function delete_products() { 
    global $wpdb; 
      $wpdb->query("DELETE p FROM {$wpdb->posts} p join {$wpdb->postmeta} pm on p.ID = pm.post_id WHERE p.post_type = 'product' and pm.meta_key='_stock_status' and pm.meta_value='outofstock'"); 
    echo '<div class="updated"><p>' . sprintf(__('Out of stock Products Deleted', 'your-text-domain')) . '</p></div>'; 
} 

您可以通过点击工具菜单上Woocommerce下做到这一点 - >系统状态,见截图enter image description here

+0

哇这是智能编码。然而,我会,我必须手动删除产品相同。一旦股票达到零点,产品就会消失。如果没有办法将它发送到垃圾箱,我会使用永久删除。 –

+0

@Ryon如果有用,你可以投票 –

+0

谢谢你的帮助。代码没有完全解决问题,所以我使用@Raunak Gupta解决方案。 –