Skip to content

Commit

Permalink
Rapidez v2 compatibility (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Feb 21, 2024
1 parent 81e9492 commit 1bb15c1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"require": {
"php": "^8.0|^8.1",
"rapidez/core": "~0.57|^1.0",
"rapidez/core": "^2.0",
"blade-ui-kit/blade-heroicons": "^2.0"
},
"autoload": {
Expand Down
93 changes: 45 additions & 48 deletions resources/js/components/amastybundles.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script>
import GetCart from 'Vendor/rapidez/core/resources/js/components/Cart/mixins/GetCart.js'
export default {
mixins: [GetCart],
import { mask, refreshMask } from 'Vendor/rapidez/core/resources/js/stores/useMask'
export default {
props: {
mainProduct: Object,
bundle: Object,
Expand Down Expand Up @@ -59,45 +58,52 @@
this.added = false
this.adding = true
await this.getMask()
if (!mask.value) {
await refreshMask()
}
let productsToAdd = Object.entries(this.selectedProducts).filter(([itemIndex, itemChecked]) => itemChecked);
try {
Object.entries(this.selectedProducts).forEach(async ([itemIndex, itemChecked]) => {
if (itemChecked) {
let product = this.bundle.items[itemIndex]
await this.magentoCart('post', 'items', {
cartItem: {
sku: product.product.sku,
qty: 1,
quote_id: localStorage.mask
}
})
}
productsToAdd = productsToAdd.map(([itemIndex, itemChecked]) => {
let product = this.bundle.items[itemIndex]
return {
sku: product.product.sku,
quantity: 1,
};
})
let response = await this.magentoCart('post', 'items', {
cartItem: {
sku: this.mainProduct.sku,
qty: 1,
quote_id: localStorage.mask,
product_option: this.productOptions
}
productsToAdd.push({
sku: this.mainProduct.sku,
quantity: 1,
selected_options: this.selectedOptions
});
const response = await window.magentoGraphQL(
`mutation (
$cartId: String!,
$cartItems: [CartItemInput!]!
) { addProductsToCart(cartId: $cartId, cartItems: $cartItems) { cart { ` +
config.queries.cart +
` } user_errors { code message } } }`,
{
cartId: mask.value,
cartItems: productsToAdd,
},
).then(async (response) => {
await this.updateCart({}, response)
return response;
})
// Just a workaround to make sure all calculations are triggered.
await this.magentoCart('put', 'items/' + response.data.item_id, {
cartItem: {
quote_id: localStorage.mask,
qty: response.data.qty
}
})
await this.refreshCart()
if (response.data.addProductsToCart.user_errors.length) {
throw new Error(response.data.addProductsToCart.user_errors[0].message)
}
this.added = true
setTimeout(() => { this.added = false }, this.addedDuration)
} catch (error) {
Notify(error.response.data.message, 'error')
error?.response && (await this.checkResponseForExpiredCart(error.response))
Notify(error?.response?.data?.message || error.message, 'error')
}
this.adding = false
Expand All @@ -120,6 +126,10 @@
},
itemPrice(index) {
if (!index) {
return
}
return this.bundle.items[index].product.price_range.maximum_price.regular_price.value
}
},
Expand Down Expand Up @@ -183,21 +193,8 @@
return this.oldBundlePrice > this.bundlePrice ? this.oldBundlePrice - this.bundlePrice : false
},
productOptions: function () {
let options = []
Object.entries(this.options).forEach(([key, val]) => {
options.push({
option_id: key,
option_value: val,
})
})
return {
extension_attributes: {
configurable_item_options: options
}
}
selectedOptions: function () {
return Object.entries(this.options).map(([optionId, optionValue]) => btoa('custom-option/' + optionId + '/' + optionValue))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/productbundles.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class="object-contain h-40 w-full"
@lang('You\'re saving')
@{{ bundleDiscountAmount | price }}
</div>
<x-rapidez::button type="submit" class="flex items-center mx-auto mt-3">
<x-rapidez::button type="submit" class="flex items-center mx-auto mt-3" v-bind:disabled="adding">
<x-heroicon-o-shopping-cart class="h-5 w-5 mr-2" v-if="!adding && !added" />
<x-heroicon-o-arrow-path class="h-5 w-5 mr-2 animate-spin" v-if="adding" />
<x-heroicon-o-check class="h-5 w-5 mr-2" v-if="added" />
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Scopes/RelatedProductsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public function apply(Builder $builder, Model $model)
'amasty_related_ids' => DB::table('amasty_mostviewed_product_index AS mainrule')
->selectRaw('GROUP_CONCAT(IF(amasty_mostviewed_group.show_out_of_stock = 0 AND related_stock.is_in_stock = 0, NULL, related.entity_id))')
->leftJoin('amasty_mostviewed_group', 'mainrule.rule_id', '=', 'amasty_mostviewed_group.group_id')
->leftJoin('amasty_mostviewed_product_index as related', function ($join) {
->leftJoin('amasty_mostviewed_product_index AS related', function ($join) {
$join->on('related.rule_id', '=', 'mainrule.rule_id')
->where('related.relation', 'what_show')
->whereIn('related.position', ['product_into_related', 'product_into_upsell'])
->where('related.store_id', config('rapidez.store'));
})
->leftJoin('cataloginventory_stock_item as related_stock', 'related_stock.product_id', '=', 'related.entity_id')
->leftJoin('cataloginventory_stock_item AS related_stock', 'related_stock.product_id', '=', 'related.entity_id')
->whereColumn('mainrule.entity_id', $builder->getQuery()->from.'.entity_id')
->where('mainrule.relation', 'where_show')
->whereIn('mainrule.position', ['product_into_related', 'product_into_upsell'])
Expand Down

0 comments on commit 1bb15c1

Please sign in to comment.