• Resolved doreoo

    (@doreoo)


    Hello Guys,

    can you pls help me with a Problem. I use Woocommerce for my Affiliate Shop, so People dont buy the products on my site. Which means “Sort by Popularity dont work”. Now i want a Sort Option which Sort my Products after Products views. I already found this here in the internet:

    /**
    * Setting post count on each visit
    *
    */
    add_action( ‘woocommerce_before_single_product’, ‘prefix_save_product_views’ );
    function prefix_save_product_views( ) {

    $product_id = get_the_ID();
    $increment = 1;
    $current_visit_count = get_post_meta( $product_id, ‘product_visit_count’, true );

    $total_visit_count = (int)$current_visit_count + $increment;
    update_post_meta( $product_id, ‘product_visit_count’, $total_visit_count );

    }

    /**
    * Change the display order based on visit count only in Catalog
    *
    */
    add_filter(‘woocommerce_get_catalog_ordering_args’, ‘prefix_woocommerce_catalog_orderby’);
    function prefix_woocommerce_catalog_orderby( $args ) {
    $args[‘meta_key’] = ‘product_visit_count’;
    $args[‘orderby’] = ‘meta_value_num’;
    $args[‘order’] = ‘desc’;
    return $args;
    }

    It worked, but it automatically Sort by Products after Views, and dont add an extra Option. Can someone help me to transform this into an option. Thanks.

    Or is there any other working method?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support EtienneP a11n

    (@etiennep)

    Hi there!

    We have documentation here on how to add a custom sorting option to WooCommerce – https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/

    If you need help with adding that, we highly recommend contacting one of the services on our Customizations page (https://woocommerce.com/customizations/)

    Thread Starter doreoo

    (@doreoo)

    Thanks for the answer!

    Buit and added one by my own now with codes from the internet.

    add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
    function prefix_save_product_views(  ) {
    
        $product_id = get_the_ID();
        $increment = 1;
        $current_visit_count = get_post_meta( $product_id, 'product_visit_count', true );
    
        $total_visit_count = (int)$current_visit_count + $increment;
        update_post_meta( $product_id, 'product_visit_count', $total_visit_count );
    
    }
    
    add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' );
     
    function misha_add_custom_sorting_options( $options ){
     
    	$options['popular'] = 'Beliebteste';
    	
    	return $options;
     
    }
    
    add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' );
     
    function misha_custom_product_sorting( $args ) {
     
    	// Nach Aufrufen sortieren
    	if ( isset( $_GET['orderby'] ) && 'popular' === $_GET['orderby'] ) {
    	$args['meta_key'] = 'product_visit_count';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'desc';
    		}
        return $args;
    }

    Only Problem is that when I use this sort option some products or product rows show twice. With other sort options ( newest,price,etc…) i dont have this problem.

    Plugin Support EtienneP a11n

    (@etiennep)

    Thanks for letting me know!

    I tested you code on my site and I do not see the duplication of products and the sorting works well;

    Sort-options
    Link to image: https://d.pr/i/80LOlK

    Could you try on your site with our Storefront theme to see if you see the same?

    Thread Starter doreoo

    (@doreoo)

    Finally found the problem!

    I just changed ‘meta_value_num’ to ‘meta_value_num meta_value’. There were some people on the internet who had similar issues with their blog sites + infinite scroll.

    I took me days to built a complete working “views sorting option” because i dont have any php skills at all xD.

    Plugin Support EtienneP a11n

    (@etiennep)

    Nice! Thanks for letting me know!

    I’m going to mark this as resolved – if you have any further questions, you can start a new thread ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add: Sort Products by Views’ is closed to new replies.