• Resolved markatimpactmerch

    (@markatimpactmerch)


    Odd issue here.

    So my site displays # of quantities for each product. I’ve written a line of code for specific categories to show each product quantity as “25” at all times rather than “0”. This works fine and dandy if you’re in the category’s page, however, when you search for the products on the “Search Results” page these quantities show at “0”.

    Does anyone know what the issue is? I can’t seem to figure it out.

    Thank you!

    https://ibb.co/drGWRDw (Category view)
    https://ibb.co/B2GD5zN (Search Results view)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support kellymetal a11n

    (@kellymetal)

    Hi there,

    Thank you for including those screenshots of your Category and Search results pages!

    Those seem quite customized from the default display/functionality (i.e listing all variations with quantity fields so can add to Cart directly), so it may be a bit tricky to figure out the difference here.

    Would you mind providing the code you used to add the “25” to display on the Category pages? Please use the Code tags in the reply box here so everything is formatted OK.

    Also, a URL to the site would be helpful.

    Thank you!

    Thread Starter markatimpactmerch

    (@markatimpactmerch)

    Absolutely thanks for the reply. The code is on the content-product.php file and the full code is below:

    <?php
    /**
     * The template for displaying product content within loops
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see     https://docs.woocommerce.com/document/template-structure/
     * @package WooCommerce/Templates
     * @version 3.4.0
     */
    
    defined( 'ABSPATH' ) || exit;
    
    global $product;
    
    // Ensure visibility.
    if ( empty( $product ) || ! $product->is_visible() ) {
    	return;
    }
    ?>
    <li <?php wc_product_class(); ?>>
    	<?php
    	/**
    	 * Hook: woocommerce_before_shop_loop_item_title.
    	 *
    	 * @hooked woocommerce_show_product_loop_sale_flash - 10
    	 * @hooked woocommerce_template_loop_product_thumbnail - 10
    	 */
    ?>
    	<a href="<?php echo the_permalink(); ?>">
    		<?php
    			do_action( 'woocommerce_before_shop_loop_item_title' );
    		?>
    	</a>
    <?php
    	/**
    	 * Hook: woocommerce_shop_loop_item_title.
    	 *
    	 * @hooked woocommerce_template_loop_product_title - 10
    	 */
    	do_action( 'woocommerce_shop_loop_item_title' );
    
    	/**
    	 * Hook: woocommerce_after_shop_loop_item_title.
    	 *
    	 * @hooked woocommerce_template_loop_rating - 5
    	 * @hooked woocommerce_template_loop_price - 10
    	 */
    	do_action( 'woocommerce_after_shop_loop_item_title' );
    
    	global $product, $post;
    ?>
    	<div class="sku-number">
    		<?php echo $product->get_sku(); ?>
    	</div>
    <?php
    	$variations = $product->get_available_variations();
    
    	foreach ($variations as $key => $value)
    	{
    		
    		/*if(isset($value['max_qty']) && is_numeric($value['max_qty'])){
                $quantity = ($value['max_qty'] < 100)? $value['max_qty'] : '99+';
            } else {
    	        $quantity = 0;
            }*/
    		$variation_id = $value['variation_id'];
            $variation_obj = new WC_Product_variation($variation_id);
            $stock = $variation_obj->get_stock_quantity();
    ?>
    		<div class="product-variation-row">
    			<div>
    				<?php echo implode('/', $value['attributes']);?>
    				<?php echo $value['price_html'] ?: $product->get_price_html();?>
    			</div>
    			<div>
    				<span>Qty:</span>
    				<input data-id="<?php echo $value['variation_id']?>" type="number" class="variation-number" value="0" min="0" />
    				<!-- QTY Code Extra Start -->
    				<span>&nbsp; <?php if (is_product_category() && has_term('DTG', 'product_cat')): echo '25'; elseif ($stock > 0): echo $stock; else: echo '0'; endif; ?></span>
    				<!-- QTY Code Extra End -->
    			</div>
    		</div>
    <?php	
    	}
    ?>
    	<button data-url="<?php echo get_bloginfo('url'); ?>" class="single_add_to_cart_button button alt product-bulk" type="submit">Add to Cart</button>
    </li>
    
    Plugin Support kellymetal a11n

    (@kellymetal)

    Hi there,

    Thank you for providing that.

    So, I see that you have added some content to the content-product.php template file.

    For the logic on the quantity part, you have is_product_category() && has_term('DTG', 'product_cat') — however, the is_product_category() part will only return true when viewing a product category page, as mentioned in the docs here:
    https://docs.woocommerce.com/document/conditional-tags/#section-5

    If you try removing that, does it work?

    Thread Starter markatimpactmerch

    (@markatimpactmerch)

    Wow! Didn’t even cross me that was the issue. Totally worked!

    Seriously, thank you so much for finding that.

    Have a wonderful day!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Woocommerce Search Results’ is closed to new replies.