1

The problem is to change the name of the product in admin. No need to change other data.

I want to accomplish this. It is really hard for me.

I searched for the document but apparently did not see many people mention :(

Who can give me a solution?

Really thank you.

enter image description here

(Pictures illustrating what I want to do)

Update:

I'm having an error

Boolean value is expected, supported values: array (    0 => true,    1 => 1,    2 => 'true'    3 '>' 1 ',    4 => false,    5 => 0,    6 => 'false'    7 => '0', )

Here is my full code. Please help me.

app\code\MGS\Editnameproduct\registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MGS_Editnameproduct',
__DIR__

app\code\MGS\Editnameproduct\Controller\Adminhtml\Product\InlineEdit.php

namespace MGS\Editnameproduct\Controller\Adminhtml\Product;

use Magento\Backend\App\Action;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class InlineEdit extends \Magento\Backend\App\Action {

const ADMIN_RESOURCE = 'Magento_Catalog::products';

private $product;
protected $productRepository;
protected $resultJsonFactory;
protected $dataObjectHelper;
protected $logger;

public function __construct(
Action\Context $context, ProductRepositoryInterface $productRepository,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
        \Psr\Log\LoggerInterface $logger
) {
    $this->productRepository = $productRepository;
    $this->resultJsonFactory = $resultJsonFactory;
    $this->dataObjectHelper = $dataObjectHelper;
    $this->logger = $logger;
    parent::__construct($context);
}

public function execute() {
    $resultJson = $this->resultJsonFactory->create();

    $postItems = $this->getRequest()->getParam('items', []);
    if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
        return $resultJson->setData([
                    'messages' => [__('Please correct the data sent.')],
                    'error' => true,
        ]);
    }

    foreach ($postItems as $productId => $productRow) {
        $this->setProduct($this->productRepository->getById($productId));

        $data = $this->product->getData();
        // ---- formatting and validating
        // $this->my_server_side_validator($data);
        // $this->my_server_side_formatter($data);

        // saving
        $dataUpdated = array_replace($data, $productRow);
        $this->product->setData($dataUpdated);
        $this->product->save();
    }

    return $resultJson->setData([
                'messages' => $this->getErrorMessages(),
                'error' => $this->isErrorExists()
    ]);
}

protected function getErrorMessages() {
    $messages = [];
    foreach ($this->getMessageManager()->getMessages()->getItems() as $error) {
        $messages[] = $error->getText();
    }
    return $messages;
}

protected function isErrorExists() {
    return (bool) $this->getMessageManager()->getMessages(true)->getCount();
}

protected function setProduct(ProductInterface $product) {
    $this->product = $product;
    return $this;
}

protected function getProduct() {
    return $this->product;
}
}

app\code\MGS\Editnameproduct\Controller\Adminhtml\Product.php

namespace MGS\Editnameproduct\Controller\Adminhtml;
use Magento\Backend\App\Action;
abstract class Product extends \Magento\Backend\App\Action {
const ADMIN_RESOURCE = 'Magento_Catalog::products';

public function __construct(
    \Magento\Backend\App\Action\Context $context
) {
    parent::__construct($context);
}
}

app\code\MGS\Editnameproduct\etc\adminhtml\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="Magento\Catalog\Ui\Component\Listing\Columns" type="MGS\Editnameproduct\Magento\Catalog\Ui\Component\Listing\Columns" />
</config>

app\code\MGS\Editnameproduct\etc\adminhtml\routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="admin">
    <route id="my_product" frontName="my_product">
        <module name="MGS_EditnameProduct"/>
    </route>
</router>
</config>

app\code\MGS\Editnameproduct\etc\module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MGS_Editnameproduct" setup_version="1.0.0">
</module>
</config>

app\code\MGS\Editnameproduct\Magento\Catalog\Ui\Component\Listing\Columns.php

namespace MGS\Editnameproduct\Magento\Catalog\Ui\Component\Listing;
class Columns extends \Magento\Catalog\Ui\Component\Listing\Columns {
// Array of attributes not included in
//  \vendor\magento\module-
catalog\view\adminhtml\ui_component\product_listing.xml
protected $additional_fields = [
    'my_pku'
];
public function __construct(
\Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Catalog\Ui\Component\ColumnFactory $columnFactory,
        \Magento\Catalog\Ui\Component\Listing\Attribute\RepositoryInterface $attributeRepository,
        array $components = [], array $data = []
) {
    parent::__construct($context, $columnFactory, $attributeRepository,
            $components, $data);
}
/**
 * {@inheritdoc}
 */
public function prepare() {
    $columnSortOrder = self::DEFAULT_COLUMNS_MAX_ORDER;
    foreach ($this->attributeRepository->getList() as $attribute) {
        $attr_code = $attribute->getAttributeCode();
        $config = [];
        if (!isset($this->components[$attr_code]) || in_array($attr_code,
                        $this->additional_fields)) {
            $config['sortOrder'] = ++$columnSortOrder;
            if ($attribute->getIsFilterableInGrid()) {
                $config['filter'] = $this->getFilterType($attribute->getFrontendInput());
            }
            // Copy editor configuration for additional attributes
            if (isset($this->components[$attr_code]->_data['config']['editor'])) {
                $config['editor'] = $this->components[$attr_code]->_data['config']['editor'];
            }
            $column = $this->columnFactory->create($attribute,
                    $this->getContext(), $config);
            $column->prepare();
            $this->addComponent($attribute->getAttributeCode(), $column);
        }
    }
    parent::prepare();
}
}

app\code\MGS\Editnameproduct\view\adminhtml\ui_component\product_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<dataSource name="product_listing_data_source"></dataSource>
<listingToolbar name="listing_top"></listingToolbar>
<columns name="product_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="editorConfig" xsi:type="array">
            <item name="selectProvider" xsi:type="string">product_listing.product_listing.product_columns.ids</item>
            <item name="enabled" xsi:type="boolean">true</item>
            <item name="indexField" xsi:type="string">entity_id</item>
            <item name="clientConfig" xsi:type="array">
                <item name="saveUrl" xsi:type="url" path="my_product/product/inlineEdit"/>
                <item name="validateBeforeSave" xsi:type="boolean">false</item>
            </item>
        </item>
        <item name="childDefaults" xsi:type="array">
            <item name="fieldAction" xsi:type="array">
                <item name="provider" xsi:type="string">product_listing.product_listing.product_columns_editor</item>
                <item name="target" xsi:type="string">startEdit</item>
                <item name="params" xsi:type="array">
                    <item name="0" xsi:type="string">${ $.$data.rowIndex }
</item>
                    <item name="1" xsi:type="boolean">true</item>
                </item>                        
            </item>
        </item>
    </item>
</argument>
<column name="name">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="editor" xsi:type="string">text</item>
        </item>
    </argument>
</column>
<column name="my_pku">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="editor" xsi:type="string">text</item>                    
        </item>
    </argument>
</column>
</columns>
</listing>

Your Answer

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

Browse other questions tagged or ask your own question.