/
ProductPage.cshtml
198 lines (175 loc) · 8.44 KB
/
ProductPage.cshtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
@using Vendr.Core.Models;
@using Vendr.DemoStore;
@using Vendr.DemoStore.Models;
@using Vendr.Extensions;
@using Umbraco.Cms.Core.Models.PublishedContent;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ProductPage>
@{
Layout = "Page.cshtml";
var multiVariantProductPage = Model as MultiVariantProductPage;
var store = Model.Value<StoreReadOnly>("store", fallback: Fallback.ToAncestors);
var addedProductReference = TempData["addedProductReference"]?.ToString();
}
@section foot {
@if (!addedProductReference.IsNullOrWhiteSpace())
{
<script>
document.addEventListener('DOMContentLoaded', function () {
Toastify({
text: "Item successfully added to cart",
duration: 3000,
gravity: "bottom", // `top` or `bottom`
position: 'center', // `left`, `center` or `right`
backgroundColor: "#4fd1c5",
className: "",
stopOnFocus: true, // Prevents dismissing of toast on hover
}).showToast();
});
</script>
}
@if (multiVariantProductPage != null && multiVariantProductPage.Variants != null && multiVariantProductPage.Variants.Count > 0)
{
<script>
// Create JS function to update variant info dynamically
var updateProductVariantInfo = function () {
// Convert selected values into a key value collection
var attributeValues = Array.from($("select[data-attribute]")).reduce(function (accumulator, currentValue) {
accumulator[currentValue.dataset.attribute] = currentValue.value;
return accumulator;
}, { });
// Construct the post data to pass to the API controller
var postData = {
productNodeId: @Model.Id,
attributes: attributeValues
}
// Disable the add to cart button while loading
$("#add-to-cart").prop('disabled', true).css({ opacity: 0.2 });
// Fetch the variant data for the given attribute combination and update the UI accordingly
$.ajax({
url: "/umbraco/api/productapi/getproductvariant",
type: "POST",
data: JSON.stringify(postData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data) {
$("[data-variant-price]").text(data.priceFormatted);
$("[name=productVariantReference]").val(data.productVariantReference);
$(".product-image").attr("src", data.imageUrl);
$("#add-to-cart").prop('disabled', false).css({ opacity: 1 });
} else {
$("[data-variant-price]").text("Unavailable");
$("[name=productVariantReference]").val("");
$(".product-image").attr("src", $(".product-image").data("defaultUrl"));
}
}
});
return false;
}
// Update variant info on load
updateProductVariantInfo();
</script>
}
}
<div class="pt-6">
<div class="md:flex mb-4">
<div class="w-full md:w-1/2 md:mr-6">
<img src="@(Model.PrimaryImage != null ? Model.PrimaryImage.GetCropUrl(500, 500).ToString() : "https://placehold.it/400x500")"
data-default-url="@(Model.PrimaryImage != null ? Model.PrimaryImage.GetCropUrl(500, 500).ToString() : "https://placehold.it/400x500")"
class="border w-full product-image" />
@if (Model.Images.Count() > 1)
{
<div class="flex flex-wrap -mx-2">
@foreach (var img in Model.Images)
{
<a href="@(img.GetCropUrl(500, 500).ToString())" class="product-image__thumb w-1/3 px-2 pt-4">
<img src="@img.GetCropUrl(150, 150).ToString()" class="border w-full" />
</a>
}
</div>
}
<div class="text-center text-gray-500 mt-4 rte trim">
@Model.Collection.ImagesCredit
</div>
</div>
<div class="flex-1">
@using (Html.BeginUmbracoForm("AddToCart", "CartSurface"))
{
<div class="py-10">
<h1 class="text-2xl font-bold">@Model.Parent.Name - @Model.Name</h1>
@{
RenderVariants(store, Model, addedProductReference);
}
</div>
<button id="add-to-cart" class="font-bold border-4 border-gray-900 px-6 py-3 mb-4 lg:w-1/2 hover:bg-teal-400 hover:border-teal-400 hover:text-white" type="submit">Add to Cart</button>
}
<div class="flex">
<a href="#" class="mr-4 hover:text-teal-400"><i class="fa fa-facebook-square"></i> Share it</a>
<a href="#" class="mr-4 hover:text-teal-400"><i class="fa fa-twitter"></i> Tweet it</a>
<a href="#" class="mr-4 hover:text-teal-400"><i class="fa fa-pinterest"></i> Pin it</a>
</div>
</div>
</div>
<hr class="my-6" />
<div>
<h2 class="text-2xl font-bold">Description</h2>
<div class="rte">
@Model.LongDescription
</div>
</div>
</div>
@{
void RenderVariants(StoreReadOnly store, ProductPage product, string addedProductReference)
{
var multiVariantProductPage = Model as MultiVariantProductPage;
if (multiVariantProductPage != null && multiVariantProductPage.Variants != null && multiVariantProductPage.Variants.Count > 0)
{
@Html.Hidden("productReference", Model.GetProductReference())
@Html.Hidden("productVariantReference", "")
var attrs = multiVariantProductPage.Variants.GetInUseProductAttributes();
foreach (var attr in attrs)
{
<p class="mt-4">@(attr.Name)</p>
<select class="w-full border border-gray-300 p-2 mt-1" data-attribute="@(attr.Alias)" data-attribute-name="@(attr.Name)" onchange="updateProductVariantInfo()">
@foreach (var val in attr.Values)
{
<!option value="@val.Alias">@(val.Name)</!option>
}
</select>
}
<p class="mt-4" data-variant-price></p>
}
else if (product.ChildVariants.Any())
{
if (product.IsGiftCard)
{
var selectedReference = addedProductReference ?? product.ChildVariants.FirstOrDefault()?.GetProductReference();
<div class="gift-cards flex justify-start space-x-2 mt-4">
@foreach (var variant in product.ChildVariants)
{
var productReference = variant.GetProductReference();
<span class="relative">
<input type="radio" class="form-radio absolute w-0 h-0" name="productReference" id="giftcard-@productReference" value="@productReference" @Html.Raw(selectedReference == productReference ? "checked=\"checked\"" : "") />
<label class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-semibold py-2 px-4 cursor-pointer" for="giftcard-@productReference">@(variant.CalculatePrice()?.Formatted())</label>
</span>
}
</div>
}
else
{
<select name="productReference" class="w-full border border-gray-300 p-2 mt-1">
@foreach (var variant in product.ChildVariants)
{
var productReference = variant.GetProductReference();
<!option value="@productReference" @Html.Raw(addedProductReference == productReference ? "selected=\" selected\"" : "" )>@(variant.Name) - @(variant.CalculatePrice()?.Formatted())</!option>
}
</select>
}
}
else
{
@Html.Hidden("productReference", product.GetProductReference())
<p>@(product.CalculatePrice()?.Formatted())</p>
}
}
}