Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements and new features. #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions app/code/community/Bubble/Api/Helper/Catalog/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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]);
}
}
Expand Down