WooCommerce中有一項功能可以從商店中隱藏缺貨產品。但是,只有當我使用本地WooCommerce簡碼時,此功能纔有效,如果其他插件正用於在網站的其他部分顯示產品,則此功能無效。已發貨產品垃圾
因此,我認爲搗毀產品將是一個更好的方法來擺脫缺貨產品的好處。 我不希望永久刪除以防萬一我想使工作更輕鬆並在將來恢復這些產品,但如果沒有其他方式,我會歡迎它。
我只是在學習一點PHP。如果您有任何想法,請告知我。
WooCommerce中有一項功能可以從商店中隱藏缺貨產品。但是,只有當我使用本地WooCommerce簡碼時,此功能纔有效,如果其他插件正用於在網站的其他部分顯示產品,則此功能無效。已發貨產品垃圾
因此,我認爲搗毀產品將是一個更好的方法來擺脫缺貨產品的好處。 我不希望永久刪除以防萬一我想使工作更輕鬆並在將來恢復這些產品,但如果沒有其他方式,我會歡迎它。
我只是在學習一點PHP。如果您有任何想法,請告知我。
如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
文件,每當到儀表板並訪問任何後端頁面此功能將觸發。
請注意:建議只使用上述任何一種方法的 一個時間,或當你只需要取消註釋功能使用 ,然後再發表評論吧。
希望這有助於!
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>';
}
哇這是智能編碼。然而,我會,我必須手動刪除產品相同。一旦股票達到零點,產品就會消失。如果沒有辦法將它發送到垃圾箱,我會使用永久刪除。 –
@Ryon如果有用,你可以投票 –
謝謝你的幫助。代碼沒有完全解決問題,所以我使用@Raunak Gupta解決方案。 –
謝謝您的幫助,但它並沒有垃圾的產品。我在想這裏可能有問題「if(!$ product-> is_in_stock())」。我知道一些C#轉移知識我會認爲這將是IF產品=「outofstock」然後垃圾 –
@Ryon:你想垃圾所有現有的outofstock產品?你有簡單的產品或變量產品?和'is_in_stock()'是一個返回'bool'的方法。 –
所有現有產品也應該被刪除。所有產品都很簡單。 –