I am trying to get the color attribute based on the size attribute.

public function getSize($product)
    $_attributes = $this->getAllowAttributes();
    $result = array();
    foreach ($_attributes as $_attribute){
         $result[] = $_attribute->getProductAttribute()->getAttributeCode();
    $size = $product->getResource()->getAttribute($result[0])->getFrontend()->getValue($product);
    if ($size == "No" || is_null($size)) {
        $size = '';

    return $size;

This will display size attribute of product. But I want to display its related color like this: Size-1 / Color-1 Size-1 / Color-2 Size-1 / Color-3 Size-1 / Color-4 Size-2 / Color-1 Size-2 / Color-2 Size-2 / Color-3 Size-2 / Color-4 Size-3 / Color-1 Size-3 / Color-2 Size-3 / Color-3 Size-3 / Color-4

My view page looks like:

    <td class="option"><span class="product-option"><?= $block->escapeHtml($block->getSize($product)); ?></span></td>
    <?php //foreach ($_attributes as $_attribute): ?>
    <?php if ($product->isAvailable()): ?>
        <td class="status"><?= /* @escapeNotVerified */ __('In stock') ?></td>
    <?php else: ?>
        <td class="status"><?= /* @escapeNotVerified */ __('Out of stock') ?><a href="#" class="restock-nortify" title="商品入荷のお知らせを受け取る">再入荷通知</a></td>
    <?php endif; ?>

Is there any method to get like this?

Thanks in advance.

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.