0

I am working with Magento 2 to get attributes values. Now here I added on an attribute which name Swatch Image. in there I selected 2 options, one I selected hex code (#cdcdcd) and other options I selected Image. Now I know how to get both values using option_id. which are mentioned below. for hex code

$opt_id = 26;
                        $swatchHelper=$objectManager->get("Magento\Swatches\Helper\Data");
                        $attributeOptionIds = array($opt_id);
                        $swatches = $swatchHelper->getSwatchesByOptionsId($attributeOptionIds);
                        echo "<pre>";
                            print_r($swatches);
                        echo "</pre>";
                        if(sizeof($swatches) > 0 ){
                            echo "<pre>";
                            print_r($swatches[$opt_id]['value']);
                            echo "</pre>";
                        }

and for Image

$opt_id1 = 27;
    $swatchHelper_image=$objectManager->get("Magento\Swatches\Helper\Media");
    $swatchCollection = $objectManager->create('Magento\Swatches\Model\ResourceModel\Swatch\Collection');

    $swatch1 = $swatchCollection->addFieldtoFilter('option_id',$opt_id1);
    $item=$swatch1->getFirstItem();
    $ThumbImage1 =  $swatchHelper_image->getSwatchAttributeImage('swatch_thumb', $item->getValue());
    echo "<br>";
    print_r($ThumbImage);

here I added code for both which I know already option 26 is hex and 27 is image. but I want to differentialte it. I mean how can I recognize which is hex and which is image.

Please help me.

0

We need to write the below code for differentiating both values!

        $opt_id = 23;
        $swatchHelper_hex = $objectManager->get("Magento\Swatches\Helper\Data");
        $attributeOptionIds = array($opt_id);
        $swatch2 = $swatchHelper_hex->getSwatchesByOptionsId($attributeOptionIds);
        if(!empty($swatch2)){
            if($swatch2[$opt_id]['type']==2){
                //image
                $opt_val = $swatch2[$opt_id]['value'];
            }else if($swatch2[$opt_id]['type']==1){
                //hexcode
                $opt_val = $swatch2[$opt_id]['value'];
            }
            echo $opt_val;
            echo "<br>";
        }
        echo "<br>";

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.