From 1e202d28e3ecb9551a94a5fc016675c33d0c2650 Mon Sep 17 00:00:00 2001 From: Purvesh Date: Fri, 24 Mar 2017 13:38:16 +1300 Subject: [PATCH] allow to remove attribute during update product. --- .../Mage2/Catalog/Helpers/ProductHelper.php | 12 ++++ .../admin/catalog/product/attribute.blade.php | 60 ++++++++----------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/modules/base/Mage2/Catalog/Helpers/ProductHelper.php b/modules/base/Mage2/Catalog/Helpers/ProductHelper.php index aee10f5f2..51a5512ed 100644 --- a/modules/base/Mage2/Catalog/Helpers/ProductHelper.php +++ b/modules/base/Mage2/Catalog/Helpers/ProductHelper.php @@ -109,11 +109,17 @@ public function saveProductAttribute($product, ProductRequest $request) //@todo update image to hasvariation = true $product->update(['has_variation' => 1]); + + $existingIds = array_flip( $product->productVariations->pluck('id')->toArray()); + foreach ($attributes as $attributeId => $attribute) { foreach ($attribute as $dropdownId => $fieldValue) { if (isset($fieldValue['id']) && $fieldValue['id'] > 0) { + + unset($existingIds [$fieldValue['id']]); + $variation = ProductVariation::findorfail($fieldValue['id']); $subProduct = $variation->subProduct; $subProduct->update($fieldValue); @@ -167,6 +173,12 @@ public function saveProductAttribute($product, ProductRequest $request) } } + foreach(array_flip($existingIds) as $id) { + ProductVariation::destroy($id); + } + + + } diff --git a/modules/base/Mage2/Catalog/views/admin/catalog/product/attribute.blade.php b/modules/base/Mage2/Catalog/views/admin/catalog/product/attribute.blade.php index b043066eb..b918c17be 100644 --- a/modules/base/Mage2/Catalog/views/admin/catalog/product/attribute.blade.php +++ b/modules/base/Mage2/Catalog/views/admin/catalog/product/attribute.blade.php @@ -5,6 +5,18 @@
+ + + getAssignedAttributes(); + + //todo How does multiple Variation works + //$firstVariation = $product->productVariations()->get()->first(); + //$attribute = $firstVariation->productAttribute; + + ?> + @foreach($attributes as $attribute)
@if(isset($product) && $product->has_variation == 1) - - - getAssignedAttributes(); - - //todo How does multiple Variation works - //$firstVariation = $product->productVariations()->get()->first(); - //$attribute = $firstVariation->productAttribute; - - ?> - - @foreach($attributes as $attribute)
- @@ -61,12 +59,16 @@ aria-labelledby="headingOne">
+ @foreach($product->getAssignedVariationBytAttributeId($attribute->product_attribute_id) as $variation) subProduct; ?>
+ @@ -114,18 +116,19 @@ class="form-control"/>
@endforeach -
- @endforeach +
@endif + @endforeach +
Add Another @@ -169,34 +172,21 @@ class="form-control"/> jQuery(document).ready(function () { - jQuery(document).on('click', '.add-another-panel-button', function (e) { e.preventDefault(); - var html = jQuery('.product-attribute-main-panel-template').html(); - jQuery(e.target).parent().before(html); - - //jQuery('.select2').select2(); - - //if(jQuery('.remove-attribute').length > 1) { - // jQuery(e.target).parent(); - //} else { - // alert('Sorry not allowed'); - //} }); jQuery(document).on('click', '.remove-attribute', function (e) { + jQuery(e.target).parent().remove(); + }); - console.info(jQuery('.remove-attribute').length); - - if(jQuery('.remove-attribute').length > 2) { - jQuery(e.target).parent().remove(); - } else { - alert('Sorry not allowed'); - } + jQuery(document).on('click', '.remove-variation-attribute', function (e) { + jQuery(e.target).parent().remove(); }); + jQuery(document).on('click', '.add-product-attribute', function (e) { e.preventDefault(); @@ -227,7 +217,7 @@ class="form-control"/> })