1

In the default.phtml for the checkout cart this is used to get all options:

<?php if ($_options = $block->getOptionList()):?>
    <dl class="item-options">
        <?php foreach ($_options as $_option) : ?>
           <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
        <dt><?php echo $block->escapeHtml($_option['label']) ?></dt>
        <dd>
            <?php if (isset($_formatedOptionValue['full_view'])): ?>
                <?php /* @escapeNotVerified */ echo $_formatedOptionValue['full_view'] ?>
            <?php else: ?>
                <?php /* @escapeNotVerified */ echo $_formatedOptionValue['value'] ?>
            <?php endif; ?>
        </dd>
        <?php endforeach; ?>
    </dl>
<?php endif;?>

But how do I get one specific option with value and label by its code?

I added that option in the backend. There it is called attribute suddenly for some reason. And there are also some functions to get attributes but neither of them returns anything other than null if I try to use it on the $product in default.phtml. I also tried to fetch it in the same matter it is fetched in ->getOptionList() but that also returns nothing else than null. Here are some things I tried in the head of default.phtml.

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Checkout\Block\Cart\Item\Renderer */

$_item = $block->getItem();
$product = $_item->getProduct();

$optionIds = $_item->getOptionByCode('option_ids'); // null
$addOptions = $_item->getOptionByCode('additional_options'); // null

$option = $product->getOptionById(140); // null
$customOption = $product->getCustomOption('mycode'); // null
$customAttribute = $product->getCustomAttribute('mycode'); // null
$customOptions = $product->getCustomOptions(); // does not contain it
$options = $product->getOptions(); // null
$data = $product->getData(); // doesn't contain it either

Trying to add more data hoping this might help someone spot where it is breaking...

Here is how the attribute is setup:

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'mycode',
    [
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'My Code',
        'input' => 'select',
        'class' => '',
        'source' => '',
        'global' => 1,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => null,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => 'configurable',
        'system' => 1,
        'group' => 'General',
        'option' => array('values' => array("x","y"))
    ]
);

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.