37
loading...
This website collects cookies to deliver better user experience
/**
* @var Magento\Catalog\Model\Product $product
* @var Magento\Catalog\Api\ProductRepositoryInterface $productRepository
*/
$product = $productRepository->get('simple', false, null, true);
$product->setData('attribute_code', $value);
$productRepository->save($product);
namespace Tschallacka\Example\Attributes;
class Attributes
{
const MY_ATTRIBUTE_NAME = 'my_supplier';
}
class Suppliers
{
const SUPPLIER_A = 'Supplier a';
const SUPPLIER_B = 'Supplier B';
const SUPPLIER_C = 'C Supplier inc.';
}
namespace Tschallacka\Example\Helper;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Helper\Context;
class AttributeHelper extends AbstractHelper
{
static $archive = [];
public function __construct(
Context $context,
AttributeRepositoryInterface $attribute_repository
) {
$this->attribute_repository = $attribute_repository;
parent::__construct($context);
}
public function getAttributeValueForStringValue($attribute_code, $search_string)
{
$options = $this->getAttributeOptions($attribute_code);
if(array_key_exists($search_string, $options)) {
return $options[$search_string];
}
}
public function getAttributeOptions($attribute_code)
{
if(!array_key_exists($attribute_code, self::$archive)) {
self::$archive[$attribute_code] = array_reduce(
$this->attribute_repository
->get(Product::ENTITY, $attribute_code)
->getSource()
->getAllOptions(false))
)
, function ($collector, $attribute) {
$collector[$attribute['label']] = $attribute['value'];
return $collector;
}, []);
}
return self::$archive[$attribute_code];
}
}
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\ObjectManager;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Tschallacka\Helper\AttributeHelper;
use Tschallacka\Attributes\Attributes;
use Tschallacka\Attributes\Suppliers;
class MyIntegrationTest extends TestCase
{
public static function modifySimpleProduct()
{
/** @var ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
$attributeRepository = $objectManager->create(AttributeRepositoryInterface::class);
/** @var AttributeHelper $attributeHelper */
$attributeHelper = $objectManager->create(AttributeHelper::class);
$option = $attributeHelper->getAttributeValueForStringValue(Attributes::MY_ATTRIBUTE_NAME, Suppliers::SUPPLIER_C);
/** @var Product $product */
$product = $productRepository->get('simple', false, null, true);
$product->setData(Attributes::MY_ATTRIBUTE_NAME, $option);
$productRepository->save($product);
}
}