按日期对 WooCommerce 交叉销售进行排序
Sort WooCommerce cross-sells by date
Woocommerce 交叉销售显示在购物车页面上。默认情况下,它们是随机排序的。谁能帮我按日期(=产品发布日期)对它们进行排序?非常感谢!
这里是跨sells.php的内容:
<?php
/**
* Cross-sells
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/cross-sells.php.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $cross_sells ) : ?>
<div class="cross-sells">
<h2><?php _e( 'You may be interested in…', 'woocommerce' ) ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $cross_sells as $cross_sell ) : ?>
<?php
$post_object = get_post( $cross_sell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
解决方法如下:
您不必编辑交叉 sells.php!相反,您可以创建一个小插件来完成这项工作:
- 将以下代码放入新的 php 文件中。
- 将其上传到您的 WP 插件文件夹。
- 在WP后台激活插件。
- 在您的购物车页面上测试结果。现在将按发布日期订购您的交叉销售。
- 如果您想手动更改订单,只需通过 WP 后端在每个产品的产品页面上更改发布日期即可。
感谢 LoicTheAztec 为这个解决方案提供过滤器挂钩!
(通过为此过滤器挂钩创建插件,您不必编辑 function.php 或创建子主题。)
这是这个小插件的代码:
<?php
/**
* Plugin Name: Woocommerce Sort Cross-sales by Date
* Description: This plugin is used to sort cross-sells by date
* Author: ARaction GmbH with help of LoicTheAztec - no guarantee or support!
* Version: 0.1
*/
/* Your code goes below here. */
add_filter( 'woocommerce_cross_sells_orderby', 'custom_cross_sells_orderby', 10, 1 );
function custom_cross_sells_orderby( $orderby ){
$orderby = 'date';
return $orderby;
}
/* Your code goes above here. */
?>
要按日期订购交叉销售,您可以使用此过滤器挂钩:
add_filter( 'woocommerce_cross_sells_orderby', 'custom_cross_sells_orderby', 10, 1 );
function custom_cross_sells_orderby( $orderby ){
$orderby = 'date';
return $orderby;
}
代码进入活动子主题的 function.php 文件(活动主题或任何插件文件)。
已测试并有效。
Woocommerce 交叉销售显示在购物车页面上。默认情况下,它们是随机排序的。谁能帮我按日期(=产品发布日期)对它们进行排序?非常感谢!
这里是跨sells.php的内容:
<?php
/**
* Cross-sells
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/cross-sells.php.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $cross_sells ) : ?>
<div class="cross-sells">
<h2><?php _e( 'You may be interested in…', 'woocommerce' ) ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $cross_sells as $cross_sell ) : ?>
<?php
$post_object = get_post( $cross_sell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
解决方法如下:
您不必编辑交叉 sells.php!相反,您可以创建一个小插件来完成这项工作:
- 将以下代码放入新的 php 文件中。
- 将其上传到您的 WP 插件文件夹。
- 在WP后台激活插件。
- 在您的购物车页面上测试结果。现在将按发布日期订购您的交叉销售。
- 如果您想手动更改订单,只需通过 WP 后端在每个产品的产品页面上更改发布日期即可。
感谢 LoicTheAztec 为这个解决方案提供过滤器挂钩! (通过为此过滤器挂钩创建插件,您不必编辑 function.php 或创建子主题。)
这是这个小插件的代码:
<?php
/**
* Plugin Name: Woocommerce Sort Cross-sales by Date
* Description: This plugin is used to sort cross-sells by date
* Author: ARaction GmbH with help of LoicTheAztec - no guarantee or support!
* Version: 0.1
*/
/* Your code goes below here. */
add_filter( 'woocommerce_cross_sells_orderby', 'custom_cross_sells_orderby', 10, 1 );
function custom_cross_sells_orderby( $orderby ){
$orderby = 'date';
return $orderby;
}
/* Your code goes above here. */
?>
要按日期订购交叉销售,您可以使用此过滤器挂钩:
add_filter( 'woocommerce_cross_sells_orderby', 'custom_cross_sells_orderby', 10, 1 );
function custom_cross_sells_orderby( $orderby ){
$orderby = 'date';
return $orderby;
}
代码进入活动子主题的 function.php 文件(活动主题或任何插件文件)。
已测试并有效。