【发布时间】:2021-03-13 09:04:11
【问题描述】:
我有一些产品,我设置了一些交叉销售和追加销售产品。但也有交叉销售产品缺货的情况。 为了只显示可用的交叉销售和追加销售产品,我需要在 functions.php 中添加什么?
【问题讨论】:
标签: wordpress woocommerce hook-woocommerce
我有一些产品,我设置了一些交叉销售和追加销售产品。但也有交叉销售产品缺货的情况。 为了只显示可用的交叉销售和追加销售产品,我需要在 functions.php 中添加什么?
【问题讨论】:
标签: wordpress woocommerce hook-woocommerce
我检查了是否有任何钩子可用于修改要显示的产品,但我没有找到。我认为唯一的解决方案是修改各自的模板。
在这里找到他们:
/woocommerce/single-product/up-sells.php/woocommerce/cart/cross-sells.php将在每个 Upsells 和 Cross-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…', '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();
代码已经过测试并且可以运行。
【讨论】: