如何在 WooCommerce 中的产品发布日期添加自定义排序选项?

cyt*_*nny 1 php wordpress product woocommerce

我正在尝试遵循本指南,并根据 WooCommerce 中产品的发布日期选项进行自定义排序。简而言之,添加自定义过滤器的方式应该是:

add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_publish_date' );
function enable_catalog_ordering_by_publish_date( $args ) {
    if ( isset( $_GET['orderby'] ) ) {
        if ( 'modified_date' == $_GET['orderby'] ) {
            return array(
                'orderby'  => 'publish_date', // this is not working. What should be the correct field name?
                'order'    => 'DESC',
            );
        }
    }
    return $args;
}

add_filter( 'woocommerce_catalog_orderby', 'add_custom_sorting_options' );

function add_custom_sorting_options( $options ){
    
    $options[ 'publish_date' ] = 'Sort by Publish Date';
    
    return $options;
}
Run Code Online (Sandbox Code Playgroud)

我尝试在互联网上搜索并找到了有关可用于排序的字段的文档,但似乎发布日期不在列表中。上面的代码不起作用(它只是回退到默认的按 id 排序),所以我的猜测是published_date字段名称不正确。我可以从 WooCommerce 的管理页面看到发布日期,所以我认为数据应该可用。发布日期的字段名称应该是什么?或者我们需要使用其他方式来做到这一点?

7uc*_*f3r 5

由于 WooCommerce 产品是 WordPress 帖子类型,因此您可以使用post_date

所以你得到:

// Ordering products based on the selected values
function filter_woocommerce_get_catalog_ordering_args( $args, $orderby, $order ) {    
    switch( $orderby ) {
        case 'publish_date':
            $args['orderby']  = 'post_date';
            $args['order']    = 'ASC';
            break;
    }

    return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'filter_woocommerce_get_catalog_ordering_args', 10, 3 );

// Orderby setting
function filter_orderby( $orderby ) {
    $orderby['publish_date'] = __( 'Sort by publish date', 'woocommerce' );
    return $orderby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'filter_orderby', 10, 1 );
add_filter( 'woocommerce_catalog_orderby', 'filter_orderby', 10, 1 );
Run Code Online (Sandbox Code Playgroud)

更多信息:WP_Query - 订单和订单依据参数


概念证明:

// Debug info on WooCommerce shop and archives pages. Delete afterwards!
function action_woocommerce_after_shop_loop_item() {
    // Get the global product object
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Product ID
        $product_id = $product->get_id();

        // Retrieve post published or modified time as a DateTimeImmutable object instance.
        $published = get_post_datetime( $product_id, 'date' );
        $modified = get_post_datetime( $product_id, 'modified' );

        // Actual date that the post had been created
        $wp_old_date = get_post_meta( $product_id, '_wp_old_date' );

        // Output
        echo '<p>Published: ' . $published->format( 'Y-m-d' ) . '</p>'; 
        echo '<p>Modified: ' . $modified->format( 'Y-m-d' ) . '</p>';
        echo '<p>WP old date</p>'; 
        echo '<pre>', print_r( $wp_old_date, 1 ), '</pre>';
    }
}
add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 5 );
Run Code Online (Sandbox Code Playgroud)

概念证明