WooCommerce Conditional Logic

WooCommerce conditional tags can be used in your functions.php or custom plugin to run functions based on conditional logic. For example, you could display a message for products in a certain category or add content to a product with a specific ID.

Single Product Pages Only

add_action( 'woocommerce_before_main_content', 'bena_single_product_pages' );
function bena_single_product_pages() {
    if ( is_product() ) {
    echo 'Something';
    } else {
    echo 'Something else';
    }
}

Product has ID

add_action( 'woocommerce_after_single_product_summary', 'bena_single_product_ID' );
function bena_single_product_ID() {
    if ( is_single( '17' ) ) {
    echo 'Something';
    } elseif ( is_single( '56' ) ) {
    echo 'Something else';
    }
}

Product is in Category

add_action( 'woocommerce_after_single_product_summary', 'bena_single_category_slug' );
function bena_single_category_slug() {
    if ( has_term( 'chairs', 'product_cat' ) ) {
    echo 'Something';
    } elseif ( has_term( 'tables', 'product_cat' ) ) {
    echo 'Something else';
    }
}

//HERE IS AN EXAMPLE SHOWN BELOW

//If product is in  Studio Session Category -> Add Deposit Message
add_action( 'woocommerce_get_price_suffix', 'bena_add_studio_depo_msg' );
function bena_add_studio_depo_msg() {
	if ( has_term( 'studio-sessions', 'product_cat' ) ) {
	    $html = '<small class="priceSuffix">Deposit will be returned after session</small>';
    	return $html;
	}
}

Full Article by Rodolfo Melogli

[instagram-feed feed=1]
© 2024 BenaDigital Limited | All Rights Reserved
magnifiercrosschevron-down