Add free order checkout process
This commit is contained in:
parent
81f825c389
commit
2d4e4ba631
@ -31,9 +31,9 @@
|
||||
{% if order.subscription %}
|
||||
<dt>Subscription</dt>
|
||||
<dd>
|
||||
{{ order.subscription_description }} <a href="https://dashboard.stripe.com/subscriptions/{{ order.subscription.stripe_id }}" target="_blank">View on Stripe ↗</a>
|
||||
{{ order.subscription_description }} <a href="https://dashboard.stripe.com/subscriptions/{{ order.subscription.stripe_id }}" target="_blank">View on Stripe ↗</a>
|
||||
</dd>
|
||||
{% else %}
|
||||
{% elif order.transaction.paypal_id %}
|
||||
<dt>PayPal Transaction</dt>
|
||||
<dd>
|
||||
{{order.transaction.get_status_display}} <a href="https://www.paypal.com/activity/payment/{{ order.transaction.paypal_id }}" target="_blank">View on PayPal ↗</a>
|
||||
@ -113,7 +113,7 @@
|
||||
<td>-${{ order.coupon_amount }}</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
<tr>
|
||||
<tr>
|
||||
<th colspan="4">Shipping:</th>
|
||||
<td>${{ order.shipping_total }}</td>
|
||||
</tr>
|
||||
|
||||
@ -477,6 +477,7 @@ class ProductVariantCreateView(
|
||||
'visible_in_listings',
|
||||
'track_inventory',
|
||||
'stock',
|
||||
'order_limit',
|
||||
]
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
||||
@ -196,7 +196,7 @@ class Cart:
|
||||
self.save()
|
||||
|
||||
def get_item_by_pk(self, pk):
|
||||
return next(i for i, v in enumerate(self) if v.variant.pk == pk)
|
||||
return next((i, v) for i, v in enumerate(self) if v.variant.pk == pk)
|
||||
|
||||
def update_item_quantity(self, item_index, quantity):
|
||||
self.items[item_index].quantity = quantity
|
||||
|
||||
@ -60,7 +60,7 @@
|
||||
</section>
|
||||
<section class="cart__summary">
|
||||
<h3>Order summary</h3>
|
||||
<form action="" method="POST" class="order-create-form">
|
||||
<form id="order-create-form" action="" method="POST" class="order-create-form">
|
||||
{% csrf_token %}
|
||||
{{form.as_p}}
|
||||
</form>
|
||||
@ -86,7 +86,13 @@
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
{% if cart.total_price == 0.00 %}
|
||||
<div>
|
||||
<button type="submit" form="order-create-form" formaction="{% url 'storefront:free-order-create' %}">Place order</button>
|
||||
</div>
|
||||
{% else %}
|
||||
<div id="paypal-button-container"></div>
|
||||
{% endif %}
|
||||
</section>
|
||||
</article>
|
||||
{% endblock %}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block head_title %}Payment Success | {% endblock %}
|
||||
{% block head_title %}Order Success | {% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article>
|
||||
<h1>Payment was successful</h1>
|
||||
<p>Thank you for your order!</p>
|
||||
</article>
|
||||
<article class="text-center">
|
||||
<h1>We've received your order</h1>
|
||||
<p>Thank you!</p>
|
||||
</article>
|
||||
{% endblock %}
|
||||
|
||||
@ -57,6 +57,11 @@ urlpatterns = [
|
||||
views.OrderCreateView.as_view(),
|
||||
name='order-create'
|
||||
),
|
||||
path(
|
||||
'checkout/free/',
|
||||
views.FreeOrderCreateView.as_view(),
|
||||
name='free-order-create'
|
||||
),
|
||||
path(
|
||||
'done/',
|
||||
views.PaymentDoneView.as_view(),
|
||||
|
||||
@ -46,7 +46,7 @@ from core.models import (
|
||||
)
|
||||
from core.forms import ShippingRateForm
|
||||
from core.shipping import get_shipping_cost
|
||||
from core import OrderStatus, ShippingContainer
|
||||
from core import OrderStatus, ShippingContainer, TransactionStatus
|
||||
|
||||
from .forms import (
|
||||
AddToCartForm, CartItemUpdateForm, OrderCreateForm,
|
||||
@ -356,29 +356,41 @@ class OrderCreateView(CreateView):
|
||||
cart = Cart(request)
|
||||
|
||||
|
||||
try:
|
||||
user = User.objects.get(
|
||||
email=request.session.get('shipping_address').get('email')
|
||||
)
|
||||
except User.DoesNotExist:
|
||||
user = None
|
||||
|
||||
if user:
|
||||
variants_ordered = ProductVariant.objects.filter(
|
||||
pk__in=cart.item_variant_pks,
|
||||
order_lines__order__customer=user,
|
||||
order_lines__order__status__in=[
|
||||
OrderStatus.UNFULFILLED,
|
||||
OrderStatus.PARTIALLY_FULFILLED,
|
||||
OrderStatus.FULFILLED
|
||||
]
|
||||
).values("id", "order_limit").annotate(
|
||||
num_ordered=Sum("order_lines__quantity")
|
||||
).order_by()
|
||||
|
||||
for variant in variants_ordered:
|
||||
index, item = cart.get_item_by_pk(variant['id'])
|
||||
available = variant['order_limit'] - variant['num_ordered']
|
||||
new_qty = item.quantity if item.quantity < available else available
|
||||
if new_qty and new_qty <= 0:
|
||||
cart.remove_item(index)
|
||||
else:
|
||||
cart.update_item_quantity(index, new_qty)
|
||||
|
||||
if len(cart) == 0:
|
||||
return HttpResponseRedirect(
|
||||
reverse('storefront:product-list')
|
||||
)
|
||||
|
||||
if cart.coupon is not None:
|
||||
try:
|
||||
user = User.objects.get(
|
||||
email=request.session.get('shipping_address').get('email')
|
||||
)
|
||||
except User.DoesNotExist:
|
||||
user = None
|
||||
|
||||
if user:
|
||||
variants_ordered = ProductVariant.objects.filter(
|
||||
pk__in=cart.item_variant_pks,
|
||||
order_lines__order__customer=user,
|
||||
order_lines__order__status="fulfilled"
|
||||
).values("id", "order_limit").annotate(
|
||||
num_ordered=Sum("order_lines__quantity")
|
||||
).order_by()
|
||||
|
||||
for item in cart:
|
||||
if item.quantity
|
||||
|
||||
|
||||
|
||||
|
||||
if user in cart.coupon.users.all():
|
||||
cart.remove_coupon()
|
||||
@ -415,6 +427,47 @@ class OrderCreateView(CreateView):
|
||||
return JsonResponse(data)
|
||||
|
||||
|
||||
class FreeOrderCreateView(CreateView):
|
||||
http_method_names = ['post']
|
||||
model = Order
|
||||
form_class = OrderCreateForm
|
||||
success_url = reverse_lazy('storefront:payment-done')
|
||||
|
||||
def form_valid(self, form):
|
||||
cart = Cart(self.request)
|
||||
form.instance.subtotal_amount = cart.subtotal_price
|
||||
form.instance.coupon = cart.coupon
|
||||
form.instance.coupon_amount = cart.discount_amount
|
||||
form.instance.total_amount = cart.total_price
|
||||
form.instance.weight = cart.total_weight
|
||||
shipping_container = cart.get_shipping_container()
|
||||
form.instance.shipping_total = cart.get_shipping_price(shipping_container)
|
||||
shipping_address = self.request.session.get('shipping_address')
|
||||
form.instance.customer, form.instance.shipping_address = get_or_create_customer(self.request, shipping_address)
|
||||
form.instance.status = OrderStatus.UNFULFILLED
|
||||
self.object = form.save()
|
||||
bulk_list = cart.build_bulk_list(self.object)
|
||||
OrderLine.objects.bulk_create(bulk_list)
|
||||
self.object.minus_stock()
|
||||
|
||||
try:
|
||||
coupon = Coupon.objects.get(
|
||||
code=self.request.session.get('coupon_code')
|
||||
)
|
||||
except ObjectDoesNotExist:
|
||||
coupon = None
|
||||
|
||||
if coupon:
|
||||
self.object.coupon = coupon
|
||||
coupon.users.add(self.object.customer)
|
||||
|
||||
transaction = Transaction.objects.get(order=self.object)
|
||||
transaction.status = TransactionStatus.COMPLETED
|
||||
transaction.save()
|
||||
cart.clear()
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
@require_POST
|
||||
def paypal_order_transaction_capture(request, transaction_id):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user