0

Magento 2 Model file Const get in Helper.

I want to this type :-

File Path :- GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source

<?php

namespace GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source;

use Magento\Framework\Option\ArrayInterface;

class Landingpageproducts extends \Magento\Eav\Model\Entity\Attribute\Source\Boolean implements ArrayInterface
{

    const VALUE_NO = 'lpage_no';
    const VALUE_NEW = 'lpage_new';
    const VALUE_FEATURED = 'lpage_featured';
    const VALUE_SALE = 'lpage_sale';

    protected $_options;

    public function getAllOptions()
    {
        return [
            ['value' => self::VALUE_NO, 'label' => __('No')],
            ['value' => self::VALUE_NEW, 'label' => __('New Products')],
            ['value' => self::VALUE_FEATURED, 'label' => __('Featured Products')],
            ['value' => self::VALUE_SALE, 'label' => __('Sale Products')]
        ];
    }
?>

Helper code :-

<?php

namespace GetSomeMojo\CategoryLandingPage\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use Magento\Cms\Api\BlockRepositoryInterface;

class Data extends AbstractHelper
{
    
    const VALUE_NO = \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source:: VALUE_NO;
    const VALUE_NEW = \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source:: VALUE_NEW;
    const VALUE_FEATURED = \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source:: VALUE_FEATURED;
    const VALUE_SALE =\GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source:: VALUE_SALE;
}
?>

Above const value get in Helper file.

So Please Help me how to get const in helper.

THANKS.

2 Answers 2

0

To use these constants variables outside of the class, you can use the full path of the class like below: \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source:: VALUE_NO

It starts to be frown upon to use these constants as it give less encapsulation to your code. But it has its pros indeed. Good luck

2
  • Hello @Herve Tribouilloy i have already try but not success. Please provide me other solution . Oct 10, 2020 at 5:59
  • I have add mu helper code in my question. Please check. Oct 10, 2020 at 6:04
0

Please try to update below lines in your helper file

const VALUE_NO = \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source\Landingpageproducts::VALUE_NO;
const VALUE_NEW = \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source\Landingpageproducts::VALUE_NEW;
const VALUE_FEATURED = \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source\Landingpageproducts::VALUE_FEATURED;
const VALUE_SALE =\GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source\Landingpageproducts::VALUE_SALE;

You missed to add class name there Landingpageproducts. Update that and it should work.

Hope this will help you!

1
  • Let me know if you resolved this issue, or if you need any help then, thanks. ;-) Oct 12, 2020 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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