简体   繁体   中英

magento2 create custom variables programmatically

Magento 2 also comes with Custom Variables like in Magento 1. Previously to set a custom variable in Magento 1 programmatically was doing something similar to the following:

$variable = Mage::getModel('core/variable')
                  ->setCode('variable-code')
                  ->setName('Variable Name')
                  ->setPlainValue(0)
                  ->save();

For Magento 2, in my current scenario I would like to create custom variables programmatically in the InstallData.php script instead of website backend. I only find via website backend, but I always prefer programmatically due to versioning advantages.

Solved. Something like the following works as expected

...
use Magento\Variable\Model\VariableFactory;

class InstallData implements InstallDataInterface
{

    protected $varFActory;

    public function __construct(VariableFactory $varFactory)
    {
        $this->varFActory = $varFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {

        $variable = $this->varFActory->create();
        $data = [
            'code' => '',
            'name' => '',
            'html_value' => '',
            'plain_value' => '',

        ];
        $variable->setData($data);
        $variable->save();          
    }
}

To update an existing variable you can do it by:

$var = $this->varFactory->create();
$var->loadByCode('YOUR_CUSTOM_VARIABLE_CODE');

$data = [
    'variable_id' => $var->getId(),
    'code' => 'YOUR_CUSTOM_VARIABLE_CODE',
    'name' => 'YOUR_CUSTOM_VARIABLE_NAME',
    'html_value' => 'YOUR_CUSTOM_VALUE_IN_HTML',
    'plain_value' => 'YOUR_CUSTOM_VALUE_IN_PLAINTEXT'
];

$var->setData($data);
$var->save();

In at least 2.3.4 you are able to define custom new variables via XML

For my use case I created a custom field within the system configuration and allowed that to be a variable as below:

etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="general">
            <group id="store_information">
                <field id="name_alt" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Store Name Alt</label>
                </field>
            </group>
        </section>
    </system>
</config>

etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Variable\Model\Source\Variables">
        <arguments>
            <argument name="configPaths" xsi:type="array">
                <item name="general/store_information" xsi:type="array">
                    <item name="general/store_information/name_alt" xsi:type="string">1</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM