Skip to content

Commit 14034b0

Browse files
authored
Merge pull request #252 from w3bdesign/dev
Support for variable products
2 parents 23e25cd + 8c39d92 commit 14034b0

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The current release has been tested and is confirmed working with the following
7272
- Vue Apollo with GraphQL Codegen
7373
- Vue Awesome Swiper for image slider on frontpage
7474
- Responsive design
75-
- Support for simple products
75+
- Support for simple and variable products
7676
- GraphQL-based filters
7777
- CSS animations and transitions
7878
- Form handling and validation with Vue Formulate
@@ -91,6 +91,5 @@ Check the attributes of the products. Right now the application requires Size an
9191

9292
## TODO
9393

94-
- Look into variable product support
9594
- Make WooCommerce session token expire and get deleted after 24 hours
9695
- Make Algolia look good on mobile

components/Cart/AddToCartButton.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import ADD_TO_CART_MUTATION from '@/apollo/mutations/ADD_TO_CART_MUTATION'
3131
3232
export default {
3333
props: {
34-
product: { type: Object, required: true },
34+
product: { type: [Number, Object], required: true },
3535
},
3636
data() {
3737
return {
@@ -52,7 +52,7 @@ export default {
5252
mutation: ADD_TO_CART_MUTATION,
5353
variables: { input: productQueryInput },
5454
})
55-
.then(({ data }) => {
55+
.then(() => {
5656
this.loading = false
5757
this.$apollo.queries.cart.refetch()
5858
})

components/Products/ShowAllProducts.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{{ product.regularPrice }}
3333
</div>
3434
<div class="ml-4 text-xl text-gray-900">
35-
{{ product.salePrice }}
35+
{{ filteredVariantPrice(product.salePrice) }}
3636
</div>
3737
</div>
3838
<div v-else>
@@ -48,17 +48,21 @@
4848
</template>
4949

5050
<script>
51+
import { filteredVariantPrice } from '@/utils/functions'
52+
5153
export default {
5254
name: 'ShowAllProducts',
5355
props: {
5456
products: { type: Array, required: true },
5557
},
58+
5659
methods: {
5760
productImage(product) {
5861
return product.image
5962
? product.image.sourceUrl
6063
: process.env.placeholderSmallImage
6164
},
65+
filteredVariantPrice,
6266
},
6367
}
6468
</script>

components/Products/ShowSingleProduct.vue

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
:alt="product.name"
1313
:src="product.image.sourceUrl"
1414
/>
15-
1615
<img
1716
v-else
1817
id="product-image"
@@ -24,7 +23,7 @@
2423
<p class="text-3xl font-bold text-left">{{ product.name }}</p>
2524
<div v-if="product.onSale" class="flex">
2625
<p class="pt-1 mt-4 text-3xl text-gray-900">
27-
{{ product.salePrice }}
26+
{{ filteredVariantPrice(product.salePrice) }}
2827
</p>
2928
<p class="pt-1 pl-8 mt-4 text-2xl text-gray-900 line-through">
3029
{{ product.regularPrice }}
@@ -44,16 +43,24 @@
4443
<span class="py-2">Varianter</span>
4544
<select
4645
id="variant"
46+
v-model="variationProduct"
4747
name="variant"
4848
class="block w-64 px-6 py-2 bg-white border border-gray-500 rounded-lg focus:outline-none focus:shadow-outline"
4949
>
50-
VARIATION
50+
<option
51+
v-for="(variation, index) in product.variations.nodes"
52+
:key="variation.databaseId"
53+
:value="variation.databaseId"
54+
:selected="index === 0"
55+
>
56+
{{ variation.name }}
57+
</option>
5158
</select>
5259
</p>
5360
<div class="pt-1 mt-2">
5461
<CartAddToCartButton
5562
v-if="product.variations"
56-
:product="product"
63+
:product="variationProduct"
5764
/>
5865
<CartAddToCartButton v-else :product="product" />
5966
</div>
@@ -65,13 +72,23 @@
6572
</template>
6673

6774
<script>
68-
import { stripHTML } from '@/utils/functions'
75+
import { stripHTML, filteredVariantPrice } from '@/utils/functions'
6976
7077
export default {
7178
name: 'ShowSingleProduct',
7279
props: {
7380
product: { type: Object, required: true },
7481
},
75-
methods: { stripHTML },
82+
data() {
83+
return {
84+
// Set default value
85+
variationProduct: 18,
86+
}
87+
},
88+
89+
methods: {
90+
stripHTML,
91+
filteredVariantPrice,
92+
},
7693
}
7794
</script>

utils/functions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ function stripHTML(description) {
44
return description.replace(/(<([^>]+)>)/gi, '')
55
}
66

7+
function filteredVariantPrice(price) {
8+
// Filter price from "kr198.00 - kr299.00" to kr299.00
9+
return price.substring(price.length, price.indexOf('-')).replace('-', '')
10+
}
11+
712
function createCheckoutData(form) {
813
const checkoutData = {
914
clientMutationId: uid(),
@@ -42,4 +47,4 @@ function createCheckoutData(form) {
4247
return { checkoutData }
4348
}
4449

45-
export { stripHTML, createCheckoutData }
50+
export { stripHTML, createCheckoutData, filteredVariantPrice }

0 commit comments

Comments
 (0)