在購物車頁面上顯示Woocommerce交叉銷售。默認情況下它們是隨機排序的。誰能幫我按日期排序(=產品發佈日期)?非常感謝!按日期排序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. */
?>
就在'if($ cross_sells):'之後,你可以做一個'print_r($ cross_sells)'並將輸出添加到你的文章中嗎? – Matthew