mnybud Posted December 4, 2016 Share Posted December 4, 2016 Hi. I need some help making some changes to some code. I have tried several things and still can't seem to get it right so hoping someone here can help me out. This is the original code I wrote that I am trying to change.... <?php do_action( 'woocommerce_before_shop_loop_item' ); ?> <a href="<?php the_permalink(); ?>"> <?php /** * woocommerce_before_shop_loop_item_title hook * * @hooked woocommerce_show_product_loop_sale_flash - 10 * @hooked woocommerce_template_loop_product_thumbnail - 10 */ do_action( 'woocommerce_before_shop_loop_item_title' ); ?> <?php echo $product->is_in_stock() ? '' : '<img src="notavailable.png" class="attachment-shop_catalog wp-post-image">'; ?> <h3><?php the_title(); ?></h3></a> <?php do_action( 'woocommerce_after_shop_loop_item' ); ?> What I am trying to accomplish is to make it so my shop only shows the product if it is in stock. Currently the code above adds a notavailable.png to out of stock items but still displays them. I tried something like this but I am not able to make it work. <?php echo $product->is_in_stock() ? ' <?php do_action( 'woocommerce_before_shop_loop_item' ); ?> <a href="<?php the_permalink(); ?>"> <?php /** * woocommerce_before_shop_loop_item_title hook * * @hooked woocommerce_show_product_loop_sale_flash - 10 * @hooked woocommerce_template_loop_product_thumbnail - 10 */ do_action( 'woocommerce_before_shop_loop_item_title' ); ?> <h3><?php the_title(); ?></h3></a>' : ''; ?> <?php do_action( 'woocommerce_after_shop_loop_item' ); ?> If anyone can help me get this right I would really appreciate it. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 4, 2016 Share Posted December 4, 2016 Don't just randomly add code somewhere in your application. All that will do is break your shop. First off, have you checked if WooCommerce can skip products that are out of stock? Have you looked for plug-ins? This is a standard problem, so I'd be surprised if you were the first person ever to have it. Secondly, if you do have to implement this yourself, a) create a module/plug-in, don't modify the core application, b) filter the database query, not the HTML stuff. Quote Link to comment Share on other sites More sharing options...
mnybud Posted December 4, 2016 Author Share Posted December 4, 2016 Good advice but I am not very good at PHP (obviously) so did what I could figure out and modified it in a way that works for me. It has been this way for over a year with no problems There is not a good way to filter out products in woocommerce for my use as I am actually filtering out the $0 products and not actual out of stock items. Thanks again for the advice but for now I am just looking for a way to modify the code above. Quote Link to comment Share on other sites More sharing options...
bsmither Posted December 11, 2016 Share Posted December 11, 2016 Your thought about using $product->is_in_stock() is heading toward the solution you will accept. The return from that call is either tru-ish or false-ish. So, let's try: <?php if($product->is_in_stock()) { ?> yada yada yada <?php } else { /* The product was not in stock. */ } ?> I added an else after the true block of code to give a hint what happened when the if expression fails. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.