I want to pass custom option values from product detail page to cart page in magento2.

Refer screenshot

enter image description here

I have selected here size: 5 and want to pass the value to cart page.

Here is my code:

    if (!$cartItem):
         foreach ($currentProduct->getOptions() as $option) {

          //echo $option['label'].'=>'. $option['value'].

          if ($option->getTitle() == 'Ring Size') {
            $values = $option->getValues();
            $name = "options[" . $option->getId() . "]";
            <div class="drop-down5">    
                <select name="options" id ="ringSize">  
                    <option>SELECT SIZE</option>
                     <option value="5">SIZE: 5</option>
                     <option value="5.5">SIZE: 5.5</option>
                     <option value="6">SIZE: 6</option>  
                     <option value="6.5">SIZE: 6.5</option> 
                     <option value="7">SIZE: 7</option> 
                     <option value="7.5">SIZE: 7.5</option> 
                     <option value="8">SIZE: 8</option>   
                     <option value="8.5">SIZE: 8.5</option>  
                     <option value="9">SIZE: 9</option>  

                    <?php echo "<input type= 'hidden' name = " . $name . " id = 'ringSizeText' > "; ?>


In checkout cart page default.phtml

<?php if ($_options = $block->getOptionList()): 
                    <?php foreach ($_options as $_option) : ?>
                            <?php if($helper_sdi->isInEngravingCustomOptions($_option['label'])) continue; ?>
                            <?php $_formatedOptionValue = $this->getFormatedOptionValue($_option);   ?>
                    <?php endforeach; ?>
                    <?php endif; ?>

                    <!--Foreach statement code start-->
                      <?php if(!empty($_formatedOptionValue)) { 

                    <section class="grid-3-3  no-padding-right ring-size"  style="text-align: center;">
                        <?php if ($_options = $block->getOptionList()):  ?>
                        <dl class="item-options">
                            <?php foreach ($_options as $_option) :  ?>
                                <?php if($helper_sdi->isInEngravingCustomOptions($_option['label'])) continue; ?>
                                <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option);  
                                <dt><?php echo $block->escapeHtml($_option['label']) ?></dt>
                                <dd style="margin: 20px 0 0px 0;" 
                                <?php if (isset($_formatedOptionValue['full_view'])): ?> class="truncated"<?php endif; ?> >
                                <?php echo $_formatedOptionValue['value'] ?>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    <?php } ?>

please someone help where I am making mistake.

Any help would be appreciated.

  • are you getting any error or warnings? – Chintan Kaneriya Jun 1 '19 at 12:32
  • No, I am not getting any errors – Amy Jun 1 '19 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.