我有一些产品,并且设置了一些交叉销售和向上销售的产品。但也有交叉销售产品缺货的情况。 为了仅显示可用的交叉销售和追加销售产品,我需要将什么放入 functions.php 中?
答案 0 :(得分:1)
我检查了是否有任何挂钩可用于修改要显示的产品,但我没有找到。我认为唯一的解决方案是修改相应的模板。
在这里找到它们:
/woocommerce/single-product/up-sells.php
/woocommerce/cart/cross-sells.php
将在每个追加销售和交叉销售模板的循环中添加一个控件以隐藏每个产品:
新模板将被复制到您的活动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…', '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…', '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();
代码已经过测试并且可以正常工作。