Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rapidez/amasty-automatic-related-…
Browse files Browse the repository at this point in the history
…products
  • Loading branch information
royduin committed Dec 9, 2021
2 parents 95935f1 + bfe4808 commit 3bdf0f6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
73 changes: 48 additions & 25 deletions resources/js/components/amastybundles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
render() {
return this.$scopedSlots.default({
bundlePrice: this.bundlePrice,
oldBundlePrice: this.oldBundlePrice,
bundleDiscountAmount: this.bundleDiscountAmount,
selectedProducts: this.selectedProducts,
addToCart: this.addToCart,
options: this.options,
adding: this.adding,
added: this.added,
itemDiscount: this.itemDiscount,
itemDiscountedPrice: this.itemDiscountedPrice,
itemPrice: this.itemPrice,
mainProductPrice: this.mainProductPrice,
mainProductDiscount: this.mainProductDiscount,
mainProductDiscountedPrice: this.mainProductDiscountedPrice
});
},
Expand Down Expand Up @@ -99,22 +104,49 @@
},
discountMultiplier(percentage) {
return (1 - (percentage / 100))
return (1 - (percentage / 100).toFixed(2))
},
itemDiscount(index) {
if (!this.bundle || this.bundle.items[index].discount_amount <= 0) {
return false
}
return this.bundle.discount_type ? (this.itemPrice(index) / 100 * this.bundle.items[index].discount_amount).toFixed(2) : this.bundle.items[index].discount_amount
},
itemDiscountedPrice(index) {
return this.itemDiscount(index) ? this.itemPrice(index) - this.itemDiscount(index) : false
},
itemPrice(index) {
return this.bundle.items[index].product.price_range.maximum_price.regular_price.value
}
},
computed: {
mainProductPrice: function () {
return parseFloat(this.mainProduct.price_range.maximum_price.regular_price.value)
return this.mainProduct.price_range.maximum_price.regular_price.value
},
mainProductDiscount: function () {
if (!this.bundle.apply_for_parent) {
return false
}
return this.bundle.discount_type ? (this.mainProductPrice / 100 * this.bundle.discount_amount).toFixed(2) : this.bundle.discount_amount
},
mainProductDiscountedPrice: function () {
return this.mainProductDiscount ? this.mainProductPrice - this.mainProductDiscount : false
},
bundlePrice: function() {
let price = 0
// This options is not available through GraphQL,
// until it's implemented by Amasty we use 1.
let apply_condition = 1
let conditionsAreNotMet = apply_condition
&& this.selectedProducts.length != Object.values(this.selectedProducts).filter(Boolean).length
let conditionsAreNotMet = apply_condition && this.selectedProducts.length < 1
if (!Object.values(this.selectedProducts).filter(Boolean).length) {
return this.mainProductPrice
Expand All @@ -123,41 +155,32 @@
if (!this.bundle.apply_for_parent || conditionsAreNotMet) {
price += this.mainProductPrice
} else {
price += this.bundle.discount_type
? this.mainProductPrice * this.discountMultiplier(this.bundle.discount_amount)
: this.mainProductPrice - this.bundle.discount_amount
price += this.mainProductDiscountedPrice
}
Object.entries(this.selectedProducts).forEach(([itemKey, itemSelected]) => {
if (itemSelected) {
let productPrice = this.bundle.items[itemKey].product.price_range.maximum_price.regular_price.value
let itemDiscount = this.bundle.items[itemKey].discount_amount ?? this.bundle.discount_amount
if (conditionsAreNotMet) {
price += productPrice
} else {
price += this.bundle.discount_type
? productPrice * this.discountMultiplier(itemDiscount)
: productPrice - itemDiscount
}
price += conditionsAreNotMet ? this.itemPrice(itemKey) : this.itemDiscountedPrice(itemKey)
}
})
return price
},
bundleDiscountAmount: function() {
let productPricesSummed = this.mainProductPrice
oldBundlePrice: function() {
let price = this.mainProductPrice
Object.entries(this.selectedProducts).forEach(([itemKey, itemSelected]) => {
if (itemSelected) {
let productPrice = this.bundle.items[itemKey].product.price_range.maximum_price.regular_price.value
productPricesSummed += productPrice
price += this.itemPrice(itemKey)
}
})
return Math.abs(this.bundlePrice - productPricesSummed)
return price
},
bundleDiscountAmount: function() {
return this.oldBundlePrice > this.bundlePrice ? this.oldBundlePrice - this.bundlePrice : false
},
productOptions: function () {
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/productbundle-item.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@props(['name', 'price', 'firstProduct' => false])
@props(['name', 'firstProduct' => false])

<div class="contents" {{ $attributes->only('v-for') }}>
@if(!$firstProduct)
Expand All @@ -17,7 +17,7 @@
{{ $image }}
</div>
<div class="text-center my-2">{{ $name }}</div>
<div class="font-bold text-center">{{ $price }}</div>
<div class="font-bold text-center">@{{ itemPrice(index) }}</div>

{{ $slot }}
</div>
Expand Down
4 changes: 1 addition & 3 deletions resources/views/components/productbundles.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
<div slot-scope="{ data }" v-if="data">
<div v-for="bundle in data.amMostviewedBundlePacks.items">
<amastybundles :main-product="data.amMostviewedBundlePacks.main_product" :bundle="bundle">
<div class="mb-3" slot-scope="{ bundlePrice, bundleDiscountAmount, selectedProducts, addToCart, options, adding, added }">
<div class="mb-3" slot-scope="{ bundlePrice, bundleDiscountAmount, selectedProducts, addToCart, options, adding, added, itemPrice }">
<div class="mb-3 font-bold text-lg">@{{ bundle.block_title }}</div>
<form class="flex flex-col sm:flex-row" v-on:submit.prevent="addToCart">
<x-amastyrelatedproducts::productbundle-item
:name="'@{{ data.amMostviewedBundlePacks.main_product.name }}'"
:price="'@{{ data.amMostviewedBundlePacks.main_product.price_range.maximum_price.regular_price.value | price }}'"
:firstProduct="true"
>
<x-slot name="image">
Expand Down Expand Up @@ -46,7 +45,6 @@ class="block mx-auto mb-3"

<x-amastyrelatedproducts::productbundle-item
:name="'@{{ item.product.name }}'"
:price="'@{{ item.product.price_range.maximum_price.regular_price.value | price }}'"
v-for="(item, index) in bundle.items"
:firstProduct="false"
>
Expand Down

0 comments on commit 3bdf0f6

Please sign in to comment.