How can I change the position of Save, Save & Close buttons on the product add/edit?
I need to place the "Save & close" button first instead of "Save" button on the product add/edit form.
How can I achieve this?
Try following way:
app/code/SR/MagentoCommunity/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">
<type name="Magento\ConfigurableProduct\Block\Adminhtml\Product\Edit\Button\Save">
<plugin name="SR_MagentoCommunity::change_product_button_position"
type="SR\MagentoCommunity\Plugin\Catalog\Block\Adminhtml\Product\Edit\Button\Save" sortOrder="1"/>
</type>
</config>
app/code/SR/MagentoCommunity/Plugin/Catalog/Block/Adminhtml/Product/Edit/Button/Save.php
<?php
namespace SR\MagentoCommunity\Plugin\Catalog\Block\Adminhtml\Product\Edit\Button;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\UiComponent\Context;
use Magento\Ui\Component\Control\Container;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableType;
class Save
{
/**
* Url Builder
*
* @var Context
*/
protected $context;
/**
* Registry
*
* @var Registry
*/
protected $registry;
/**
* Generic constructor
*
* @param Context $context
* @param Registry $registry
*/
public function __construct(
Context $context,
Registry $registry
) {
$this->context = $context;
$this->registry = $registry;
}
/**
* Generate url by route and parameters
*
* @param string $route
* @param array $params
* @return string
*/
public function getUrl($route = '', $params = [])
{
return $this->context->getUrl($route, $params);
}
/**
* Get product
*
* @return ProductInterface
*/
public function getProduct()
{
return $this->registry->registry('current_product');
}
public function aroundGetButtonData(
\Magento\ConfigurableProduct\Block\Adminhtml\Product\Edit\Button\Save $subject,
\Closure $proceed
) {
//return $proceed();
if ($this->getProduct()->isReadonly()) {
return [];
}
return [
'id_hard' => 'save_and_close',
'label' => __('Save & Close'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => [
'buttonAdapter' => [
'actions' => [
[
'targetName' => $this->getSaveTarget(),
'actionName' => $this->getSaveAction(),
'params' => [
true
]
]
]
]
]
],
'class_name' => Container::SPLIT_BUTTON,
'options' => $this->getOptions(),
];
}
/**
* Retrieve options
*
* @return array
*/
protected function getOptions()
{
$options[] = [
'id_hard' => 'save-button',
'label' => __('Save'),
'data_attribute' => [
'mage-init' => [
'buttonAdapter' => [
'actions' => [
[
'targetName' => $this->getSaveTarget(),
'actionName' => $this->getSaveAction(),
'params' => [
false
]
]
]
]
]
],
];
$options[] = [
'id_hard' => 'save_and_new',
'label' => __('Save & New'),
'data_attribute' => [
'mage-init' => [
'buttonAdapter' => [
'actions' => [
[
'targetName' => $this->getSaveTarget(),
'actionName' => $this->getSaveAction(),
'params' => [
true,
[
'back' => 'new'
]
]
]
]
]
]
],
];
if (!$this->context->getRequestParam('popup') && $this->getProduct()->isDuplicable()) {
$options[] = [
'label' => __('Save & Duplicate'),
'id_hard' => 'save_and_duplicate',
'data_attribute' => [
'mage-init' => [
'buttonAdapter' => [
'actions' => [
[
'targetName' => $this->getSaveTarget(),
'actionName' => $this->getSaveAction(),
'params' => [
true,
[
'back' => 'duplicate'
]
]
]
]
]
]
],
];
}
return $options;
}
/**
* Retrieve target for button.
*
* @return string
*/
protected function getSaveTarget()
{
$target = 'product_form.product_form';
if ($this->isConfigurableProduct()) {
$target = 'product_form.product_form.configurableVariations';
}
return $target;
}
/**
* Retrieve action for button.
*
* @return string
*/
protected function getSaveAction()
{
$action = 'save';
if ($this->isConfigurableProduct()) {
$action = 'saveFormHandler';
}
return $action;
}
/**
* Is configurable product.
*
* @return boolean
*/
protected function isConfigurableProduct()
{
return !$this->getProduct()->isComposite() || $this->getProduct()->getTypeId() === ConfigurableType::TYPE_CODE;
}
}
Modify them with CSS. Make their container display:flex
add a flex-direction
and then on the save & close button put order:1
and order:2
on the save button. Here is a link for reference https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Ordering_Flex_Items