Skip to content

Commit

Permalink
allow to remove attribute during update product.
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Mar 24, 2017
1 parent 03319ec commit 1e202d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
12 changes: 12 additions & 0 deletions modules/base/Mage2/Catalog/Helpers/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -167,6 +173,12 @@ public function saveProductAttribute($product, ProductRequest $request)
}
}

foreach(array_flip($existingIds) as $id) {
ProductVariation::destroy($id);
}



}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
</div>

<div class="panel-body">


<?php
$attributes = $product->getAssignedAttributes();
//todo How does multiple Variation works
//$firstVariation = $product->productVariations()->get()->first();
//$attribute = $firstVariation->productAttribute;
?>
@foreach($attributes as $attribute)
<div class="panel panel-default ">
<button type="button" class="remove-attribute close" data-dismiss="alert" aria-label="Close">
&times;
Expand All @@ -14,21 +26,7 @@
</div>
<div class="panel-body">
@if(isset($product) && $product->has_variation == 1)


<?php
$attributes = $product->getAssignedAttributes();
//todo How does multiple Variation works
//$firstVariation = $product->productVariations()->get()->first();
//$attribute = $firstVariation->productAttribute;
?>

@foreach($attributes as $attribute)
<div class="col-md-12">

<label>Please Select Option</label>
<span class="input-group">

Expand Down Expand Up @@ -61,12 +59,16 @@
aria-labelledby="headingOne">
<div class="panel-body">


@foreach($product->getAssignedVariationBytAttributeId($attribute->product_attribute_id) as $variation)
<?php
$subProduct = $variation->subProduct;
?>
<div class="col-md-12 single-option-box"
style="border: 1px solid #ccc; padding: 10px;margin-bottom: 10px">
<button type="button" class="remove-variation-attribute close" data-dismiss="alert" aria-label="Close">
&times;
</button>

<label>{{ $subProduct ->title }}</label>

Expand Down Expand Up @@ -114,18 +116,19 @@ class="form-control"/>
<hr/>
@endforeach


</div>
</div>
</div>

</div>

@endforeach

</div>
</div>
@endif

@endforeach


<div class="col-md-12">
<a class="add-another-panel-button" href="#">Add Another</a>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -227,7 +217,7 @@ class="form-control"/>
})
</script>
<style>
.remove-attribute {
.remove-attribute, .remove-variation-attribute {
font-size:42px;
margin-right: 10px;
}
Expand Down

0 comments on commit 1e202d2

Please sign in to comment.