【问题标题】:How to hide out of stock products from Cross-sells and Upsells products in WooCommerce?如何从 WooCommerce 中的交叉销售和追加销售产品中隐藏缺货产品?
【发布时间】:2021-03-13 09:04:11
【问题描述】:

我有一些产品,我设置了一些交叉销售和追加销售产品。但也有交叉销售产品缺货的情况。 为了只显示可用的交叉销售和追加销售产品,我需要在 functions.php 中添加什么?

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce


    【解决方案1】:

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

    在这里找到他们:

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

    将在每个 UpsellsCross-sells 模板的循环中添加一个控件,以隐藏以下产品:

    • 缺货
    • 并且不允许延期交货

    新模板将被复制到您的活动child theme

    交叉销售

    您需要添加以下行作为 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();
    

    代码已经过测试并且可以运行。

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 2016-06-15
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多