Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MagentoFrameworkRegistry $registry,
- MagentoConfigurableProductModelResourceModelProductTypeConfigurable $configurableProductType,
- MagentoCatalogModelResourceModelProductAction $action
- ){
- $this->registry = $registry;
- $this->configurableProductType = $configurableProductType;
- $this->action=$action;
- }
- $_product= $this->registry->registry('current_product');
- $productIds = $_product->getId();
- $result = $closure($productIds, $attrData, $storeId);
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- $attributeCode = 'choix_prix_product';
- $attribute = $objectManager->create('MagentoEavModelConfig')
- ->getAttribute('catalog_product', $attributeCode);
- $options = $attribute->getSource()->getAllOptions();
- foreach ($options as $option) {
- $products = $objectManager->create('MagentoCatalogModelProduct')
- ->getCollection()
- ->addAttributeToFilter($attributeCode, $option['value']);
- $productIds = [];
- foreach ($products as $prod) {
- $productIds[] = $prod->getId();
- }
- }
- $duplicateOptions = [];
- foreach ($options as $option) {
- // because the first option can be blank
- if (!empty(trim($option['label']))) {
- $products = $objectManager->create('MagentoCatalogModelProduct')
- ->getCollection()
- ->addAttributeToFilter($attributeCode, $option['value']);
- if ($products->count()) {
- $productsCount = $products->count();
- } else {
- $productsCount = 0;
- }
- $duplicateOptions[$option['label']][$option['value']] = $productsCount;
- }
- }
- foreach ($duplicateOptions as $key => $value) {
- if (count($value) > 1) {
- foreach ($value as $k => $v) {
- if ($v == 0) {
- $duplicateOptions[$key]['delete'][] = $k;
- }
- }
- if (isset($duplicateOptions[$key]['delete'])) {
- /**
- * if multiple options are in delete array,
- * i.e. multiple option ids have zero product count
- */
- if (count($duplicateOptions[$key]['delete']) > 1) {
- sort($duplicateOptions[$key]['delete']); // sort the array
- array_shift($duplicateOptions[$key]['delete']); // remove the first item of the array
- /**
- * if you assume that the older option id is the duplicate one
- * then, you keep the older option id in the delete list
- * and remove the latest/newly-added attribute option from the delete list
- */
- // rsort($duplicateOptions[$key]['delete']); // reverse sort the array
- // array_shift($duplicateOptions[$key]['delete']); // remove the first item of the array
- // DELETE DUPLICATE ATTRIBUTE OPTIONS
- foreach ($duplicateOptions[$key]['delete'] as $optionId) {
- $optionModel = $objectManager->create('MagentoEavModelEntityAttributeOption')->load($optionId);
- try {
- $optionModel->delete();
- echo '<font color="green">"' . $key . ' (' . $optionId . ')" Option Deleted!</font><br />';
- } catch (Exception $e) {
- echo '<font color="red">' . $e->getMessage() . '</font><br />';
- }
- }
- }
- }
- }
- }
- $_name = $_product->getName();
- $_sku = $_product->getSku();
- $_id = $_product->getId();
- $config = $this->configurableProductType->getParentIdsByChild($_id);// to know if a product is a variant of a configurable product
- if ($config) {
- $optionId = $_product->getData('choix_prix_product');
- $isAttributeExist = $_product->getResource()->getAttribute('choix_prix_product');
- if ($isAttributeExist && $isAttributeExist->usesSource()) {
- $optionText = $isAttributeExist->getSource()->getOptionText($optionId);
- }
- $choixAtt = explode("-", $optionText);
- $var7 = $choixAtt[0];
- $var8=substr($var7,6);
- $choixvalue = substr($var7, 6);
- //add code choice in the sku of article and add the desgnation to the name of the child
- $this->action->updateAttributes([$_id], ['name' => $_name . " " . $var7], $storeId);
- var_dump( $this->action->updateAttributes([$_id], ['name' => $_name . " " . $var7], $storeId));
- die('here');
- //$_product->setName($_name . " " . $var7);
- $newsku= explode("-",$_sku);
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
- $connection = $resource->getConnection();
- $tableName = $resource->getTableName('table_choix');
- $fields = array('code_choix');
- $sql = $connection->select()
- ->from($tableName, $fields)
- ->where('designation_choix' . '=?', $var8);
- $result = $connection->fetchAll($sql);
- $var9 = $result[0]['code_choix'];
- $sku=$newsku.$var9;
- $_product->setSku($sku);
- $this->action->updateAttributes([$_id], ['sku' => $sku], $storeId);
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement