【问题标题】:Hide all products with a specific stock status from WooCommerce catalog从 WooCommerce 目录中隐藏具有特定库存状态的所有产品
【发布时间】:2021-02-12 02:20:12
【问题描述】:

我在 Wordpress 和 Woocommerce 网站上使用 Flatsome 模板。我还创建了自定义库存状态(例如 noproduzione,当不再从制造商创建产品时使用)。但我不想在我的网站上(仅在管理页面中)显示此产品(noproduzione 库存状态)。

我正在使用我在这里编写的这段代码(我把它放在我的 functions.php 文件中),但似乎在 flatsome 上不起作用。如何在这个模板上应用?

add_action( 'woocommerce_product_query', 'qc_action_product_query', 10, 2 );
function qc_action_product_query( $q, $query ) {

    // Get any existing meta query
    $meta_query = $q->get( 'meta_query');
    
    // Define an additional meta query 
    $q->set( 'meta_query', array( array(
        'key'     => '_stock_status',
        'value'   => 'noproduzione',
        'compare' => 'NOT LIKE',
    ) ) );
    
    // Set the new merged meta query
    $q->set( 'meta_query', $meta_query );
}

为了创建自定义代码,我使用了 StackOverflow 上的一些代码

// Add new stock status options
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
function filter_woocommerce_product_stock_status_options( $status ) {
    // Add new statuses
    $status['10days'] = __( 'Disponibile entro 10 giorni', 'woocommerce' );
    $status['inarrivo'] = __( 'In arrivo', 'woocommerce' );
    $status['noproduzione'] = __( 'Fuori produzione', 'woocommerce' );

    return $status;
}
                  
// Availability text
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
function filter_woocommerce_get_availability_text( $availability, $product) {
    switch( $product->get_stock_status() ) {
        case '10days':
            $availability = __( 'Disponibile entro 10 giorni', 'woocommerce' );
        break;
        case 'inarrivo':
            $availability = __( 'In arrivo', 'woocommerce' ); 
        break;
    case 'noproduzione': 
    $availability = __( 'Fuori produzione', 'woocommerce' );  
    
        break;
    }
    return $availability;  
}

我添加了代码来显示可用性文本,如果产品是 noproduzione 库存状态,还隐藏购物车按钮

// Display the stock status on other page
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
function wcs_stock_text_shop_page() {
    //returns an array with 2 items availability and class for CSS
    global $product;

    $availability = $product->get_stock_status();
    //check if availability in the array = string 'noproduzione'
    //if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
    if ( $product->get_stock_status() === 'noproduzione') {
        echo '<span style="color:#b20000;">Fuori produzione!</span>';
    }
    else if ( $product->get_stock_status() === 'onbackorder') {
        echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
    }
    else if ( $product->get_stock_status() === '10days') {
        echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
    }
    else if ( $product->get_stock_status() === 'inarrivo') {
        echo '<span style="color:#e0c81d;">In arrivo</span>';
    }
    else if ( $product->get_stock_status() === 'outofstock') {
        echo '<span style="color:#b20000;">Terminato!</span>';
    }
    else {
        echo '<span style="color:#53af00;">Disponibile!</span>';
    }
}

// Hide the cart button if stock status is 'noproduzione'
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
    if ( $product->get_stock_status() === 'noproduzione' ) { 
        return false;
    }
    return $purchasable;
}

最后,我还添加了此代码,它很有用,因为按“库存”状态订购相关产品,对我来说很好,因为不显示其他自定义库存状态。但我想将此应用到我所有的前端站点。

//show, in the related products, only instock products!
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
function filter_woocommerce_related_products( $related_posts, $product_id, $args ) {    
    foreach( $related_posts as $key => $related_post ) {        
        // Get product
        $related_product = wc_get_product( $related_post );
        
        // Is a WC product 
        if ( is_a( $related_product, 'WC_Product' ) ) {
            // Stock status
            $stock_status = $related_product->get_stock_status();
            
            // NOT instock
            if ( $stock_status != 'instock' ) {
                unset( $related_posts[$key] );
            }
        }
    }
    
    return $related_posts;
}

所以,问题是:如何在我的网站上隐藏此产品(noproduzione 库存状态)(并且仅在管理页面中显示)? Noproduzione 与 OUTOFSTOCK 不同!

【问题讨论】:

  • 那么如何解决呢?我需要帮助:D
  • 亲爱的 LoicTheAztec,我编辑了所有帖子,包括我的代码。一些代码是从您在 stackoverflow 上的旧帖子答案中复制的!希望有一个有效的答案!我在寻找解决问题的方法时很疯狂!并且支持主题不回答我。
  • 亲爱的 Loic,有什么消息吗?
  • 我试过了(你的代码更新了),似乎它有效。但不是在相关产品中,我使用(解决问题)按库存订购,因此未显示其他产品。顺便说一句,谢谢!
  • 针对相关产品提出新问题

标签: php wordpress woocommerce product stock


【解决方案1】:

您最好使用专用的woocommerce_product_query_meta_query 过滤器挂钩,如下所示,从您的商店中隐藏所有具有特定库存状态的产品(适用于自定义库存状态)

add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
    if ( ! is_admin() ) {
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'noproduzione',
            'compare' => '!=',
        );
    }
    return $meta_query;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。自 WooCommerce 3 以来的所有版本都经过测试和工作。

【讨论】:

  • 亲爱的 LoicTheAztec 我正在尝试您的代码,但我想向“值”添加更多值。尝试使用“值”=> 数组(“noproduzione”,“10days”),但它给出了我的错误
猜你喜欢
  • 1970-01-01
  • 2022-08-24
  • 2018-06-09
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
相关资源
最近更新 更多