Skip to content
Advertisement

WooCommerce check if product attribute has value

I’m trying to amend an existing Woo code snippet to change how prices are displayed. I need to display certain text if a certain attribute is present on a variable product.

I thought this might work >

$my_terms = get_the_terms( $product->id, 'pa_pricing');

    if (in_array('rent', $my_terms)) {$price_text = sprintf('%s: ', __('is rental', 'wcvp_range'));} else {$price_text = sprintf('%s: ', __('no rental here', 'wcvp_range'));};

Or maybe this

if ( has_term( 'Rent', 'pa_pricing' )) {$price_text = sprintf('%s: ', __('is rental', 'wcvp_range'));} else {$price_text = sprintf('%s: ', __('no rental here', 'wcvp_range'));};

Neither of the above work….

It’s to sit insde this code (in functions.php) and to eventually replace the text in $wcv_price if the word ‘Rent’ is not found as a price attribute. I’m obviously not a php genius…. 🙂

function wc_varb_price_range( $wcv_price, $product ) {
 
    $prefix = sprintf('%s: ', __('Rent from', 'wcvp_range'));
 
    $wcv_reg_min_price = $product->get_variation_regular_price( 'min', true );
    $wcv_reg_max_price = $product->get_variation_regular_price( 'max', true );
    $wcv_min_sale_price    = $product->get_variation_sale_price( 'min', true );
    $wcv_max_sale_price    = $product->get_variation_sale_price( 'max', true );
    $wcv_max_price = $product->get_variation_price( 'max', true );
    $wcv_min_price = $product->get_variation_price( 'min', true );

    $wcv_price = ( $wcv_min_sale_price == $wcv_reg_min_price ) ?
        wc_price( $wcv_reg_min_price ) :
        '<del>' . wc_price( $wcv_reg_min_price ) . '</del>' . '<ins>' . wc_price( $wcv_min_sale_price ) . '</ins>';
        
    $wcv_bo_price = ( $wcv_max_sale_price == $wcv_reg_max_price ) ?
        wc_price( $wcv_reg_max_price ) :
        '<del>' . wc_price( $wcv_reg_max_price ) . '</del>' . '<ins>' . wc_price( $wcv_max_sale_price ) . '</ins>';    
 
    return ( $wcv_min_price == $wcv_max_price ) ?
        $wcv_price :
        sprintf('%s%s', $prefix, $wcv_price)." per month <br> Buy for <span class='woocommerce-Price-amount amount'>".$wcv_bo_price."</span>";

}

add_filter( 'woocommerce_variable_sale_price_html', 'wc_varb_price_range', 10, 2 ); 
add_filter( 'woocommerce_variable_price_html', 'wc_varb_price_range', 10, 2 );

Advertisement

Answer

To get the value of a product attribute, you can use the get_attribute() method on the product class.

Example:

$pricing = $product->get_attribute( 'pricing' );

This method will return a string.

Where this gets slightly more complicated is when you potentially have multiple values for a given attribute. get_attribute() will return a comma delimited string of values. In that case, you’d need to convert it back into an array.

Example:

$pricing = explode( ', ', $product->get_attribute( 'pricing' ) );

Full version of check:

// If you know that 'pricing' will only ever be a single value.
if ( 'rent' == $product->get_attribute( 'pricing' ) ) {
  // . . .


// If you believe that the pricing attribute may have multiple values.
$pricing = explode( ', ', $product->get_attribute( 'pricing' ) );

if ( in_array( 'rent', $pricing ) ) {
    // . . .
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement