I have created an dropdown attribute programatically. Now i am trying to get an attribute value inside the class using object manager but unfortunately i am facing an error
function getServiceScheduleSlotss() is were i need the attribute value which is then called inside the function getAllOptions().
This is the Error i found in the error.log
"PHP Fatal error: Uncaught Error: Call to a member function getAttributeText() on null in /var/www/html/magento2sample/app/code/Today/Slot/Model/Config/Source/Options.php:20"
Is there anyway to get attribute value here without using Object manager , If not how to solve this issue?
Also i have used this same method to get attribute value to a phtml using block and worked fine
<?php
namespace Today\Slot\Model\Config\Source;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* Get all options
*
* @return array
*/
function getServiceScheduleSlotss() {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$slot_break = $product->getAttributeText('choose_slot_break');
$slot_interval = $product->getAttributeText('choose_slot_intervel');
$time_period_from = $product->getAttributeText('time_period_from');
$time_period_to = $product->getAttributeText('time_period_to');
$date_from = $product->getchooseDateFrom();
$date_to = $product->getchooseDateTo();
$slot_break = substr($slot_break,0,2);
$slot_interval = substr($slot_interval,0,2);
$date_from = substr($date_from,0,10);
$date_to = substr($date_to,0,10);
$start = new \DateTime($time_period_from);
$end = new \DateTime($time_period_to);
$interval = new \DateInterval("PT" . $slot_interval. "M");
$slot_breakInterval = new \DateInterval("PT" . $slot_break. "M");
for ($intStart = $start;
$intStart < $end;
$intStart->add($interval)->add($slot_breakInterval)) {
$endPeriod = clone $intStart;
$endPeriod->add($interval);
if ($endPeriod > $end) {
$endPeriod=$end;
}
$periods[] = $intStart->format('H:iA') .
' - ' .
$endPeriod->format('H:iA');
}
return $periods;
}
public function getAllOptions()
{
$periods = $this->getServiceScheduleSlotss();
$this->_options = [
['label'=>'', 'value'=>''],
['label'=>'10-11', 'value'=>'1'],
['label'=>'12-13', 'value'=>'2'],
['label'=>'15-16', 'value'=>'3']
];
return $this->_options;
}
/**
* Get a text for option value
*
* @param string|integer $value
* @return string|bool
*/
public function getOptionText($value)
{
foreach ($this->getAllOptions() as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
return false;
}
}