WordPress.org

Ready to get started?Download WordPress

Forums

in_category condition not working (6 posts)

  1. brianhre
    Member
    Posted 10 months ago #

    Hello. I`m trying to display a label with specific text for products that belong to two categories (Green and Oolong). All other categories will display the same text. I am having trouble getting this to work correctly.

    <?php if ( in_category('Green') ) { ?>
    
        <label><?php _e( 'Quantity - 150 G', 'yit' ) ?></label>
    
    <?php } elseif ( in_category('Oolong') ) { ?>
    
        <label><?php _e( 'Quantity - 150 G', 'yit' ) ?></label>
    
    <?php } else { ?>
    
    <label><?php _e( 'Quantity', 'yit' ) ?></label>
    
    <?php } ?>

    Any help would be appreciated.

  2. bcworkz
    Member
    Posted 10 months ago #

    How is it not working for you? The code shows the special text for any posts that have the category green and/or oolong assigned. Is this not what you want?

    This code is on a template inside the Loop, yes? A link to a page demonstrating the problem might help.

  3. brianhre
    Member
    Posted 10 months ago #

    Thanks for your reply bcworkz. It is not working in the sense that the Category "Green" or "Oolong" is only displaying "Quantity" rather than the label with the weight. Here is the complete page code below. Maybe there is an issue with my syntax that I am missing. Thank for any assistance! :)

    <?php
    
    global $woocommerce, $product;
    
    if ( ! $product->is_purchasable() ) return;
    ?>
    
    <?php
    	// Availability
    	$availability = $product->get_availability();
    
    	if ($availability['availability']) :
    		echo apply_filters( 'woocommerce_stock_html', '<p class="stock '.$availability['class'].'">'.$availability['availability'].'</p>', $availability['availability'] );
        endif;
    ?>
    
    <?php if ( $product->is_in_stock() && is_shop_enabled() ) : ?>
    
    	<?php do_action('woocommerce_before_add_to_cart_form'); ?>
    
    	<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype='multipart/form-data'>
    
    	 	<?php do_action('woocommerce_before_add_to_cart_button'); ?>
    
    	 	<?php if ( ! $product->is_sold_individually() ){   ?>            
    
                    <label> <?php if ( in_category('Green') ) { ?>
    
        <label><?php _e( 'Quantity - 150 G', 'yit' ) ?></label>
    
    <?php } elseif ( in_category('Oolong') ) { ?>
    
        <label><?php _e( 'Quantity - 150 G', 'yit' ) ?></label>
    
    <?php } else { ?>
    
    <label><?php _e( 'Quantity', 'yit' ) ?></label>
    
    <?php } ?></label>
    
    				<?php
    	 			woocommerce_quantity_input( array(
    	 				'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
    	 				'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
    	 			) );
    			}
    	 	?>
    
    	 	<button type="submit" class="single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'yit' ), $product->product_type); ?></button>
    
    	 	<?php do_action('woocommerce_after_add_to_cart_button'); ?>
    
    	</form>
    
    	<?php do_action('woocommerce_after_add_to_cart_form'); ?>
    
    <?php endif; ?>
  4. bcworkz
    Member
    Posted 10 months ago #

    This code does not appear to be within a standard WP "Loop". in_category() either needs the post ID as a parameter or it needs to be within the Loop where the global $post is available.

    I'm guessing $product is the current object for this code. If this object has an ID to which categories are attached, pass it as the second parameter and then your code will work correctly.

  5. brianhre
    Member
    Posted 10 months ago #

    Still not having much luck with this. This is now showing "Quantity 150" for all simple products. hmmm....

    <?php
    global $woocommerce, $product;
    
    if ( ! $product->is_purchasable() ) return;
    ?>
    
    <?php
    	// Availability
    	$availability = $product->get_availability();
    
    	if ($availability['availability']) :
    		echo apply_filters( 'woocommerce_stock_html', '<p class="stock '.$availability['class'].'">'.$availability['availability'].'</p>', $availability['availability'] );
        endif;
    ?>
    
    <?php if ( $product->is_in_stock() && is_shop_enabled() ) : ?>
    
    	<?php do_action('woocommerce_before_add_to_cart_form'); ?>
    
    	<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype='multipart/form-data'>
    
    	 	<?php do_action('woocommerce_before_add_to_cart_button'); ?>
    
    	 	<?php if ( ! $product->is_sold_individually() ){   ?>
              <?php  if ( in_category( 'Green', $product->id ) || in_category( 'Oolong', $product->id ) || in_category( 'White', $product->id )) {?>
                    <label><?php _e( 'Quantity 150 G', 'yit' ) ?></label>
              <?php }else {?>
                    <label><?php _e( 'Quantity', 'yit' ) ?></label>
              <?php }?>
    
    				<?php
    	 			woocommerce_quantity_input( array(
    	 				'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
    	 				'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
    	 			) );
    			}
    	 	?>
    
    	 	<button type="submit" class="single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'yit' ), $product->product_type); ?></button>
    
    	 	<?php do_action('woocommerce_after_add_to_cart_button'); ?>
    
    	</form>
    
    	<?php do_action('woocommerce_after_add_to_cart_form'); ?>
    
    <?php endif; ?>
  6. bcworkz
    Member
    Posted 10 months ago #

    My lack of knowledge of your commerce system is showing. $product must be a custom post type or something. in_category() only works for 'post' post types. For other objects, you need to use a more generic function. Try this:
    is_object_in_term( $product->id, 'category', array( 'Green', 'Oolong', 'White',))

    Note how you can place all terms (to match any of) in an array, avoiding a clumsy OR logic sequence. I hope this works as this is the best I can offer considering my lack of knowledge of your commerce system. If it still does not work, it is due to some small data mismatch or typo, as this function should work on any object (in theory).

Reply

You must log in to post.

About this Topic