Sorting products in product archive using custom global attribute

  • Avatar von Unbekannt

    Hi everyone,

    I created a global attribute called ‚testkosten‘ and created only numeric attribute options like 50, 100, 150, 200. I assigned the attribute to some of my products and wanted to add a new sorting option in product archive which would sort based on ‚testkosten‘ in ascending order. I used the following code to add the new sorting option and make sorting available:

    // Add sorting option
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_product_sorting_options' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_product_sorting_options' );
    function custom_product_sorting_options( $options ) {
        $options['pa_testkosten'] = 'Sort by Testkosten';
        return $options;
    }
    
    // Add sorting arguments
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_product_sorting_args' );
    function custom_product_sorting_args( $args ) {
        $orderby_value = isset( $_GET['orderby'] ) ? $_GET['orderby'] : '';
    
        if ( 'pa_testkosten' === $orderby_value ) {
            $args['orderby'] = 'meta_value_num';
            $args['order'] = 'asc';
            $args['meta_key'] = 'pa_testkosten';
        }
    
        return $args;
    }

    The new sorting option is showing up in product archive page. However, if I select the new sorting option (Sort by Testkosten) no products are selected. Can someone help me to figure out what is wrong? Thank you in advance.

    Best regards

    Tobias

  • Avatar von Unbekannt

    Hallo zusammen,

    Habe jetzt erst gemerkt, dass ich in das deutsche Forum gepackt wurde. Also ich habe ein globale Eigenschaft als Text erstellt mit den Namen ‚Testkosten‘. Diese Eigenschaft hat nur numerische Optionen wie 50, 100, 150, 200. Diese habe ich ein paar Produkten zugeordnet. Dann habe ich den folgenden Code bei Code Snippet hinzugefügt:

    // Add sorting option
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_product_sorting_options' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_product_sorting_options' );
    function custom_product_sorting_options( $options ) {
        $options['pa_testkosten'] = 'Sort by Water';
        return $options;
    }
    
    // Add sorting arguments
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_product_sorting_args' );
    function custom_product_sorting_args( $args ) {
        $orderby_value = isset( $_GET['orderby'] ) ? $_GET['orderby'] : '';
    
        if ( 'pa_testkosten' === $orderby_value ) {
            $args['orderby'] = 'meta_value_num';
            $args['order'] = 'asc';
            $args['meta_key'] = 'pa_testkosten';
        }
    
        return $args;
    }

    Die neue Option zum Sortieren finde ich auf meiner Website wenn ich mir den Produktkatalgog anzeigen lasse. Wenn ich aber die Option auswähle, verschwinden alle Produkte und nichts ist ausgewählt obwohl ich ja die Eigenschaft ein paar Produkten zugewiesen hab. Könnt ihr mir helfen?

    Viele Grüße

    Tobias

  • Avatar von Unbekannt

    Welche URL ist betroffen?

  • Avatar von Unbekannt

    ecogu.net

    Ist noch nicht online, falls ich was freischalten soll kann ich das machen.

  • Avatar von Unbekannt

    Diese URL wird bei IONOS gehostet.
    Du bist daher hier im falschen Forum, bitte lesen:
    https://wordpress.com/de/forums/topic/hier-keine-selbstgehosteten-blogs/

  • Avatar von Unbekannt

    Ok danke, dann habe ich das mit dem selbst hosten falsch verstanden. Dachte, wenn ich nicht selbst hoste (sondern IONOS) wäre ich hier richtig. Danke für die Aufklärung und einen schönen Abend!

  • Das Thema ‘Sorting products in product archive using custom global attribute’ ist für neue Antworten geschlossen.