diff --git a/app/code/community/Bubble/Api/Helper/Catalog/Product.php b/app/code/community/Bubble/Api/Helper/Catalog/Product.php index 4f76934..d72ab2c 100644 --- a/app/code/community/Bubble/Api/Helper/Catalog/Product.php +++ b/app/code/community/Bubble/Api/Helper/Catalog/Product.php @@ -23,7 +23,7 @@ public function associateProducts(Mage_Catalog_Model_Product $product, $simpleSk ->addFilterByRequiredOptions() ->getAllIds(); - $usedProductIds = array_diff($newProductIds, $oldProductIds); + $usedProductIds = array_unique(array_merge($newProductIds, $oldProductIds)); if (!empty($usedProductIds)) { if ($product->isConfigurable()) { @@ -59,7 +59,6 @@ public function getCategoryIdsByNames($categoryNames) if (!empty($parentIds)) { $collection->getSelect()->where('parent_id IN (?)', $parentIds); } - $parentIds = array(); if ($collection->count()) { foreach ($collection as $category) { $addCategories[] = (int) $category->getId(); @@ -68,6 +67,15 @@ public function getCategoryIdsByNames($categoryNames) } $parentIds[] = $category->getId(); } + } else { + if ($level > 0) { + $parentId = end($parentIds); + } else { + $parentId = Mage::app()->getWebsite(1)->getDefaultStore()->getRootCategoryId(); + } + $newCategoryId = $this->createCategory($name, $parentId); + $addCategories[] = $newCategoryId; + $parentIds[] = $newCategoryId; } } if (!empty($addCategories)) { @@ -79,6 +87,24 @@ public function getCategoryIdsByNames($categoryNames) return !empty($categories) ? $categories : $categoryNames; } + /** + * @param string $name + * @param int|string $parentId + * @return string + */ + public function createCategory($name, $parentId) + { + $category = Mage::getModel('catalog/category'); + $parentCategory = Mage::getModel('catalog/category')->load($parentId); + + $category->setName($name) + ->setIsActive(1) + ->setPath($parentCategory->getPath()); + $category->save(); + + return $category->getId(); + } + /** * @param string $attributeCode * @param string $label @@ -96,9 +122,35 @@ public function getOptionKeyByLabel($attributeCode, $label) } } + if ($newOptionId = $this->createOption($attribute, $label)) { + return $newOptionId; + } + return $label; } + /** + * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute + * @param string $label + * @return string|null + */ + public function createOption(Mage_Catalog_Model_Resource_Eav_Attribute $attribute, $label) + { + $option = array( + 'value' => array( + 'option' => array($label) + ) + ); + $attribute->setOption($option); + $attribute->save(); + + $optionId = Mage::getModel('eav/entity_attribute_source_table') + ->setAttribute($attribute) + ->getOptionId($label); + + return $optionId; + } + protected function _getCatagoriesSeparator() { return Mage::getStoreConfig(self::CATEGORIES_SEPARATOR_PATH_XML); @@ -134,7 +186,7 @@ protected function _initConfigurableAttributesData(Mage_Catalog_Model_Product $m } if (!empty($configurableAttributes)){ foreach ($attributesData as $idx => $val) { - if (!in_array($val['attribute_id'], $configurableAttributes)) { + if (!in_array($val['attribute_code'], $configurableAttributes)) { unset($attributesData[$idx]); } }