2

After Adding a Product Redirect to Shopping Cart, Stores Config Section

So in the Magento2 admin, under Stores > Configuration > Sales > Checkout > Shopping Cart, I would like the "After Adding a Product Redirect to Shopping Cart" dropdown with the Yes/No option to either: -Only have the option to set it to "No". -Have a comment for the field, so that the customer only knows to set it to "No".

I see the field is created in the Magento2 vendor in this file: "vendor/magento/module-checkout/etc/adminhtml/system.xml" (in the code below, see the field tag with the id "redirect_to_cart", in the group tag with the id "cart".

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="checkout" translate="label" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Checkout</label>
            <tab>sales</tab>
            <resource>Magento_Checkout::checkout</resource>
            <group id="options" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Checkout Options</label>
                <field id="onepage_checkout_enabled" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Onepage Checkout</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="guest_checkout" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Allow Guest Checkout</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="customer_must_be_logged" translate="label" type="select" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Require Customer To Be Logged In To Checkout</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <depends>
                        <field id="guest_checkout">0</field>
                    </depends>
                </field>
            </group>
            <group id="cart" translate="label" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Shopping Cart</label>
                <field id="delete_quote_after" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Quote Lifetime (days)</label>
                </field>
                <field id="redirect_to_cart" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>After Adding a Product Redirect to Shopping Cart</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
            <group id="cart_link" translate="label" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>My Cart Link</label>
                <field id="use_qty" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Display Cart Summary</label>
                    <source_model>Magento\Checkout\Model\Config\Source\Cart\Summary</source_model>
                </field>
            </group>
            <group id="sidebar" translate="label" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Shopping Cart Sidebar</label>
                <field id="display" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Display Shopping Cart Sidebar</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="count" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Maximum Display Recently Added Item(s)</label>
                </field>
            </group>
            <group id="payment_failed" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Payment Failed Emails</label>
                <field id="identity" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Payment Failed Email Sender</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Identity</source_model>
                </field>
                <field id="receiver" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Payment Failed Email Receiver</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Identity</source_model>
                </field>
                <field id="template" translate="label comment" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Payment Failed Template</label>
                    <comment>Email template chosen based on theme fallback when "Default" option is selected.</comment>
                    <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                </field>
                <field id="copy_to" translate="label comment" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Send Payment Failed Email Copy To</label>
                    <comment>Separate by ",".</comment>
                </field>
                <field id="copy_method" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Send Payment Failed Email Copy Method</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Method</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

3 Answers 3

0

I found out how to add a comment to an existing field in Stores Configuration in the Admin! I just created a system.xml file in a new extension, and used the existing section id, group id, and field id. Here is my code in /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="checkout" translate="label" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
            <group id="cart" translate="label" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="redirect_to_cart" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <comment>Please only set value to "No".</comment>
                </field>
            </group>
        </section>
    </system>
</config>
0

How about setting a different source_model to a custom model like below?

<?php
namespace MyModule\MyExample\Model\Config\Source;

class No implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * Options getter
     *
     * @return array
     */
    public function toOptionArray()
    {
        return [['value' => 0, 'label' => __('No')]];
    }

    /**
     * Get options in "key-value" format
     *
     * @return array
     */
    public function toArray()
    {
        return [0 => __('No')];
    }
}
0

Steps to Override Vendor Configuration Field:

=> Create a Custom Module:

In your Magento 2 installation, create a new module. You can place it in the app/code directory or use a composer-based approach.

=> Declare Dependency Injection Preference:

In your custom module, declare a preference in the di.xml file to override the vendor class responsible for the configuration.

app/code/YourVendor/YourModule/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"><preference for="Vendor\Module\Model\Config" type="YourVendor\YourModule\Model\Config" /></config>

=> Create a Custom Configuration Model:

Now, create a custom configuration model that extends the vendor's configuration model. Override the method where the configuration field is defined.

=> app/code/YourVendor/YourModule/Model/Config.php

<?php namespace YourVendor\YourModule\Model; use Vendor\Module\Model\Config as VendorConfig; class Config extends VendorConfig { public function getYourCustomField() { // Your custom logic to get the value of the field // You can modify or replace the logic from the vendor class } }

=> Update Configuration:

Update your configuration field in your custom model according to your requirements.

=> Clear Cache:

After making changes, clear the Magento cache to apply the configuration overrides.

php bin/magento cache:clean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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