1

I am using Magento 2 for my eCommerce website.I am trying to get option id from option text(i.e. D & G).Somehow I am getting option id for normal option text (i.e. Sample) but when it comes to special character,it doesn't work. Following is my code.

File : app/design/frontend/Theme/default/Magento_Catalog/templates/product/view/customattributes.phtml

<?php
  $_helper = $this->helper('Magento\Catalog\Helper\Output');
  $_product = $block->getProduct();
?>
<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($_product->getId());
    $categoriesIds = $_product->getCategoryIds();
    $cadeaubonnenId = '119';
?>
<?php if ($_additional = $block->getAdditionalData()): ?>
<h3><?= /* @escapeNotVerified */ __('More Information') ?></h3>
<div class="additional-attributes-wrapper table-wrapper">
    <table class="data table additional-attributes" id="product-attribute-specs-table">
        <caption class="table-caption"><?= /* @escapeNotVerified */ __('More Information') ?></caption>
        <tbody>

        <?php $alterCodeAtt = array('alternativebarcode1','alternativebarcode2','alternativebarcode3','alternativebarcode4'); ?>
        <?php foreach ($_additional as $_data): ?>
            <?php 
            $attr = $_product->getResource()->getAttribute($_data['code']);
             if ($attr->usesSource()) {
 $option_id = $attr->getSource()->getOptionId($_data['value']);// option id generates here
             } ?>
            <?php $attributes = $_helper->productAttribute($_product, $_data['value'], $_data['code']); 
            if(isset($attributes) && $attributes != 'Nee' && $attributes != 'N/A' && $attributes != 'n.v.t.' && !in_array($cadeaubonnenId,$categoriesIds) ):  ?>

                <?php if (in_array($_data['code'], $alterCodeAtt)):      
                   $alterCodevalArray[] = $_helper->productAttribute($_product, $_data['value'], $_data['code']); ?>
                <?php else: ?>

                    <?php if($_data['code'] != "price_elsewhere"): ?>
                        <?php if($_data['code'] == "manufacturer" || $_data['code'] == "lijn" || $_data['code'] == "submerk"): ?>
                              <tr>
                                <th class="col label" scope="row"><?= $block->escapeHtml(__($_product->getResource()->getAttribute($_data['code'])->getFrontendLabel())) ?>:</th>
                                <td class="col data" data-th="<?= $block->escapeHtml(__($_data['label'])) ?>">
                                    <a href="<?php echo $this->getUrl('catalogsearch/advanced/result').'?'.$_data['code'].'='.$option_id ?>"><?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></a></td>
                              </tr>
                            <?php else: ?>
                                <tr>
                                    <th class="col label" scope="row"><?= $block->escapeHtml(__($_product->getResource()->getAttribute($_data['code'])->getFrontendLabel())) ?>:</th>
                                    <td class="col data" data-th="<?= $block->escapeHtml(__($_data['label'])) ?>"><?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
                                </tr>
                        <?php endif; ?>
                    <?php endif; ?>

                <?php endif;?>
            <?php endif; ?>
        <?php endforeach;?>

            <tr>
                <th class="col label" scope="row"><?= $block->escapeHtml(__('Artikelnummer')) ?>:</th>
                <td class="col data" data-th="<?= $block->escapeHtml(__('Artikelnummer')) ?>"><?php echo $_product->getSku(); ?></td>
            </tr>
        <?php if (!empty($alterCodevalArray)): ?>
            <tr>
                <th class="col label" scope="row"><?= $block->escapeHtml(__('Alternatieve barcode')) ?>:</th>
                <td class="col data" data-th="<?= $block->escapeHtml(__('Alternatieve barcode')) ?>"><?php echo implode(" &nbsp;", $alterCodevalArray );  ?></td>
            </tr>
        <?php endif; ?>
        <?php if(!in_array($cadeaubonnenId,$categoriesIds)): ?>
            <tr>
                <th class="col label" scope="row"><?= $block->escapeHtml(__('Voorraad')) ?>:</th>
                <td class="col data" data-th="<?= $block->escapeHtml(__('Voorraad')) ?>"><?php echo $productStockObj->getQty(); ?></td>
            </tr>
        <?php endif; ?>
            <tr>
                <th class="col label" scope="row"><?= $block->escapeHtml(__('Status')) ?>:</th>
                <td class="col data" data-th="<?= $block->escapeHtml(__('Status')) ?>"><?php echo __(($productStockObj->getIsInStock() == 1) ? "In Stock" : "Out of Stock"); ?></td>
            </tr>
        </tbody>
    </table>
</div>
<?php endif;?>

Can someone please help me that what should I write in that option id line ?

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.