Support » Plugin: WooCommerce » Add external link views to Woocommerce sorting option

  • I built a working woocommerce sorting option which sorts by views. The problem is that it counts only views leading to the single product page. And people reach the product page only when they click on the product title. When they click on the image or add to cart button they are forwarded to another page. How can I add external link views to this sorting option? Heres my code:

    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 meta_value';
        $args['order'] = 'desc';
    
          }
    
        return $args;
    }

    Is there a hook like “woocommerce_before_external_link” or something? So i could add it.

  • You must be logged in to reply to this topic.