Skip to content

Commit

Permalink
Discount documentation fixes (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
NuktukDev authored Oct 6, 2023
1 parent 66a2541 commit 47b4c57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
2 changes: 1 addition & 1 deletion docs/core/extending/discounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MyCustomDiscountType extends AbstractDiscountType
/**
* Called just before cart totals are calculated.
*
* @return CartLine
* @return Cart
*/
public function apply(Cart $cart): Cart
{
Expand Down
67 changes: 15 additions & 52 deletions docs/core/reference/discounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Lunar\Models\Discount
| `id` | | |
| `name` | The given name for the discount | |
| `handle` | The unique handle for the discount | |
| `type` | The type of discount | `Lunar\DiscountTypes\Coupon` |
| `type` | The type of discount | `Lunar\DiscountTypes\BuyXGetY` |
| `data` | JSON | Any data to be used by the type class
| `starts_at` | The datetime the discount starts (required) |
| `ends_at` | The datetime the discount expires, if `NULL` it won't expire |
Expand Down Expand Up @@ -94,78 +94,41 @@ Lunar\Models\DiscountPurchasable
### Adding your own Discount type

```php
namespace App\Discounts;
<?php

use Lunar\Base\DataTransferObjects\CartDiscount;
use Lunar\DataTypes\Price;
use Lunar\Facades\Discounts;
use Lunar\Models\CartLine;
use Lunar\Models\Discount;

class CustomDiscount
{
protected Discount $discount;
namespace App\DiscountTypes;

/**
* Set the data for the discount to user.
*
* @param array $data
* @return self
*/
public function with(Discount $discount): self
{
$this->discount = $discount;

return $this;
}
use Lunar\Models\Cart;
use Lunar\DiscountTypes\AbstractDiscountType;

class MyCustomDiscountType extends AbstractDiscountType
{
/**
* Return the name of the discount.
*
* @return string
*/
public function getName(): string
{
return 'Custom Discount';
return 'Custom Discount Type';
}

/**
* Called just before cart totals are calculated.
*
* @return CartLine
* @return Cart
*/
public function execute(CartLine $cartLine): CartLine
public function apply(Cart $cart): Cart
{
$data = $this->discount->data;

// Return the unaltered cart line back
if (! $conditionIsMet) {
return $cartLine;
}

$cartLine->discount = $this->discount;

$discountTotal = $cartLine->unitPrice->value * $discountQuantity;

$cartLine->discountTotal = new Price(
$discountTotal,
$cartLine->cart->currency,
1
);

$cartLine->subTotalDiscounted = new Price(
$line->subTotal->value - $discountTotal,
$cart->currency,
1
);
// ...
return $cart;
}
}

```

```php
Discounts::addType(
CustomDiscount::class
);
use Lunar\Facades\Discounts;

Discounts::addType(MyCustomDiscountType::class);
```

1 comment on commit 47b4c57

@vercel
Copy link

@vercel vercel bot commented on 47b4c57 Oct 6, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.