• function skyverge_shop_display_skus() {	
    
    	global $product;
    
    	if ( $product->get_sku() ) {
    		echo '<div class="product-meta">SKU: ' . $product->get_sku() . '</div>';
    	} elseif ( $product->get_available_variations() ) {
        $available_variations = $product->get_available_variations();
      	echo '<div class="product-meta-sku">';
    		for ( $i = 0; $i < count($available_variations); $i++ ) {
    			if ( $available_variations[$i]['sku'] ) {
    		    echo ($available_variations[$i]['sku']);
    		    if ( $i < (count($available_variations) - 1) ) {
    		      echo ', ';
    		    }
    			}
    		}
       	echo '</div>';
    	}
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'skyverge_shop_display_skus', 9 );  

    I have this function, but i need exclude this function on same category pages, because on some pages giving me error.

    Is it possible?

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hey @ondrejkrejc,

    Can you clarify what you mean by ‘same category’ pages?

    If you mean on specific category archives, you can use the is_category() function for that purpose.

    For example, if you wanted to prevent the function from running on the category with the ID 142 or with the slug cats, you would add this at the beginning of the function:

    if ( is_category( [ 42, 'cats' ] ) ) {
    	return;
    }

    However, you could also try this. It should prevent an error from occurring by checking if the $product variable exists before attempting to use it:

    add_action( 'woocommerce_after_shop_loop_item_title', function () {
    	global $product;
    	
    	if ( ! isset( $product ) ) {
    		return;
    	}
    
    	if ( $product->get_sku() ) {
    		echo '<div class="product-meta">SKU: ' . $product->get_sku() . '</div>';
    	} elseif ( $product->get_available_variations() ) {
    		$available_variations = $product->get_available_variations();
    		echo '<div class="product-meta-sku">';
    		for ( $i = 0; $i < count($available_variations); $i++ ) {
    			if ( $available_variations[$i]['sku'] ) {
    				echo ($available_variations[$i]['sku']);
    				if ( $i < (count($available_variations) - 1) ) {
    					echo ', ';
    				}
    			}
    		}
    		echo '</div>';
    	}
    }, 9 );
Viewing 1 replies (of 1 total)
  • The topic ‘Exclude php function’ is closed to new replies.