Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roma-glushko/3c826d56fdba092036d2d656e4d3242b to your computer and use it in GitHub Desktop.
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
<?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