Answer the question
In order to leave comments, you need to log in
How to display the value of a specific Opencart product option on the category page?
Hello! I ran into a problem in Opencart.
It is necessary to display the value of the product size option
in a small product card (the one that is displayed in categories, search, etc.) .
Some products have multiple sizes,
others only one. Others do not have this option.
In the case when the product has only one size option - we display the size, in the case when there are several - "Sizes to choose from", when there is no size - we do not display anything.
Through <?php var_dump($product['options']) ?>
I dumped all the elements of the array with options.
array(2) { [0]=> array(7) { ["product_option_id"]=> string(4) "1504" ["product_option_value"]=> array(7) { [0]=> array(6) { ["product_option_value_id"]=> string(5) "17905" ["option_value_id"]=> string(3) "609" ["name"]=> string(10) "White" ["image"]=> NULL ["price"]=> bool(false) ["price_prefix"]=> string(1) "+" } [1]=> array(6) { ["product_option_value_id"]=> string(5) "17906 " ["option_value_id"]=> string(3) "598" ["name"]=> string(14) "Beige" ["image"]=> NULL ["price"]=> bool(false) [" price_prefix"]=> string(1) "+" } [2]=> array(6) { ["product_option_value_id"]=> string(5) "17907" ["option_value_id"]=> string(3) "604" ["name"]=> string(12) "Laguna" ["image"]=> NULL [" price"]=> bool(false) ["price_prefix"]=> string(1) "+" } [3]=> array(6) { ["product_option_value_id"]=> string(5) "17621" [" option_value_id"]=> string(3) "601" ["name"]=> string(20) "Purple" ["image"]=> NULL ["price"]=> bool(false) ["price_prefix"] => string(1) "+" } [4]=> array(6) { ["product_option_value_id"]=> string(5) "17909" ["option_value_id"]=> string(3) "701" [" name"]=> string(14) "Pink" ["image"]=> NULL ["price"]=> bool(false) ["price_prefix"]=> string(1) "+" } [5]=> array(6) { ["product_option_value_id"]=> string (5) "17910" ["option_value_id"]=> string(3) "700" ["name"]=> string(12) "Peach" ["image"]=> NULL ["price"]=> bool (false) ["price_prefix"]=> string(1) "+" } [6]=> array(6) { ["product_option_value_id"]=> string(5) "17911" ["option_value_id"]=> string (3) "702" ["name"]=> string(35) "Intense pink" ["image"]=> string(90) "product_option_value_id"]=> string(5) "17910" ["option_value_id"]=> string(3) "700" ["name"]=> string(12) "Peach" ["image"]=> NULL [" price"]=> bool(false) ["price_prefix"]=> string(1) "+" } [6]=> array(6) { ["product_option_value_id"]=> string(5) "17911" [" option_value_id"]=> string(3) "702" ["name"]=> string(35) "Intense pink" ["image"]=> string(90) "product_option_value_id"]=> string(5) "17910" ["option_value_id"]=> string(3) "700" ["name"]=> string(12) "Peach" ["image"]=> NULL [" price"]=> bool(false) ["price_prefix"]=> string(1) "+" } [6]=> array(6) { ["product_option_value_id"]=> string(5) "17911" [" option_value_id"]=> string(3) "702" ["name"]=> string(35) "Intense pink" ["image"]=> string(90) "} [6]=> array(6) { ["product_option_value_id"]=> string(5) "17911" ["option_value_id"]=> string(3) "702" ["name"]=> string(35) "Intense pink" ["image"]=> string(90) "} [6]=> array(6) { ["product_option_value_id"]=> string(5) "17911" ["option_value_id"]=> string(3) "702" ["name"]=> string(35) "Intense pink" ["image"]=> string(90) "inf-mebel.nichost.ru/image/cache/catalog/options/c..." ["price"]=> bool(false) ["price_prefix"]=> string(1) "+" } } ["option_id"]=> string(2) "20" ["name"]=> string (21) "Upholstery color" ["type"]=> string(5) "image" ["value"]=> string(0) "" ["required"]=> string(1) "1" } [ 1]=> array(7) { ["product_option_id"]=> string(4) "1512" ["product_option_value"]=> array(3) { [0]=> array(6) { ["product_option_value_id"] => string(5) "17786" ["option_value_id"]=> string(3) "680" ["name"]=> string(39) ""M 90/190" - 1045x2060x1045" ["image"]= > NULL ["price"]=> bool(false) ["price_prefix"]=> string(1) "+" } [1]=> array(6) { ["product_option_value_id"]=> string(5) "17787" ["option_value_id"]=> string(3) "681" ["name"]=> string( 39) ""M 90/200" - 1045x2160x1045" ["image"]=> NULL ["price"]=> string(11) "3185.00 RUB" ["price_prefix"]=> string(1) "+" } [2]=> array(6) { ["product_option_value_id"]=> string(5) "17797" ["option_value_id"]=> string(3) "682" ["name"]=> string(36) ""M 120" - 1045x2160x1345" ["image"]=> NULL ["price"]=> string(12) "$15720.00" ["price_prefix"]=> string(1) "+" } } [" option_id"]=> string(2) "17" ["name"]=>string(12) "Size" ["type"]=> string(5) "radio" ["value"]=> string(0) "" ["required"]=> string(1) "1" } }
How can I access exactly that part of the array that is responsible for the size, in order to make a selection by the number of these sizes through if else and form the result for each case, as well as get the value of the size itself?
Thank you!
Answer the question
In order to leave comments, you need to log in
$size_options = array(17, ...); // id опций которые отвечают за размер, в примере это 17
foreach ($product['options'] as $option) {
if (in_array( $option['option_id'], $size_options)) { // работаем только с теми опциями, которые отвечают за размер.
... // тут проверяем нудные вам условия
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question