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

Choose attribute for configurable product #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
67 changes: 46 additions & 21 deletions app/code/community/JR/Api/Helper/Catalog/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ class JR_Api_Helper_Catalog_Product extends Mage_Core_Helper_Abstract
* @param array $priceChanges
* @return JR_Api_Helper_Catalog_Product
*/
public function associateProducts(Mage_Catalog_Model_Product $product, $simpleSkus, $priceChanges = array())
public function associateProducts(Mage_Catalog_Model_Product $product, $simpleSkus, $priceChanges = array(), $configAttributes = array())
{
if (!empty($simpleSkus)) {
$usedProductIds = Mage::getModel('catalog/product')->getCollection()

// Get current associations
$usedProductIds = Mage::getModel('catalog/product_type_configurable')->setProduct($product)->getUsedProductCollection()
->addAttributeToSelect('*')
->addFilterByRequiredOptions()
->getAllIds();

// Get new associations
$newProductIds = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('sku', array('in' => (array) $simpleSkus))
->addFieldToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
->getAllIds();

// Combine new and current associations
$usedProductIds = array_unique(array_merge($usedProductIds, $newProductIds));

if (!empty($usedProductIds)) {
if ($product->isConfigurable()) {
$this->_initConfigurableAttributesData($product, $usedProductIds, $priceChanges);
$this->_initConfigurableAttributesData($product, $usedProductIds, $priceChanges, $configAttributes);
} elseif ($product->isGrouped()) {
$relations = array_fill_keys($usedProductIds, array('qty' => 0, 'position' => 0));
$product->setGroupedLinkData($relations);
Expand Down Expand Up @@ -102,32 +114,40 @@ protected function _getCatagoriesSeparator()
* @param array $priceChanges
* @return JR_Api_Helper_Catalog_Product
*/
protected function _initConfigurableAttributesData(Mage_Catalog_Model_Product $mainProduct, $simpleProductIds, $priceChanges = array())
protected function _initConfigurableAttributesData(Mage_Catalog_Model_Product $mainProduct, $simpleProductIds, $priceChanges = array(), $configAttributes = array())
{
if (!$mainProduct->isConfigurable() || empty($simpleProductIds)) {
return $this;
}

$mainProduct->setConfigurableProductsData(array_flip($simpleProductIds));

$productType = $mainProduct->getTypeInstance(true);
$productType->setProduct($mainProduct);
$attributesData = $productType->getConfigurableAttributesAsArray();

if (empty($attributesData)) {
// Auto generation if configurable product has no attribute
$attributeIds = array();
foreach ($productType->getSetAttributes() as $attribute) {
if ($productType->canUseAttribute($attribute)) {
$attributeIds[] = $attribute->getAttributeId();
}

// Get attributes already associated to this product
$used_attr_codes = array();
foreach($productType->getConfigurableAttributesAsArray($mainProduct) as $attr){
$used_attr_codes[] = $attr['attribute_code'];
}

// Add only the new attributes to this product
$new_attr_ids = array();
foreach($productType->getSetAttributes() as $attribute) {
if($productType->canUseAttribute($attribute)) {
if(!empty($configAttributes) &&
in_array($attribute->getAttributeCode(), $configAttributes) &&
!in_array($attribute->getAttributeCode(), $used_attr_codes)) {

$new_attr_ids[] = $attribute->getAttributeId();
}
}
$productType->setUsedProductAttributeIds($attributeIds);
$attributesData = $productType->getConfigurableAttributesAsArray();
}

$products = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($simpleProductIds);

$productType->setUsedProductAttributeIds($new_attr_ids);
$attributesData = $productType->getConfigurableAttributesAsArray();

// Update 'association' attributes
$products = Mage::getModel('catalog/product')->getCollection()->addIdFilter($simpleProductIds);
if (count($products)) {
foreach ($attributesData as &$attribute) {
$attribute['label'] = $attribute['frontend_label'];
Expand Down Expand Up @@ -157,9 +177,14 @@ protected function _initConfigurableAttributesData(Mage_Catalog_Model_Product $m
);
}
}

$mainProduct->setConfigurableAttributesData($attributesData);
}

//
// $log_file = "/home/sourismini/magento_app/prod/var/log/soap.log";
// $log = print_r(array_flip($simpleProductIds), true);
// file_put_contents($log_file, $log, FILE_APPEND);
//
return $this;
}
}
52 changes: 51 additions & 1 deletion app/code/community/JR/Api/Model/Catalog/Product/Api/V2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,48 @@ public function create($type, $set, $sku, $productData, $store = null)
$set = Mage::helper('jr_api')->getAttributeSetIdByName($set);
}

//If the product exists with diffrent configurable attributes, drop and create a similar product
if($type == 'configurable' && isset($productData->additional_attributes['config_attributes'])) {

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$configAttributes = explode(',', $productData->additional_attributes['config_attributes']);

if ($product && !empty($configAttributes)) {
$old_attrs = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);

if((count($old_attrs) != count($configAttributes)) || $old_attrs[0]['attribute_code'] && ($old_attrs[0]['attribute_code'] != $configAttributes[0])) {

$productData->name = $product->getName();
$productData->description = $product->getDescription();
$productData->short_description = $product->getShortDescription();
$productData->weight = $product->getWeight();
$productData->price = $product->getPrice();
$productData->status = $product->getStatus();
$productData->additional_attributes = array(
'orig_name' => $product->getData('orig_name'),
'composition' => $product->getData('composition'),
'maintenance' => $product->getData('maintenance'),
'size_chart' => $product->getData('size_chart'),
'config_attributes' => $productData->additional_attributes['config_attributes']
);
$productData->media = array('image' => $product->getData('image'));
$productData->categories = $product->getCategoryIds();

Mage::dispatchEvent('catalog_controller_product_delete', array('product' => $product));
$product->delete();
}
}
}

return parent::create($type, $set, $sku, $productData, $store);
}

protected function _prepareDataForSave($product, $productData)
{
/* @var $product Mage_Catalog_Model_Product */

$configAttributes = array();

if (property_exists($productData, 'categories')) {
$categoryIds = Mage::helper('jr_api/catalog_product')
->getCategoryIdsByNames((array) $productData->categories);
Expand All @@ -24,9 +59,24 @@ protected function _prepareDataForSave($product, $productData)
}
}


if (property_exists($productData, 'media')) {
$path = Mage::getBaseDir().'/media/catalog/product';
if(file_exists($path.$productData->media['image'])) {
$product->addImageToMediaGallery($path.$productData->media['image'], array('image','small_image','thumbnail'), true, false);
}
}


if (property_exists($productData, 'additional_attributes')) {
$singleDataExists = property_exists((object) $productData->additional_attributes, 'single_data');
$multiDataExists = property_exists((object) $productData->additional_attributes, 'multi_data');

$configAttributesExists = property_exists((object) $productData->additional_attributes, 'config_attributes');
if ($configAttributesExists) {
$configAttributes = explode(',', $productData->additional_attributes['config_attributes']);
}

if ($singleDataExists || $multiDataExists) {
if ($singleDataExists) {
foreach ($productData->additional_attributes->single_data as $_attribute) {
Expand Down Expand Up @@ -77,7 +127,7 @@ protected function _prepareDataForSave($product, $productData)
$priceChanges = $productData->price_changes;
}
}
Mage::helper('jr_api/catalog_product')->associateProducts($product, $simpleSkus, $priceChanges);
Mage::helper('jr_api/catalog_product')->associateProducts($product, $simpleSkus, $priceChanges, $configAttributes);
}
}
}
1 change: 1 addition & 0 deletions app/code/community/JR/Api/etc/wsdl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<complexType name="catalogProductCreateEntity">
<all>
<element name="associated_skus" type="typens:ArrayOfString" minOccurs="0"/>
<element name="config_attributes" type="typens:ArrayOfString" minOccurs="0"/>
<element name="price_changes" type="typens:associativeArray" minOccurs="0" />
</all>
</complexType>
Expand Down
1 change: 1 addition & 0 deletions app/code/community/JR/Api/etc/wsi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<xsd:complexType name="catalogProductCreateEntity">
<xsd:sequence>
<xsd:element name="associated_skus" type="typens:ArrayOfString" minOccurs="0" />
<xsd:element name="config_attributes" type="typens:ArrayOfString" minOccurs="0" />
<xsd:element name="price_changes" type="typens:complexMultiArray" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
Expand Down