I'm using the following code to retrieve and display my product attribute value on product page:
$discounts = get_post_meta( $product->id, '_my_discounts' );
_my_discount
is an array containing different discounts. Each discount contains different information (min_qty
, discount
, type
, is_flat
etc). When I use echo var_dump($discounts);
I get the following output (I have two discounts):
array(1) { [0]=> array(2) {
["58038e0802c2252b7f50a70d25a33ede"]=> array(7) { ["min_qty"]=> int(4) ["discount"]=> string(3) "100" ["type"]=> int(2) ["role"]=> string(3) "any" ["position"]=> string(1) "0" ["is_flat"]=> int(1) ["is_variations_sep"]=> int(0)
["007632f30006ccaac16982b779ec57ae"]=> array(7) { ["min_qty"]=> int(8) ["discount"]=> string(3) "293" ["type"]=> int(2) ["role"]=> string(3) "any" ["position"]=> string(1) "0" ["is_flat"]=> int(1) ["is_variations_sep"]=> int(0)
My question is, how do I retrieve the highest discount out of the array in ["discount"]
and display it on product page? For example the above product has two discounts $100 & $293. I want to retrieve the highest of the two and display it on my product page.
I tried the following, but it didn't work:
foreach ( $discounts as $highest_discount ) :
$discount_value = wc_get_product_terms( $product->id, $highest_discount['discount'], array( 'fields' => 'names' ) );
echo $discount_value;
endforeach;
Any help would be appreciated.