Created
April 8, 2019 08:16
-
-
Save roma-glushko/3c826d56fdba092036d2d656e4d3242b to your computer and use it in GitHub Desktop.
Usage of Magento\Framework\Stdlib\ArrayManager in Magento 2 while working with nested arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Vendor\Module\Plugin\Checkout; | |
use Magento\Checkout\Model\DefaultConfigProvider; | |
use Magento\Framework\Stdlib\ArrayManager; | |
class AddProductLabelPlugin | |
{ | |
/** | |
* @var ArrayManager | |
*/ | |
protected $arrayManager; | |
/** | |
* @param ArrayManager $arrayManager | |
*/ | |
public function __construct( | |
ArrayManager $arrayManager | |
) { | |
$this->arrayManager = $arrayManager; | |
} | |
/** | |
* Adds shipping label for product data | |
* | |
* @param DefaultConfigProvider $subject | |
* @param array $result | |
* | |
* @return array | |
* | |
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
*/ | |
public function afterGetConfig(DefaultConfigProvider $subject, array $result): array | |
{ | |
if (!$this->arrayManager->exists('totalsData/items', $result)) { | |
return $result; | |
} | |
$items = $this->arrayManager->get('totalsData/items', $result); | |
foreach ($items as $index => $itemData) { | |
$result = $this->arrayManager->set( | |
'totalsData/items/' . $index .'/shipping_method', | |
$result, | |
__('Some label') | |
); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment