SHARE
TWEET

Untitled

a guest Jun 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MagentoFrameworkRegistry $registry,
  2.     MagentoConfigurableProductModelResourceModelProductTypeConfigurable $configurableProductType,
  3.     MagentoCatalogModelResourceModelProductAction $action
  4. ){
  5.  
  6.     $this->registry = $registry;
  7.     $this->configurableProductType = $configurableProductType;
  8.     $this->action=$action;
  9. }
  10.      
  11. $_product= $this->registry->registry('current_product');
  12.     $productIds = $_product->getId();
  13. $result = $closure($productIds, $attrData, $storeId);
  14. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  15.  
  16. $attributeCode = 'choix_prix_product';
  17.  
  18. $attribute = $objectManager->create('MagentoEavModelConfig')
  19.     ->getAttribute('catalog_product', $attributeCode);
  20.  
  21. $options = $attribute->getSource()->getAllOptions();
  22.  
  23.  
  24. foreach ($options as $option) {
  25.     $products = $objectManager->create('MagentoCatalogModelProduct')
  26.         ->getCollection()
  27.         ->addAttributeToFilter($attributeCode, $option['value']);
  28.  
  29.     $productIds = [];
  30.     foreach ($products as $prod) {
  31.         $productIds[] = $prod->getId();
  32.     }
  33.  
  34.  
  35. }
  36. $duplicateOptions = [];
  37. foreach ($options as $option) {
  38.     // because the first option can be blank
  39.     if (!empty(trim($option['label']))) {
  40.         $products = $objectManager->create('MagentoCatalogModelProduct')
  41.             ->getCollection()
  42.             ->addAttributeToFilter($attributeCode, $option['value']);
  43.  
  44.         if ($products->count()) {
  45.             $productsCount = $products->count();
  46.         } else {
  47.             $productsCount = 0;
  48.         }
  49.  
  50.         $duplicateOptions[$option['label']][$option['value']] = $productsCount;
  51.     }
  52. }
  53.  
  54.  
  55. foreach ($duplicateOptions as $key => $value) {
  56.     if (count($value) > 1) {
  57.         foreach ($value as $k => $v) {
  58.             if ($v == 0) {
  59.                 $duplicateOptions[$key]['delete'][] = $k;
  60.             }
  61.         }
  62.  
  63.         if (isset($duplicateOptions[$key]['delete'])) {
  64.             /**
  65.              * if multiple options are in delete array,
  66.              * i.e. multiple option ids have zero product count
  67.              */
  68.             if (count($duplicateOptions[$key]['delete']) > 1) {
  69.                 sort($duplicateOptions[$key]['delete']); // sort the array
  70.                 array_shift($duplicateOptions[$key]['delete']); // remove the first item of the array
  71.  
  72.                 /**
  73.                  * if you assume that the older option id is the duplicate one
  74.                  * then, you keep the older option id in the delete list
  75.                  * and remove the latest/newly-added attribute option from the delete list
  76.                  */
  77.                 // rsort($duplicateOptions[$key]['delete']); // reverse sort the array
  78.                 // array_shift($duplicateOptions[$key]['delete']); // remove the first item of the array
  79.  
  80.                 // DELETE DUPLICATE ATTRIBUTE OPTIONS
  81.                 foreach ($duplicateOptions[$key]['delete'] as $optionId) {
  82.                     $optionModel = $objectManager->create('MagentoEavModelEntityAttributeOption')->load($optionId);
  83.                     try {
  84.                         $optionModel->delete();
  85.                         echo '<font color="green">"' . $key . ' (' . $optionId . ')" Option Deleted!</font><br />';
  86.                     } catch (Exception $e) {
  87.                         echo '<font color="red">' . $e->getMessage() . '</font><br />';
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
  94. $_name = $_product->getName();
  95. $_sku = $_product->getSku();
  96. $_id = $_product->getId();
  97.  
  98. $config = $this->configurableProductType->getParentIdsByChild($_id);// to know if a product is a variant of a configurable product
  99.  
  100. if ($config) {
  101.  
  102.  
  103.     $optionId = $_product->getData('choix_prix_product');
  104.  
  105.     $isAttributeExist = $_product->getResource()->getAttribute('choix_prix_product');
  106.     if ($isAttributeExist && $isAttributeExist->usesSource()) {
  107.         $optionText = $isAttributeExist->getSource()->getOptionText($optionId);
  108.     }
  109.  
  110.  
  111.     $choixAtt = explode("-", $optionText);
  112.     $var7 = $choixAtt[0];
  113.     $var8=substr($var7,6);
  114.  
  115.  
  116.  
  117.     $choixvalue = substr($var7, 6);
  118.  
  119.     //add code choice in the sku of article and add the desgnation to the name of the child
  120.     $this->action->updateAttributes([$_id], ['name' => $_name . " " . $var7], $storeId);
  121.     var_dump( $this->action->updateAttributes([$_id], ['name' => $_name . " " . $var7], $storeId));
  122.     die('here');
  123.  
  124.     //$_product->setName($_name . " " . $var7);
  125.  
  126.     $newsku= explode("-",$_sku);
  127.  
  128.     $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  129.     $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
  130.     $connection = $resource->getConnection();
  131.     $tableName = $resource->getTableName('table_choix');
  132.  
  133.  
  134.     $fields = array('code_choix');
  135.  
  136.  
  137.     $sql = $connection->select()
  138.         ->from($tableName, $fields)
  139.         ->where('designation_choix' . '=?', $var8);
  140.  
  141.  
  142.  
  143.     $result = $connection->fetchAll($sql);
  144.     $var9 = $result[0]['code_choix'];
  145.     $sku=$newsku.$var9;
  146.  
  147.  
  148.      $_product->setSku($sku);
  149.     $this->action->updateAttributes([$_id], ['sku' => $sku], $storeId);
  150.      
  151. }
  152.  
  153.  
  154.  
  155.  
  156.     return $result;
  157. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top