• Hi, I have WOO ?7.3.0 ver. I have many products with zero price…. for some reason I cant delete/draft it. I need just hide it from frontend. I do this:

    
    function hide_zero_price_products( $query ) {
        if (   is_woocommerce() && $query->is_main_query()   ) {
            $query->set( 'meta_query', array(
                array(
                    'key'     => '_regular_price',
                    'compare' => '>',
                    'value'   => 0,
                ),
            ) );
        }
    }
    

    But I steel see zero price product in woocommerce_related_products widget

    I try many code variations…. but cant hide zero price products from this widget:

    add_filter( 'woocommerce_related_products_args', function( $args ) 
    {
        unset( $args['post__in'] );
        $args['meta_query'] = array( 
        
           'key'     => '_regular_price',
                    'compare' => '>',
                    'value'   => 0,
        
        );
        return $args;
    });
    
    and
    
    add_action( 'woocommerce_product_query', 'themelocation_product_query' );
    function themelocation_product_query( $q ){
    $meta_query = $q->get( 'meta_query' );
        $meta_query[] = array(
                    'key'       => '_regular_price',
                    'value'     => 0,
                    'compare'   => '>'
                );
    $q->set( 'meta_query', $meta_query );
    }
    
     
    
    
    

    Any tips?

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you use a commercial theme or plugin and need support, please go to their official support channel. In order to be good stewards of the WordPress community, and encourage innovation and progress, we feel it’s important to direct people to those official locations.

    https://woo.com/contact-us/

    Forum volunteers are also not given access to commercial products, so they would not know why your commercial theme or plugin is not working properly. This is one other reason why volunteers forward you to the commercial product’s vendors. The vendors are responsible for supporting their commercial product.

    Thread Starter yessoftmk

    (@yessoftmk)

    If you use a commercial theme or plugin

    no, its standard woo function:

    woocommerce_output_related_products()

    Thread Starter yessoftmk

    (@yessoftmk)

    done with hardcode (/plugins/woocommerce/templates/single-product/related.php):

    <?php foreach ( $related_products as $related_product ) : ?>
    
    					<?php
    					
    //check price					
    if ($related_product->regular_price >0){
    					
    					$post_object = get_post( $related_product->get_id() );
    
    					setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found
    
    					wc_get_template_part( 'content', 'product' );
    					}
    					?>
    
    			<?php endforeach; ?>
    
    Moderator bcworkz

    (@bcworkz)

    You will want to avoid altering plugin code. Your efforts will be undone during updates. You can override a Woo template by copying it to a particular path in your theme. A modified template there is safe provided your theme is not subject to periodic updates itself. If it is, you’ll want to create a child theme for your modified template.

    Modifying Woo templates is less than ideal even if the override is in a safe place. Woo frequently updates template files and you’d need to keep your modified file up to date yourself.

    Ideally you’d want to utilize some filter or action hook to modify behavior. If you can do it this way, it’s the least amount of required ongoing maintenance. It’s possible to alter the related products query to exclude products with zero cost. You can use the “pre_get_posts” action, but your callback must confirm that it’s only acting upon a related products query.

    I’m unsure where Woo stores product pricing. If it’s in post meta, you could set a “meta_query” query var so only products with prices > 0 are found. “pre_get_posts” is a general action hook used for any kind of posts query. There might be a more specific Woo hook you could use that I’m unaware of. You can ask for further guidance through the dedicated support forum for WC. But if you’ve purchased any theme or extension from WooCommerce, you’ll likely be better served through the link that Liz D provided.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide woocommerce zero price in related products’ is closed to new replies.