Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Get option labels with attribute options in the cart #726

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions resources/js/stores/useAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export const attributes = computedAsync(
{ lazy: true, shallow: false },
)

window.attributeLabel = (attributeCode) => {
return Object.values(attributes.value)?.find((attribute) => attribute.code === attributeCode)?.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to return a computed value of this expression.
attributes is a lazy computed async. It will only be fetched when it's used and returns a default value in the meantime.

calling attributeLabel might give us undefined back while the request is still going and it will not be able to update.
if we wrap this in a computed function it has the chance to rerender with the updated attributes

}

export default () => attributes
21 changes: 11 additions & 10 deletions resources/js/stores/useCart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StorageSerializers, asyncComputed, useLocalStorage, useMemoize } from '@vueuse/core'
import { computed, watch } from 'vue'
import { computed } from 'vue'
import { GraphQLError } from '../fetch'
import { mask, clearMask } from './useMask'

Expand Down Expand Up @@ -59,16 +59,17 @@ export const fetchCustomerCart = async function () {

export const fetchAttributeValues = async function (attributes = []) {
if (!attributes.length) {
return { data: { customAttributeMetadata: { items: null } } }
return { data: { customAttributeMetadataV2: { items: null } } }
}

return await window.magentoGraphQL(
`
query attributeValues($attributes: [AttributeInput!]!) {
customAttributeMetadata(attributes: $attributes) {
customAttributeMetadataV2(attributes: $attributes) {
items {
attribute_code
attribute_options {
label
code
options {
label
value
}
Expand Down Expand Up @@ -121,7 +122,7 @@ export const cart = computed({
set(value) {
getAttributeValues()
.then((response) => {
if (!response?.data?.customAttributeMetadata?.items) {
if (!response?.data?.customAttributeMetadataV2?.items) {
value.items = value.items?.map((item) => {
item.is_available = checkAvailability(item)

Expand All @@ -132,10 +133,9 @@ export const cart = computed({
}

const mapping = Object.fromEntries(
response.data.customAttributeMetadata.items.map((attribute) => [
attribute.attribute_code,
Object.fromEntries(attribute.attribute_options.map((value) => [value.value, value.label])),
]),
response.data.customAttributeMetadataV2.items.map((attribute) => {
return [attribute.code, Object.fromEntries(attribute.options.map((value) => [value.value, value.label]))]
}),
)

value.items = value.items?.map((cartItem) => {
Expand All @@ -144,6 +144,7 @@ export const cart = computed({

for (const key in mapping) {
cartItem.product.attribute_values[key] = cartItem.product[key]

if (cartItem.product.attribute_values[key] === null) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions resources/views/cart/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class="mx-auto"
@{{ option.label }}: @{{ option.values[0].label || option.values[0].value }}
</div>
<div v-for="option in config.cart_attributes">
<template v-if="item.product.attribute_values?.[option] && typeof item.product.attribute_values[option] === 'object'">
@{{ option }}: <span v-html="item.product.attribute_values[option]?.join(', ')"></span>
<template v-if="item.product.attribute_values[option] && typeof item.product.attribute_values[option] === 'object'">
@{{ window.attributeLabel(option) }}: <span v-html="item.product.attribute_values[option].join(', ')"></span>
</template>
</div>
@include('rapidez::cart.item.remove')
Expand Down