If else syntax issue

if ($product->is_type( 'variable' )) {
	if( $variation_product ->sale_price !=0 ) {
			echo $variation_product ->sale_price;
	}else {
		echo $variation_product ->regular_price;
	}

	}
}else {
	echo wc_price($product->get_price());
}

This is generating error →

else {
	echo wc_price($product->get_price());
}

**Parse error** : syntax error, unexpected 'else' (T_ELSE), expecting end of file in **/home3/**

I think we are still in this condition:

if ( *condition* ) {
*code to be executed if condition is true;*
} else {
 <i>code to be executed if condition is false;</i>
}

Count how many { and how many } you have :slight_smile: . It’s that simple. Figure out where each if / else end, and find the issue :slight_smile: .

Just count your curly brackets. The counts must be equal.

I prefer this formatting structure:

if ($product->is_type( 'variable' ))
{
  if( $variation_product ->sale_price !=0 )
  {
	echo $variation_product ->sale_price;
  }else {
	echo $variation_product ->regular_price;
  } // end if/else
// } NOT REQUIRED 
}else {
  echo wc_price($product->get_price());
}

This “alternative syntax” is even better:

if ($product->is_type( 'variable' )) :
  if( $variation_product ->sale_price !=0 ) :
    echo $variation_product ->sale_price;

  else :
    echo $variation_product ->regular_price;
  endif; // } }

else :
  echo wc_price($product->get_price());
endif; //}

The additional effort involved saves hours of frustratng debugging :slight_smile:

1 Like

Full code →

function wooprice(){
	global $product;
	// 1 Get product varations
	$product_variations = $product->get_available_variations();

	// 2 Get one variation id of a product
	$variation_product_id = $product_variations [0]['variation_id'];

	// 3 Create the product object
	$variation_product = new WC_Product_Variation( $variation_product_id );

	if ($product_variations !=0) {
		if( $variation_product ->sale_price !=0 ) {
				echo $variation_product ->sale_price;
		}else {
			echo $variation_product ->regular_price;
		}
	}else {
		 wc_price($product->get_price());
	}

}

Last else is still failing.

Fantastic syntax.

1 Like

Why not use it?

Revised function with fred(…)* the Debugger: (not tested)

<?php
DECLARE(STRICT_TYPES=1);
error_reporting(-1);
ini_set('display_errors', '1');

echo wooprice($product);

//==================================================================
function wooprice($product)
{
  $result =  NULL ;

  // global $product;

  // 1 Get product varations
  $product_variations = $product->get_available_variations();

  fred( $product_variations, '$product_variations');
  die; // halt execution

  // 2 Get one variation id of a product
  # $variation_product_id = $product_variations [0]['variation_id'];

  // 3 Create the product object
  # $variation_product = new WC_Product_Variation( $variation_product_id );

  if ($product_variations !=0) : 
    $variation_product_id = $product_variations [0]['variation_id'];
    $variation_product    = new WC_Product_Variation( $variation_product_id );

    if( $variation_product ->sale_price !=0 ) : 
        $result = $variation_product ->sale_price;
    else : 
      $result = $variation_product ->regular_price;
    endif; 
  else :
     wc_price($product->get_price());
  endif;

  return $result;
}

//=======================================================================
//  DEBUG FUNCTION:
//=======================================================================
function fred($val='No $val???', $title='')
{
  echo '<br><hr><pre style="background-color: yellow: color: red;">' ,$title;
    // var_dump($val);
    print_r($val); 
  echo '</pre><hr><br>';
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.