Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable default measurement units #1334

4 changes: 4 additions & 0 deletions docs/core/reference/products.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ by default:
- gal
- floz

::: tip
You can define a default unit of all measurements by adding a `'default' => true` key/value pair to the desired unit.
:::

### Getting and converting measurement values

You are free to access to the `*_value` and `*_unit` values for the variant and use them in their raw form, however we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AbstractDiscount extends Component
* @var array
*/
public Collection $selectedProducts;

/**
* The product variants to restrict the coupon for.
*
Expand Down Expand Up @@ -122,32 +122,29 @@ public function mount()
$this->selectedProducts = $this->discount->purchasables()
->whereIn('type', ['limitation', 'exclusion'])
->wherePurchasableType(Product::class)
->whereHas('purchasable')
->get()
->map(function ($limitation) {
return array_merge($this->mapProductToArray($limitation->purchasable), ['type' => $limitation->type]);
});

$this->selectedProductVariants = $this->discount->purchasables()
->whereIn('type', ['limitation', 'exclusion'])
->wherePurchasableType(ProductVariant::class)
->whereHas('purchasable')
->get()
->map(function ($limitation) {
return array_merge($this->mapProductVariantToArray($limitation->purchasable), ['type' => $limitation->type]);
});

$this->selectedProductVariants = $this->discount->purchasableLimitations()
->wherePurchasableType(ProductVariant::class)
->get()
->map(function ($limitation) {
return $this->mapProductVariantToArray($limitation->purchasable);
});

$this->selectedConditions = $this->discount->purchasableConditions()
->wherePurchasableType(Product::class)
->whereHas('purchasable')
->pluck('purchasable_id')->values()->toArray();

$this->selectedRewards = $this->discount->purchasableRewards()
->wherePurchasableType(Product::class)
->whereHas('purchasable')
->pluck('purchasable_id')->values()->toArray();

$this->syncAvailability();
Expand Down Expand Up @@ -258,7 +255,7 @@ public function selectProducts(array $ids)
? $this->selectedProducts->merge($selectedProducts)
: $selectedProducts;
}

/**
* Select product variants given an array of IDs
*
Expand Down Expand Up @@ -345,7 +342,7 @@ public function removeProduct($index)
{
$this->selectedProducts->forget($index);
}

/**
* Remove the product variant by it's index.
*
Expand Down Expand Up @@ -414,7 +411,7 @@ public function save()
$this->discount->collections()->sync(
$this->selectedCollections->mapWithKeys(fn ($collection) => [$collection['id'] => ['type' => $collection['type']]])
);

$this->discount->purchasables()
->whereIn('type', ['exclusion', 'limitation'])
->where('purchasable_type', Product::class)
Expand All @@ -434,7 +431,7 @@ public function save()
])
->save();
}

$this->discount->purchasables()
->whereIn('type', ['exclusion', 'limitation'])
->where('purchasable_type', ProductVariant::class)
Expand All @@ -454,20 +451,6 @@ public function save()
])
->save();
}

$this->discount->purchasableLimitations()
->where('purchasable_type', ProductVariant::class)
->whereNotIn('purchasable_id', $this->selectedProductVariants->pluck('id'))
->delete();

foreach ($this->selectedProductVariants as $variant) {
$this->discount->purchasableLimitations()->firstOrCreate([
'discount_id' => $this->discount->id,
'type' => 'limitation',
'purchasable_type' => ProductVariant::class,
'purchasable_id' => $variant['id'],
]);
}
});

$this->emit('discount.saved', $this->discount->id);
Expand Down Expand Up @@ -579,7 +562,7 @@ private function mapProductToArray($product)
'thumbnail' => optional($product->thumbnail)->getUrl('small'),
];
}

/**
* Return the data we need from a product variant
*
Expand Down
88 changes: 46 additions & 42 deletions packages/core/database/state/ConvertTaxbreakdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,20 @@ public function prepare()

public function run()
{
$prefix = config('lunar.database.table_prefix');
$updateTime = now();

if ($this->canRunOnOrders()) {
DB::table("{$prefix}orders")
->whereJsonContainsKey("${prefix}orders.tax_breakdown->[0]->total")
->orderBy('id')
->chunk(500, function ($rows) use ($prefix, $updateTime) {
foreach ($rows as $row) {
$originalBreakdown = json_decode($row->tax_breakdown, true);

DB::table("{$prefix}orders")->where('id', '=', $row->id)->update([
'tax_breakdown' => collect($originalBreakdown)->map(function ($breakdown) use ($row) {
return [
'description' => $breakdown['description'],
'identifier' => $breakdown['identifier'] ?? $breakdown['description'],
'percentage' => $breakdown['percentage'],
'value' => $breakdown['total'],
'currency_code' => $row->currency_code,
];
})->toJson(),
'updated_at' => $updateTime,
]);
}
});
}

if ($this->canRunOnOrderLines()) {
DB::table("{$prefix}order_lines")
->whereJsonContainsKey("${prefix}order_lines.tax_breakdown->[0]->total")
->orderBy("${prefix}order_lines.id")
->select(
"${prefix}order_lines.id",
"${prefix}order_lines.tax_breakdown",
"${prefix}orders.currency_code",
)
->join("${prefix}orders", "${prefix}order_lines.order_id", '=', "${prefix}orders.id")
->chunk(500, function ($rows) use ($prefix, $updateTime) {
DB::transaction(function () use ($prefix, $updateTime, $rows) {
DB::usingConnection(config('lunar.database.connection') ?: DB::getDefaultConnection(), function () {

$prefix = config('lunar.database.table_prefix');
$updateTime = now();

if ($this->canRunOnOrders()) {
DB::table("{$prefix}orders")
->whereJsonContainsKey("{$prefix}orders.tax_breakdown->[0]->total")
->orderBy('id')
->chunk(500, function ($rows) use ($prefix, $updateTime) {
foreach ($rows as $row) {
$originalBreakdown = json_decode($row->tax_breakdown, true);

DB::table("{$prefix}order_lines")->where('id', '=', $row->id)->update([
DB::table("{$prefix}orders")->where('id', '=', $row->id)->update([
'tax_breakdown' => collect($originalBreakdown)->map(function ($breakdown) use ($row) {
return [
'description' => $breakdown['description'],
Expand All @@ -70,8 +41,41 @@ public function run()
]);
}
});
});
}
}

if ($this->canRunOnOrderLines()) {
DB::table("{$prefix}order_lines")
->whereJsonContainsKey("{$prefix}order_lines.tax_breakdown->[0]->total")
->orderBy("{$prefix}order_lines.id")
->select(
"{$prefix}order_lines.id",
"{$prefix}order_lines.tax_breakdown",
"{$prefix}orders.currency_code",
)
->join("{$prefix}orders", "{$prefix}order_lines.order_id", '=', "{$prefix}orders.id")
->chunk(500, function ($rows) use ($prefix, $updateTime) {
DB::transaction(function () use ($prefix, $updateTime, $rows) {
foreach ($rows as $row) {
$originalBreakdown = json_decode($row->tax_breakdown, true);

DB::table("{$prefix}order_lines")->where('id', '=', $row->id)->update([
'tax_breakdown' => collect($originalBreakdown)->map(function ($breakdown) use ($row) {
return [
'description' => $breakdown['description'],
'identifier' => $breakdown['identifier'] ?? $breakdown['description'],
'percentage' => $breakdown['percentage'],
'value' => $breakdown['total'],
'currency_code' => $row->currency_code,
];
})->toJson(),
'updated_at' => $updateTime,
]);
}
});
});
}

});
}

protected function canRunOnOrders()
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/Base/Traits/CanScheduleAvailability.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ protected function unschedule(Relation $relation, $models, array $pivotData = []

/**
* Returns the data for the sync update.
*
* @param \Illuminate\Support\Collection $models
* @return \Illuminate\Support\Collection
*/
private function getScheduleMapping($models, array $pivotData = null)
private function getScheduleMapping(Collection $models, array $pivotData = null): Collection
{
return $models->mapWithKeys(function ($model) use ($pivotData) {
return [
Expand Down
Loading
Loading