I
I
Igor Frolov2018-09-04 14:59:55
opencart
Igor Frolov, 2018-09-04 14:59:55

Set default selection in Opencart product options?

Given:
Opencart 2.3
Premium theme from themeforet (support expired)
File Product.tpl
Task:
There is a choice of options in the product card, it is necessary that when opening the product the default option is ALREADY
selected Solution:
I tried to solve the issue by inserting: As a result, on the page goods output is:
<?php if ($key == 2) echo ('checked');?>


{**} Option 1
{**} Option 2
{*checked*} Option 3

Here is the part of the Product.tpl file where the code was inserted:
<!--radio or new option Color-->
        <?php if ($option['type'] == 'radio' || $option['type'] == 'radio-color') { $num ++; ?>
        <div class="wrapper form-group7 display-block w-auto">
            <label class="title-options" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?><?php if ($option['required']) : ?><span class="color-required">*</span><?php endif; ?></label>

          <?php
          foreach ($option['product_option_value'] as $option_value) {
                if ($option_value['image']) { $radio_class = 'options-swatch-texture'; } else {$radio_class = 'radio-list';}
           }
          ?>


            <ul class="options-swatch <?php echo $radio_class; ?>" id="input-option<?php echo $option['product_option_id']; ?>">
                <?php foreach ($option['product_option_value'] as $key => $option_value) { ?>
                <li>
                    <?php if ($option_value['image']) { ?>
                    <a>
                        <label>
                            <input class="display-n" type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" />
                            <img class="icon-color icon-color<?php echo $num; ?>" src="<?php echo $option_value['image']; ?>" title="<?php echo $option_value['name']; ?>
                        <?php if ($option_value['price']) { ?>(<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)<?php } ?>" alt="<?php echo $option_value['name'] . ($option_value['price'] ? ' ' . $option_value['price_prefix'] . $option_value['price'] : ''); ?>" />
                        </label>
                    </a>
                    <?php } else { ?>
                    <label class="radio">
                        {*<?php if ($key == 2) echo ('checked');?>*}
                        <input type="radio" <?php if ($key == 2) echo ('checked');?> name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>"  /> <!--echo $option_value['product_option_value_id']; -->
                        <span class="outer"><span class="inner"></span></span><?php echo $option_value['name']; ?>


                    </label>
                    <?php } ?>
                </li>
                <?php } ?>
            </ul>
        </div>

        <script type="text/javascript">
            var iconcolor<?php echo $num; ?> = $(".icon-color<?php echo $num; ?>:not(.disable)");
            iconcolor<?php echo $num; ?>.click(function () {
                if ($(this).hasClass("selected")) iconcolor<?php echo $num; ?>.each(function () {
                    $(this).removeClass("selected")
                });
                else {
                    iconcolor<?php echo $num; ?>.each(function () {
                        $(this).removeClass("selected")
                    });
                    $(this).addClass("selected")
                }
            });
        </script>
        <?php } ?>
        <!--//radio or new option Color-->

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question