按日期排序WooCommerce交叉销售

时间:2017-10-10 16:30:37

标签: php wordpress sorting woocommerce cart

Woocommerce交叉销售显示在购物车页面上。默认情况下,它们随机排序。有谁可以帮我按日期排序(=产品发布日期)?非常感谢!

以下是cross-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();

这是解决方案:

您不必编辑cross-sells.php!相反,你可以创建一个小插件来完成这项工作:

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

1 个答案:

答案 0 :(得分: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文件中。

经过测试和工作。