Friday 7 February 2020

Magento 2 get product custom option details in phtml file

protected $_customOption;

\Magento\Catalog\Model\Product\Option $customOption,

$this->_customOption = $customOption;

public function customOption($product)
{
$custom = $this->_customOption->getProductOptionCollection($product);

if(!empty($custom)):
foreach($custom as $option):
$values = $option->getValues();
if(!empty($values)):
foreach($values as $value):
$valueData[] = $value->getData();
endforeach;
return $valueData;
else:
return 'Product has no custom option value';
endif;
endforeach;
else:
return 'Product does not have custom options';
endif;
}

Thursday 6 February 2020

How to remove product custom option '+' symbol in magento 2

/app/design/frontend/venderName/softtouch/Magento_Catalog/templates/product/view/options/type/select.phtml

<script>
  require([
   'jquery',
   'domReady!'
   ], function ($) {
       $(document).ready(function () {
           $('select.product-custom-option').change(function(){
               $('option').each(function(){
                   var selectedOption = $(this).text();
                 
                   if (selectedOption.indexOf('+') > -1) {
                        var replaced = selectedOption.replace('+', ' ');
                       // selectedOption = selectedOption.substring(0, selectedOption.indexOf('+'));
                        $(this).text(replaced);
                   } else if (selectedOption.indexOf('-') > -1) {
                        //selectedOption = selectedOption.substring(0, selectedOption.indexOf('-'));
                        var replaced = selectedOption.replace('+', ' ');
                        $(this).text(replaced);
                   }
               });   
           });   
       });
    });
</script>