wordpress - 如何从 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…', '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();
该代码已经过测试并且可以工作。
推荐阅读
- excel - 使用 excel vba 添加数据透视表
- ios - xcode 中没有这样的模块“SwiftSVG”
- java - Java ExecutorService 一次性拉取所有任务
- javascript - 如何通过其值jquery增加一个数字
- reactive-programming - WhenAnyValue:如何在 select 中调用异步函数
- r - 关于在 R 中使用 Caret 包的 LASSO 逻辑回归的截距项
- jenkins - 在 Jenkins 中看不到参数选项
- c# - 如何在 ASP.NET MVC 中传递数据而不在 URL 中显示
- c# - 如何在移动设备上滑动时获得 -1 到 1 之间的值,如 Input.GetAxis("Horizontal") 所做的那样
- reactjs - 反应可加载,preloadReady 不加载模块