系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中使用自定义变量的步骤:
步骤1:首先,您需要从Magento管理面板添加自定义变量值
在“管理”边栏上,转到“系统>其他设置”>“自定义变量”。
单击添加新变量并填写详细信息。
步骤2:现在您需要在我们的代码中访问或获取自定义变量值,因此我们需要在以下路径中添加一个帮助程序文件。
app\code\Vendor\Extension\Helper\Data.php
<?php
namespace Vendor\Extension\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $variable
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Variable\Model\Variable $variable
)
{
$this->variable = $variable;
parent::__construct($context);
}
public function getCustomVarible()
{
$variableData = $this->variable->loadByCode('test', 'base'); // Here first parameter is custom-variable-code and second one is store-code
return $variableData->getPlainValue();
}
}结论:
因此,通过这种方式,您可以在Magento 2中使用自定义变量。