Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1468987

    Hi all,
    I need to order posts in the Woocommerce Slider integrated in Enfold by a custom meta, which happens a date.
    I am not sure I understand the filter capability, but I tried the short plugin below, with no effect. The custom ordering is not appearing in the dropdown menu. I am overiding the priority of Enfold ordering by a priority, but I would be more than happy to use more elegant solution if possible, and this does not work anyway…

    <?php
    
    // Add custom sorting option to Enfold's sorting dropdown
    add_filter( 'avf_wc_product_order_dropdown_frontend', 'add_custom_sorting_option' );
    
    function add_custom_sorting_option( $product_order ) {
        $product_order['course_date'] = __( 'Sort by course date', 'woocommerce-course-date-sorting' );
        return $product_order;
    }
    
    // Modify the ordering arguments after Enfold's modifications
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 30 );
    
    function custom_catalog_ordering_args( $args ) {
        if ( isset( $_GET['product_order'] ) && 'course_date' === $_GET['product_order'] ) {
            $order = isset( $_GET['product_sort'] ) ? strtoupper( $_GET['product_sort'] ) : 'ASC';
            $args['orderby'] = 'meta_value';
            $args['order'] = $order;
            $args['meta_key'] = '_course_date';
            $args['meta_type'] = 'DATE';
        }
        return $args;
    }
    
    // Modify the product query to include only products with '_course_date' meta key
    add_action( 'woocommerce_product_query', 'custom_woocommerce_product_query' );
    
    function custom_woocommerce_product_query( $query ) {
        if ( ! is_admin() && $query->is_main_query() && is_shop() ) {
            if ( isset( $_GET['product_order'] ) && 'course_date' === $_GET['product_order'] ) {
                $meta_query = $query->get( 'meta_query' );
                if ( ! is_array( $meta_query ) ) {
                    $meta_query = array();
                }
                $meta_query[] = array(
                    'key'     => '_course_date',
                    'compare' => 'EXISTS',
                );
                $query->set( 'meta_query', $meta_query );
            }
        }
    }
    

    Can you please guide me on how to tackle this properly? Thanks, Jakub

    #1469195

    Hey jakubc15,

    Thank you for the inquiry.

    What do you mean by “Woocommerce Slider integrated in Enfold by a custom meta”? Please provide a screenshot and the link to the page containing the slider so that we can check it.

    Best regards,
    Ismael

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.