cytsunny
cytsunny

Reputation: 5030

How to add custom sort option on published date of product in WooCommerce?

I am trying to follow this guide and make a custom sort by option on published date of product in WooCommerce. In short, the way of adding a custom filter should be:

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;
}

I have tried to search on the internet and found this documentation on what are the fields that I can use for sorting, but it seems that the published date is not in the list. The code above is not working (it just falls back to the default sort by id), so my guess on published_date as the field name is not correct. I can see the publish date from the admin page in WooCommerce, so I think the data should be available. What should be the field name for the published date? Or we will need to do that using other ways?

Upvotes: 1

Views: 1879

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

Since WooCommerce product is a WordPress post type, you can use post_date

So you get:

// 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 );

More info: WP_Query - Order & Orderby Parameters


Proof of concept:

// 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 );

Proof of concept

Upvotes: 5

Related Questions