2017-10-10 77 views
1

在购物车页面上显示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&hellip;', '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!相反,您可以创建一个小插件来完成这项工作:

  1. 将以下代码放到新的php文件中。
  2. 将其上传到您的WP插件文件夹。
  3. 激活WP后端的插件。
  4. 在购物车页面上测试结果。您的交叉销售将立即按发布日期排序。
  5. 如果您想手动更改订单,只需通过产品页面上的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. */ 

?> 
+0

就在'if($ cross_sells):'之后,你可以做一个'print_r($ cross_sells)'并将输出添加到你的文章中吗? – Matthew

回答

1

要订购交主要销售的日期,你可以使用这个过滤钩子:

add_filter('woocommerce_cross_sells_orderby', 'custom_cross_sells_orderby', 10, 1); 
function custom_cross_sells_orderby($orderby){ 
    $orderby = 'date'; 
    return $orderby; 
} 

代码放在您的活动子主题的function.php文件(活动的主题或任何插件文件)。

经过测试和工作。

+0

非常感谢您的回答!奇迹般有效! – ARaction