To achieve the functionality of translating child product names in Magento according to their respective store languages, you can follow these steps:
Enable Multiple Store Views: Make sure you have created and enabled multiple store views, each corresponding to a specific language.
Configure Attribute Labels: Ensure that the Color and Size attributes have the appropriate labels for each store view. You can manage this in the Admin Panel by navigating to "Stores" -> "Attributes" -> "Product" and editing the respective attributes.
Translate Attribute Options: Translate the options of the Color and Size attributes into the desired languages. You can do this by editing each attribute and adding the translations for each option in the "Manage Labels/Options" section.
Update Existing Products: To apply the naming convention to existing products, you can use a script or a Magento data migration tool. This script should iterate through each child product and update its name based on the configured rules.
Here's a sample script to help you understand the process:
<?php
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Table;
use Magento\Eav\Model\Entity\AttributeFactory;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface;
class ProductNameTranslator
{
private $productRepository;
private $attributeFactory;
private $storeManager;
private $appState;
private $registry;
public function __construct(
ProductRepositoryInterface $productRepository,
AttributeFactory $attributeFactory,
StoreManagerInterface $storeManager,
State $appState,
Registry $registry
) {
$this->productRepository = $productRepository;
$this->attributeFactory = $attributeFactory;
$this->storeManager = $storeManager;
$this->appState = $appState;
$this->registry = $registry;
}
public function translateProductNames()
{
try {
$this->appState->setAreaCode('adminhtml');
} catch (LocalizedException $e) {
// Handle the exception
}
$products = $this->productRepository->getList();
foreach ($products as $product) {
$this->registry->register('product', $product);
$configurableProduct = $this->getConfigurableProduct();
$childProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
foreach ($childProducts as $childProduct) {
$translatedName = $this->generateTranslatedProductName($childProduct);
$childProduct->setName($translatedName);
$this->productRepository->save($childProduct);
}
}
}
private function getConfigurableProduct(): ProductInterface
{
/** @var ProductInterface $product */
$product = $this->registry->registry('product');
if ($product->getTypeId() === 'configurable') {
return $product;
}
// If the current product is not a configurable product, find and return its parent
$parentIds = $product->getExtensionAttributes()->getConfigurableProductLinks();
$parentId = reset($parentIds);
return $this->productRepository->getById($parentId);
}
private function generateTranslatedProductName(ProductInterface $product): string
{
$colorAttributeCode = 'color';
$sizeAttributeCode = 'size';
$colorOptionValue = $this->getAttributeOptionValue($colorAttributeCode, $product);
$sizeOptionValue = $this->getAttributeOptionValue($sizeAttributeCode, $product);
$translatedColor = $this->translateAttributeValue($colorAttributeCode, $colorOptionValue);
$translatedSize = $this->translateAttributeValue($sizeAttributeCode, $sizeOptionValue);
$configurableProduct = $this->getConfigurableProduct();
$baseName = $configurableProduct->getName();
$translatedName = $baseName . ' ' . $translatedColor . ' ' . $translatedSize;
return $translatedName;
}
private function getAttributeOptionValue(string $attributeCode, ProductInterface $product): string
{
$attribute = $this->attributeFactory->create()->loadByCode('catalog_product', $attributeCode);
$optionId = $product->getData($attribute->getAttributeCode());
$optionValue = '';
if ($optionId) {
$option = $attribute->getSource()->getOptionText($optionId);
if ($option) {
$optionValue = $option;
}
}
return $optionValue;
}
private function translateAttributeValue(string $attributeCode, string $attributeValue): string
{
$storeId = $this->storeManager->getStore()->getId();
$attribute = $this->attributeFactory->create()->loadByCode('catalog_product', $attributeCode);
$optionId = $attribute->getSource()->getOptionId($attributeValue);
$translatedValue = $attribute->setStoreId($storeId)->getSource()->getOptionText($optionId);
return $translatedValue ?: $attributeValue;
}
}
// Usage:
$translator = new ProductNameTranslator(
$objectManager->get(ProductRepositoryInterface::class),
$objectManager->get(AttributeFactory::class),
$objectManager->get(StoreManagerInterface::class),
$objectManager->get(State::class),
$objectManager->get(Registry::class)
);
$translator->translateProductNames();