I want to get a plain value from custom variable (defined on admin) in php with objectManager.
storeID is in $order->getStore()->getId()
Custom Variable Code is 'mycustomvar'
why does following code not work?
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$variables = $objectManager->create('Magento\Variable\Model\Variable');
if ($var = $variables->getResource()->getVariableByCode('mycustomvar', true, $order->getStore()->getId())) {
$value = 'defined';
// $value = $variables->getData('plain_value');
} else {
$value = 'undefined';
}
It looks like $var
contains the array with the values of mycustomvar, but I'm not sure.
How can I pass $var
to getData('plain_value')
? Or is there a better way to get 'plain_value'
?
Classes I was looking into are
Magento\Variable\Model\Variable
Magento\Variable\Model\ResourceModel\Variable
PS I understood objectManager is not the preferred way, but I need to get this working here
PPS Magento 2.1
Best Answer
Class
Magento\Variable\Model\Variable
offers everything - such as setStoreId(StoreId), which needs to be set case by case for my application. There is no need to dig intoMagento\Variable\Model\ResourceModel\Variable
.The following code is working now. Replace
myStoreID
andmycustomvar
with your needs. Remove code line with setStoreId(StoreId), if you need access tomycustomvar
in the current store.Thanks to Vikas comments, it helped me find the solution.