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

时间:2020-10-27 08:15:54

标签: php wordpress woocommerce product

我只想在WooCommerce单一产品页面上显示“有货”相关产品。

我知道我可以通过“我的主题”覆盖single-product/related.php模板文件。以下是此模板的相关代码:

<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>

是否可以对此文件进行一些更改以仅在WooCommerce单一产品页面上显示“有货”相关产品?任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

更新

因为当前四个产品缺货时,它不会显示任何内容(如果显示了默认的四个产品)您可以使用以下代码段代替覆盖模板文件。 >

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 );


覆盖模板文件

总是有多种解决方案,但是其中一种可能是通过覆盖模板文件来实现的。

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

  • 可以通过将该模板复制到yourtheme/woocommerce/single-product/related.php
  • 来覆盖它

替换

<?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; ?>

使用

<?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; ?>

答案 1 :(得分:1)

您可以使用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;
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

相关:Customize related products with a custom meta query in Woocommerce

答案 2 :(得分:1)

这是我一直在使用的,它对我有用:

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;
  }
}