Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
Ensure we check variant options for is_digital_product toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 17, 2023
1 parent 0150159 commit ff91468
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Listeners/ProcessCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DoubleThreeDigital\DigitalProducts\Facades\LicenseKey;
use DoubleThreeDigital\SimpleCommerce\Events\OrderStatusUpdated;
use DoubleThreeDigital\SimpleCommerce\Orders\OrderStatus;
use DoubleThreeDigital\SimpleCommerce\Products\ProductType;
use DoubleThreeDigital\SimpleCommerce\Products\ProductVariant;
use Illuminate\Support\Facades\URL;

class ProcessCheckout
Expand All @@ -21,9 +23,17 @@ public function handle(OrderStatusUpdated $event)
->filter(function ($lineItem) {
$product = $lineItem->product();

return $product->has('is_digital_product') ?
$product->get('is_digital_product') :
false;
if ($product->purchasableType() === ProductType::Variant) {
$productVariant = $product->variant($lineItem->variant());

return $productVariant->has('is_digital_product')
? $productVariant->get('is_digital_product')
: false;
}

return $product->has('is_digital_product')
? $product->get('is_digital_product')
: false;
})
->each(function ($lineItem) use ($event) {
$event->order->updateLineItem($lineItem->id(), [
Expand Down
70 changes: 70 additions & 0 deletions tests/Listeners/ProcessCheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,74 @@ public function can_process_checkout()
DigitalDownloadsNotification::class,
);
}

/** @test */
public function can_process_checkout_with_variant_product()
{
Notification::fake();

Config::set('simple-commerce.notifications', [
'digital_download_ready' => [
DigitalDownloadsNotification::class => [
'to' => 'customer',
],
],
]);

$product = Product::make()
->price(1200)
->productVariants([
'variants' => [
['name' => 'Colours', 'values' => ['Red']],
['name' => 'Sizes', 'values' => ['Small']],
],
'options' => [
['key' => 'Red_Small', 'variant' => 'Red Small', 'price' => 1200, 'is_digital_product' => true],
],
]);

$product->save();

$customer = Customer::make()
->email('[email protected]')
->merge([
'name' => 'Duncan',
]);

$customer->save();

$order = Order::make()
->lineItems([
[
'id' => Stache::generateId(),
'product' => $product->id(),
'quantity' => 1,
'variant' => 'Red_Small',
'total' => 1200,
],
])
->customer($customer->id());

$order->save();

$order->updateOrderStatus(OrderStatus::Placed);
$order->updatePaymentStatus(PaymentStatus::Paid);

$order->save();

$order = $order->fresh();

// Asset metadata is saved
$lineItem = $order->lineItems()->first();

$this->assertTrue($lineItem->metadata()->has('license_key'));
$this->assertTrue($lineItem->metadata()->has('download_url'));
$this->assertTrue($lineItem->metadata()->has('download_history'));

// Assert notification has been sent
Notification::assertSentTo(
new AnonymousNotifiable,
DigitalDownloadsNotification::class,
);
}
}

0 comments on commit ff91468

Please sign in to comment.