• Resolved lkras

    (@lkras)


    Hello.
    I do custom sorting of the catalog by a certain attribute. Please tell me how you can display this sorting in only one specific category and its child categories?

    I am attaching the code to create a custom sort.

    
    /* Sort criteria with attribute */
    add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
        global $wp_query;
        if (isset($_GET['orderby'])) {
            switch ($_GET['orderby']) :
                case 'pa_razmer-sazhencza' :
                    $args['order'] = 'ASC';
                    $args['meta_key'] = 'pa_razmer-sazhencza';
    				$args['orderby'] = 'meta_value';
                break;
            endswitch;
        }
        return $args;
    }
     
    /* Adding a sorting method to the dropdown list */
    add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
     
    function custom_woocommerce_catalog_orderby( $sortby ) {
        $sortby['pa_razmer-sazhencza'] = 'По высоте';
        return $sortby;
    }
     
    /* Saving Attributes as Metadata*/
    add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
    function save_woocommerce_attr_to_meta( $post_id ) {
        foreach( $_REQUEST['attribute_names'] as $index => $value ) {
            update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] );
        }
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom sorting in one category’ is closed to new replies.