简体   繁体   English

在 WooCommerce 单个产品页面上仅显示“有货”相关产品

[英]Show only "in stock" related products on WooCommerce single product page

I would like to show only "in stock" related products on WooCommerce single product pages.我想在 WooCommerce 单个产品页面上仅显示“有货”相关产品。

I know I can override single-product/related.php template file via My theme.我知道我可以通过我的主题覆盖single-product/related.php模板文件。 Here below is the related code for this template:下面是这个模板的相关代码:

<section class="related products">

    <h2><?php _e( 'You May Also Want', 'MyStore' ); ?></h2>

    <?php woocommerce_product_loop_start(); ?>

        <?php foreach ( $related_products as $related_product ) : ?>

            <?php
                $post_object = get_post( $related_product->get_id() );

                setup_postdata( $GLOBALS['post'] =& $post_object );

                wc_get_template_part( 'content', 'product' ); ?>
                
        <?php endforeach; ?>

    <?php woocommerce_product_loop_end(); ?>

</section>

Is it possible making some changes to this file to only show "in stock" related products on WooCommerce single product page?是否可以对此文件进行一些更改以仅在 WooCommerce 单个产品页面上显示“有货”相关产品? Any help is appreciated.任何帮助表示赞赏。

UPDATE更新

Because when the 4 first products are out of stock it does not display anything (In case the default 4 products are shown) you could use the following snippet instead of overwriting the template file .因为当前 4 个产品缺货时,它不会显示任何内容(如果显示默认的 4 个产品),您可以使用以下代码段而不是覆盖模板文件

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;
}
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );


Overwriting the template file覆盖模板文件

There are always multiple solutions but 1 of them could be by overwriting the template file.总是有多种解决方案,但其中一种可能是通过覆盖模板文件。

https://github.com/woocommerce/woocommerce/blob/02cf0dfaed5923513de0c88add597d1560c2cfd2/templates/single-product/related.php https://github.com/woocommerce/woocommerce/blob/02cf0dfaed5923513de0c88add597d1560c2cfd2/templates/single-product/related.php

  • This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php可以通过将其复制到yourtheme/woocommerce/single-product/related.php来覆盖此模板

Replace代替

<?php foreach ( $related_products as $related_product ) : ?>

        <?php
        $post_object = get_post( $related_product->get_id() );

        setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found

        wc_get_template_part( 'content', 'product' );
        ?>

<?php endforeach; ?>

With

<?php foreach ( $related_products as $related_product ) : ?>

    <?php
    $stock_status = $related_product->get_stock_status();

    if ( $stock_status == 'instock' ) {
        
        $post_object = get_post( $related_product->get_id() );

        setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found

        wc_get_template_part( 'content', 'product' );
    }
    ?>

<?php endforeach; ?>

Instead of editing template files, you can use woocommerce_product_related_posts_query dedicated filter hook to alter the query excluding "out of stock" products from displayed related products:您可以使用woocommerce_product_related_posts_query专用过滤器钩子来更改查询,从显示的相关产品中排除“缺货”产品,而不是编辑模板文件:

add_filter( 'woocommerce_product_related_posts_query', 'alter_product_related_posts_query', 10, 3 );
function alter_product_related_posts_query( $query, $product_id, $args ){
    global $wpdb;

    $query['join']  .= " INNER JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id ";
    $query['where'] .= " AND pm.meta_key = '_stock_status' AND meta_value != 'outofstock' ";

    return $query;
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

Related: Customize related products with a custom meta query in Woocommerce相关: 使用 Woocommerce 中的自定义元查询自定义相关产品

This is what I've been using, and it works for me:这是我一直在使用的,它对我有用:

add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids )
{

if (!is_admin()) {
    foreach ($related_product_ids as $key => $value) {
        $relatedProduct = wc_get_product($value);
        if (!$relatedProduct->is_in_stock() ) {
            unset($related_product_ids["$key"]);
        }
    }

    return $related_product_ids;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品 - WooCommerce: Show price suffix only on single product Page - Not for related products WooCommerce单品页面隐藏“缺货”相关产品 - Hide “out of stock” related products on WooCommerce single product page 在单个产品页面上增加相关产品(woocommerce) - Increase related products on single product page (woocommerce) 在 WooCommerce 的单个产品页面上为低库存产品添加 CSS 类 - Add CSS class for low stock products on single product page in WooCommerce 在WooCommerce单品页的相关商品标题添加URL - Add relative URL to related products title on WooCommerce single product page 在产品单页上显示自定义和标准产品属性 WooCommerce - Show custom AND standard products atrributes on Product Single page WooCommerce 将相关商品移至Woocommerce单品侧边栏 - Move related product to the sidebar on Woocommerce single products woocommerce-仅显示少数产品的库存数量 - woocommerce - Show stock quantity only on few products Woocommerce - 仅当有加售产品时才将 html 添加到单个产品页面 - Woocommerce - Add html to the single product page only if there are upsell products 如果一个类别中只有一个产品,则将其他类别中的随机 WooCommerce 产品显示为相关产品 - Show random WooCommerce products from other categories as related products if only one product in a category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM