如何在WooCommerce中隐藏缺货产品以避免交叉销售和追加销售产品? [英] How to hide out of stock products from Cross-sells and Upsells products in WooCommerce?

查看:8
本文介绍了如何在WooCommerce中隐藏缺货产品以避免交叉销售和追加销售产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些产品,我已经设置了一些交叉销售和追加销售的产品。但有一种情况是,交叉销售的产品脱销。 要仅显示可用的交叉销售和追加销售产品,我需要将哪些内容放到unctions.php中?

推荐答案

我检查了是否有可用于修改要显示的产品的挂钩,但没有找到。我认为唯一的解决方案是修改各自的模板。

在此处查找它们:

  • /woocommerce/single-product/up-sells.php
  • /woocommerce/cart/cross-sells.php

将在每个向上销售交叉销售模板的循环中添加一个控件,以隐藏以下每个产品:

  • 缺货
  • 并且不允许延交

新模板复制到您活动的子项中 主题

交叉销售

您需要添加以下行作为Foreach中的第一个表达式:

<?php if ( ! $cross_sell->is_in_stock() && ! $cross_sell->backorders_allowed() ) : continue; endif; ?>

完整页面为:

<?php
/**
 * Cross-sells
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/cross-sells.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerceTemplates
 * @version 4.4.0
 */

defined( 'ABSPATH' ) || exit;

if ( $cross_sells ) : ?>

    <div class="cross-sells">
        <?php
        $heading = apply_filters( 'woocommerce_product_cross_sells_products_heading', __( 'You may be interested in&hellip;', 'woocommerce' ) );

        if ( $heading ) :
            ?>
            <h2><?php echo esc_html( $heading ); ?></h2>
        <?php endif; ?>

        <?php woocommerce_product_loop_start(); ?>

            <?php foreach ( $cross_sells as $cross_sell ) : ?>

                <?php if ( ! $cross_sell->is_in_stock() && ! $cross_sell->backorders_allowed() ) : continue; endif; ?>

                <?php
                    $post_object = get_post( $cross_sell->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 woocommerce_product_loop_end(); ?>

    </div>
    <?php
endif;

wp_reset_postdata();

追加销售

您需要添加以下行作为Foreach中的第一个表达式:

<?php if ( ! $upsell->is_in_stock() && ! $upsell->backorders_allowed() ) : continue; endif; ?>

完整页面为:

<?php
/**
 * Single Product Up-Sells
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerceTemplates
 * @version     3.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( $upsells ) : ?>

    <section class="up-sells upsells products">
        <?php
        $heading = apply_filters( 'woocommerce_product_upsells_products_heading', __( 'You may also like&hellip;', 'woocommerce' ) );

        if ( $heading ) :
            ?>
            <h2><?php echo esc_html( $heading ); ?></h2>
        <?php endif; ?>

        <?php woocommerce_product_loop_start(); ?>

            <?php foreach ( $upsells as $upsell ) : ?>

                <?php if ( ! $upsell->is_in_stock() && ! $upsell->backorders_allowed() ) : continue; endif; ?>

                <?php
                $post_object = get_post( $upsell->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 woocommerce_product_loop_end(); ?>

    </section>

    <?php
endif;

wp_reset_postdata();

代码已经过测试,可以正常工作。

这篇关于如何在WooCommerce中隐藏缺货产品以避免交叉销售和追加销售产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆