Jump to content

Need Help Modifing This PHP Code


mnybud

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.