3

I try create custom module config in magento2. This code add field productzoom

Source file: Xyaddons\Productzoom\etc\adminhtml\system.xml.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
    <system>
         <tab id="catalog" translate="label" sortOrder="200">
            <label>Catalog</label>
        </tab>
        <section id="catalog" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Catalog</label>
            <tab>catalog</tab>
            <resource>Magento_Catalog::config_catalog</resource>
            <group id="productzoom" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Xyaddons Productzoom(Product views page)</label>
                <field id="flat_xyaddons_productzoom" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Active</label>
                   <!-- <backend_model>Magento\Catalog\Model\Indexer\Category\Flat\System\Config\Mode</backend_model> -->
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="list_zoom_style" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Set style zoom</label>
                    <source_model>Xyaddons\Productzoom\Model\Config\Source\Productzoom</source_model>
                </field>                
            </group>
        </section>
    </system>
</config> 

source File Productzoom.php : Xyaddons\Productzoom\Model\Config\Source\Productzoom

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Xyaddons\Productzoom\Model\Config\Source;

class Productzoom implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * {@inheritdoc}
     *
     * @codeCoverageIgnore
     */
    public function toOptionArray()
    {
        return [
            ['value' => 'tints', 'label' => __('Tints')],
            ['value' => 'easing', 'label' => __('Easing')]
        ];
    }
}

how do get value in frontend?

  • is there any error you are facing? Code seems fine – Keyur Shah Nov 2 '15 at 5:23
  • plese clean cache after update/add config file – KAndy Nov 2 '15 at 6:01
3
/** @var \Magento\Framework\App\Config\ScopeConfigInterface */
protected $scopeConfig;

/**
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig    
 */
public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
    $this->scopeConfig = $scopeConfig;
}

/**
 * Fetch config value for above case
 * @return string
 */
public function getCustomConfigValue()
{
    return $this->scopeConfig->getValue('catalog/productzoom/flat_xyaddons_productzoom');
}

In your .phtml file, call:

echo $block->getCustomConfigValue();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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