assignSimpleProductToConfigurable()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 44
rs 8.439
cc 6
eloc 28
nc 10
nop 2
1
<?php
2
3
namespace Jh\DataImportMagento\Service;
4
5
use Exception;
6
use Jh\DataImportMagento\Exception\MagentoSaveException;
7
use Mage_Catalog_Model_Product;
8
use Mage_Eav_Model_Entity_Attribute;
9
10
/**
11
 * Class ConfigurableProductService
12
 * @package Jh\DataImportMagento\Service
13
 * @author  Aydin Hassan <[email protected]>
14
 */
15
class ConfigurableProductService
16
{
17
18
    /**
19
     * @var Mage_Eav_Model_Entity_Attribute
20
     */
21
    protected $eavAttrModel;
22
23
    /**
24
     * @var Mage_Catalog_Model_Product
25
     */
26
    protected $productModel;
27
28
    /**
29
     * @param Mage_Eav_Model_Entity_Attribute $eavAttrModel
30
     * @param Mage_Catalog_Model_Product       $product
31
     */
32
    public function __construct(Mage_Eav_Model_Entity_Attribute $eavAttrModel, Mage_Catalog_Model_Product $product)
33
    {
34
        $this->eavAttrModel = $eavAttrModel;
35
        $this->productModel = $product;
36
    }
37
38
    /**
39
     * @param \Mage_Catalog_Model_Product $product
40
     * @param string                      $parentSku
41
     *
42
     * @throws MagentoSaveException
43
     */
44
    public function assignSimpleProductToConfigurable(
45
        \Mage_Catalog_Model_Product $product,
46
        $parentSku
47
    ) {
48
        $configProduct  = $this->productModel
49
            ->loadByAttribute('sku', $parentSku);
50
51
        if (false === $configProduct) {
52
            throw new MagentoSaveException(sprintf('Parent product with SKU: "%s" does not exist', $parentSku));
53
        }
54
55
        if ($configProduct->getData('type_id') !== 'configurable') {
56
            throw new MagentoSaveException(sprintf('Parent product with SKU: "%s" is not configurable', $parentSku));
57
        }
58
59
        $configType = $configProduct->getTypeInstance();
60
        $attributes = $configType->getConfigurableAttributesAsArray($configProduct);
61
62
        $configData = [];
63
        foreach ($attributes as $attribute) {
64
            $attributeCode    = $attribute['attribute_code'];
65
            $configData[]     = [
66
                'attribute_id'  => $this->eavAttrModel->getIdByCode('catalog_product', $attributeCode),
67
                'label'         => $product->getAttributeText($attributeCode),
68
                'value_index'   => $product->getData($attributeCode),
69
                'pricing_value' => $product->getPrice(),
70
            ];
71
        }
72
73
        //We wanna keep the old used products as well so we add them to the config too. Their ids are enough.
74
        $newProductsRelations = [];
75
        foreach ($configType->getUsedProductIds() as $existingUsedProductId) {
76
            $newProductsRelations[$existingUsedProductId] = [];
77
        }
78
79
        $newProductsRelations[$product->getId()] = $configData;
80
        $configProduct->setData('configurable_products_data', $newProductsRelations);
81
82
        try {
83
            $configProduct->save();
84
        } catch (Exception $e) {
85
            throw new MagentoSaveException($e->getMessage(), 0, $e);
86
        }
87
    }
88
89
    /**
90
     * @param \Mage_Catalog_Model_Product $product
91
     * @param array                       $configurableAttributes
92
     *
93
     * @throws MagentoSaveException
94
     */
95
    public function setupConfigurableProduct(\Mage_Catalog_Model_Product $product, array $configurableAttributes)
96
    {
97
        $productTypeInstance = $product->getTypeInstance();
98
        $attributeIds = [];
99
100
        //get attribute ID's
101
        foreach ($configurableAttributes as $attribute) {
102
            $attributeId = $this->eavAttrModel->getIdByCode('catalog_product', $attribute);
103
104
            if (false === $attributeId) {
105
                throw new MagentoSaveException(
106
                    sprintf(
107
                        'Cannot create configurable product with SKU: "%s". Attribute: "%s" does not exist',
108
                        $product->getData('sku'),
109
                        $attribute
110
                    )
111
                );
112
            }
113
114
            if (!$productTypeInstance->getAttributeById($attributeId)) {
115
                $msg  = 'Cannot create configurable product with SKU: "%s". Attribute: "%s" is not assigned ';
116
                $msg .= 'to the attribute set: "%s"';
117
118
                throw new MagentoSaveException(
119
                    sprintf($msg, $product->getData('sku'), $attribute, $product->getData('attribute_set_id'))
120
                );
121
            }
122
123
            $attributeIds[] = $attributeId;
124
        }
125
126
        //set the attributes that should be configurable for this product
127
        $productTypeInstance->setUsedProductAttributeIds($attributeIds);
128
        $configurableAttributesData = $productTypeInstance->getConfigurableAttributesAsArray();
129
130
        $product->addData([
131
            'can_save_configurable_attributes' => true,
132
            'configurable_attributes_data'     => $configurableAttributesData,
133
        ]);
134
    }
135
}
136