From 7e75da7bdf5cda11922a718bfddf97e09bf05009 Mon Sep 17 00:00:00 2001 From: prashant-webkul Date: Fri, 26 Oct 2018 09:49:19 +0530 Subject: [PATCH] Product datagrid event binded with product create --- .../Webkul/Admin/src/Listeners/Product.php | 54 +++++++++---------- .../ProductAttributeValueRepository.php | 6 +-- .../src/Repositories/ProductRepository.php | 22 ++++---- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/packages/Webkul/Admin/src/Listeners/Product.php b/packages/Webkul/Admin/src/Listeners/Product.php index 04be65a8b..97a7d0776 100644 --- a/packages/Webkul/Admin/src/Listeners/Product.php +++ b/packages/Webkul/Admin/src/Listeners/Product.php @@ -94,12 +94,6 @@ class Product { $qty = 0; } } - - return [ - 'parent' => $gridObject, - 'variants' => $variantObjects - ]; - } else { $qty = 0; @@ -111,13 +105,13 @@ class Product { $gridObject['quantity'] = $qty; $qty = 0; - - return [ - 'parent' => $gridObject, - 'variants' => $variantObjects - ]; } } + // dd($gridObject, $variantObjects); + return [ + 'parent' => $gridObject, + 'variants' => $variantObjects + ]; } /** @@ -139,31 +133,37 @@ class Product { * @return boolean */ public function saveProduct($product, $data) { - die; - - if ($this->productGrid->findOneByField('product_id', $product->id)) { - if($product->type == 'configurable') { + $productGridObject = $this->productGrid->findOneByField('product_id', $product->id); + // dd($product, $data, $productGridObject); + if (!is_null($productGridObject)) { + if($product->type == 'simple') { + $r = $productGridObject->update($data['parent']); } else { - if($this->productGrid->update($product, $id)) { - return true; - } else { - return false; + $productGridObject->update($data['parent']); + if(count($data['variants'])) { + dd($data['variants']); + foreach($data['variants'] as $variant) { + $variantObject = $this->productGrid->findOneByField('product_id', $variant['product_id']); + if(!is_null($variantObject)) { + $variantObject->update($variant); + } else { + $this->productGrid->create($variant); + } + } } } } else { - if($product->type == 'configurable') { + $this->productGrid->create($data['parent']); - } else { - if($this->productGrid->create($product)) { - return true; - } else { - return false; + //no need for tese lines + if(count($data['variants'])) { + foreach($data['variants'] as $variant) { + $this->productGrid->create($variant); } } } - - + return true; } /** diff --git a/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php b/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php index ca7aaf449..847571422 100644 --- a/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductAttributeValueRepository.php @@ -1,7 +1,7 @@ -update($data); - if(isset($data['categories'])) + if(isset($data['categories'])) { $product->categories()->sync($data['categories']); + } $attributes = $product->attribute_family->custom_attributes; foreach ($attributes as $attribute) { if(!isset($data[$attribute->code]) || ($attribute->code == 'boolean' && !$data[$attribute->code])) continue; - + $attributeValue = $this->attributeValue->findOneWhere([ 'product_id' => $product->id, 'attribute_id' => $attribute->id, @@ -172,16 +173,17 @@ class ProductRepository extends Repository if(!$attributeValue) { $this->attributeValue->create([ - 'product_id' => $product->id, - 'attribute_id' => $attribute->id, - 'value' => $data[$attribute->code], - 'channel' => $attribute->value_per_channel ? $data['channel'] : null, - 'locale' => $attribute->value_per_locale ? $data['locale'] : null - ]); + 'product_id' => $product->id, + 'attribute_id' => $attribute->id, + 'value' => $data[$attribute->code], + 'channel' => $attribute->value_per_channel ? $data['channel'] : null, + 'locale' => $attribute->value_per_locale ? $data['locale'] : null + ]); } else { $this->attributeValue->update([ - ProductAttributeValue::$attributeTypeFields[$attribute->type] => $data[$attribute->code] - ], $attributeValue->id); + ProductAttributeValue::$attributeTypeFields[$attribute->type] => $data[$attribute->code] + ], $attributeValue->id + ); } }