【问题标题】:Send Out Of Stock Products to trash将缺货产品发送到垃圾箱
【发布时间】:2017-08-07 11:57:52
【问题描述】:

WooCommerce 中有一项功能可以隐藏商店中缺货的产品。但是,此功能仅在我使用本机 WooCommerce 短代码时有效,并且在使用其他插件在网站的其他部分显示产品时无效。

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

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

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:
       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 下的工具菜单上一键执行此操作 -> 系统状态,请参见截图

    【讨论】:

    • 哇,这是智能编码。但是,我必须手动删除产品。目标是一旦库存达到零,产品就会消失。如果无法将其发送到垃圾箱,我将使用永久删除。
    • 感谢您的帮助。代码并没有完全解决问题,所以我改用@Raunak Gupta 解决方案。
    【解决方案2】:

    在 WordPress 中,所有内容都被视为不同的帖子 post_type,所以每当更新帖子/产品时save_post 是 调用。

    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 文件中,每当管理员登录仪表板并访问任何后端页面时,都会触发此功能。

    请注意:建议使用以上任何一种方法 仅一次,或者在您需要时取消注释功能使用 然后再次评论它。

    希望这会有所帮助!

    【讨论】:

    • 感谢您的帮助,但它不会破坏产品。我认为这里“ if (!$product->is_in_stock())”可能存在问题。我知道一点 c# 转移知识我认为这将是 IF product = "outofstock" THEN 垃圾
    • @Ryon:你想丢弃所有现有的缺货产品吗?你有简单的产品还是可变产品?而is_in_stock() 是一个返回bool 的方法。
    • 所有现有产品也应该被丢弃。所有产品都很简单。
    • @Ryon:我已经更新了我的答案,请检查,但使用后记得删除上面的代码。
    • 我测试了它,看看为什么它是一次性代码。它不允许从垃圾箱中恢复任何产品,但是当代码被删除时,产品不会自动发送到垃圾箱。我决定始终保持代码处于活动状态,因为它解决了问题。请告诉我保留代码是否明智。
    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多