如何在WooCommerce中隐藏交叉销售和追加销售产品中的缺货产品?

苗栗

我有一些产品,并且设置了一些交叉销售和向上销售的产品。但是,有时会出现交叉销售产品缺货的情况。为了只显示可用的交叉销售和向上销售产品,我需要在functions.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 WooCommerce\Templates
 * @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     WooCommerce\Templates
 * @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();

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用交叉销售产品在WooCommerce谢谢页面上追加销售

来自分类Dev

在 Woocommerce 中,如何查找某个产品是交叉销售的所有产品?

来自分类Dev

在Woocommerce中隐藏“缺货”产品

来自分类Dev

在WooCommerce中在相关产品之前显示交叉销售

来自分类Dev

WooCommerce:在商店中显示正在销售的产品

来自分类Dev

WooCommerce-不要在购物车页面上显示交叉销售的缺货产品

来自分类Dev

如何在以下Woocommerce缺货产品文本中添加单个产品上的Woocommerce产品价格

来自分类Dev

如何在报告销售订单中打印产品变体

来自分类Dev

如何在Postgres中找到交叉销售产品

来自分类Dev

产品之间的关联或交叉销售 %

来自分类Dev

在Woocommerce中隐藏特定类别档案中的缺货产品

来自分类Dev

Woocommerce - 如何删除价格并为追加销售和相关产品添加类别?

来自分类Dev

WooCommerce:仅在商店中显示正在销售的产品

来自分类Dev

从取消的WooCommerce订单中减少产品总销售数量

来自分类Dev

Woocommerce:如何仅显示计划销售的产品

来自分类Dev

如何从销售订单中获得产品价格?

来自分类Dev

在SQL中显示每天的产品销售

来自分类Dev

商店交易中销售最多的产品

来自分类Dev

如何根据产品类别自动设置交叉销售

来自分类Dev

Magento:依赖追加销售产品

来自分类Dev

如何在 Magento 2 中将交叉销售产品显示为 product.info.details

来自分类Dev

我如何获得在magento的可配置产品中不销售的产品?

来自分类Dev

如何获得在magento的可配置产品中不销售的产品?

来自分类Dev

如何在Magento CE 1.7中获取产品的缺货状态?

来自分类Dev

如何在WooCommerce中隐藏免费产品的支付网关?

来自分类Dev

Woocommerce:如何仅显示预定销售的产品

来自分类Dev

如何禁用特定用户的 WooCommerce 产品个人销售?

来自分类Dev

如何为woocommerce产品的每个销售单位在订单电子邮件中添加唯一编号

来自分类Dev

从WooCommerce管理中的所有产品中删除销售价格

Related 相关文章

  1. 1

    使用交叉销售产品在WooCommerce谢谢页面上追加销售

  2. 2

    在 Woocommerce 中,如何查找某个产品是交叉销售的所有产品?

  3. 3

    在Woocommerce中隐藏“缺货”产品

  4. 4

    在WooCommerce中在相关产品之前显示交叉销售

  5. 5

    WooCommerce:在商店中显示正在销售的产品

  6. 6

    WooCommerce-不要在购物车页面上显示交叉销售的缺货产品

  7. 7

    如何在以下Woocommerce缺货产品文本中添加单个产品上的Woocommerce产品价格

  8. 8

    如何在报告销售订单中打印产品变体

  9. 9

    如何在Postgres中找到交叉销售产品

  10. 10

    产品之间的关联或交叉销售 %

  11. 11

    在Woocommerce中隐藏特定类别档案中的缺货产品

  12. 12

    Woocommerce - 如何删除价格并为追加销售和相关产品添加类别?

  13. 13

    WooCommerce:仅在商店中显示正在销售的产品

  14. 14

    从取消的WooCommerce订单中减少产品总销售数量

  15. 15

    Woocommerce:如何仅显示计划销售的产品

  16. 16

    如何从销售订单中获得产品价格?

  17. 17

    在SQL中显示每天的产品销售

  18. 18

    商店交易中销售最多的产品

  19. 19

    如何根据产品类别自动设置交叉销售

  20. 20

    Magento:依赖追加销售产品

  21. 21

    如何在 Magento 2 中将交叉销售产品显示为 product.info.details

  22. 22

    我如何获得在magento的可配置产品中不销售的产品?

  23. 23

    如何获得在magento的可配置产品中不销售的产品?

  24. 24

    如何在Magento CE 1.7中获取产品的缺货状态?

  25. 25

    如何在WooCommerce中隐藏免费产品的支付网关?

  26. 26

    Woocommerce:如何仅显示预定销售的产品

  27. 27

    如何禁用特定用户的 WooCommerce 产品个人销售?

  28. 28

    如何为woocommerce产品的每个销售单位在订单电子邮件中添加唯一编号

  29. 29

    从WooCommerce管理中的所有产品中删除销售价格

热门标签

归档