Skip to content

Commit

Permalink
🐛 Fix save product metas
Browse files Browse the repository at this point in the history
  • Loading branch information
juzaweb committed Jan 24, 2024
1 parent e945953 commit 83a741c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 19 deletions.
4 changes: 3 additions & 1 deletion assets/mix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const mix = require('laravel-mix');
const path = require('path');

const baseAsset = 'plugins/ecommerce/assets';
const baseAsset = path.resolve(__dirname, '');
const baseStyles = baseAsset + '/styles';
const basePublish = baseAsset + '/public';

mix.styles(
Expand Down
54 changes: 37 additions & 17 deletions src/Actions/EcommerceAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@

namespace Juzaweb\Ecommerce\Actions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Juzaweb\Backend\Models\Post;
use Juzaweb\CMS\Abstracts\Action;
use Juzaweb\CMS\Facades\HookAction;
use Juzaweb\Ecommerce\Contracts\CartManagerContract;
use Juzaweb\Ecommerce\Http\Controllers\Frontend\CartController;
use Juzaweb\Ecommerce\Http\Controllers\Frontend\CheckoutController;
use Juzaweb\Ecommerce\Http\Resources\OrderResource;
use Juzaweb\Ecommerce\Http\Resources\PaymentMethodCollectionResource;
use Juzaweb\Ecommerce\Http\Resources\ProductVariantResource;
use Juzaweb\Ecommerce\Models\Order;
use Juzaweb\Ecommerce\Models\PaymentMethod;
use Juzaweb\Ecommerce\Models\ProductVariant;

class EcommerceAction extends Action
{
public function handle()
public function handle(): void
{
$this->addAction(
Action::INIT_ACTION,
Expand Down Expand Up @@ -78,14 +79,22 @@ public function handle()
[$this, 'registerEmailHooks']
);

$this->addFilter(
'frontend.post_type.products.detail.data',
[$this, 'addVariantsProductDetail']
);
$this->addFilter('post.withFrontendDetailBuilder', [$this, 'addWithVariantsProductDetail']);

$this->addFilter('jw.resource.post.products', [$this, 'addVariantsProductDetail'], 20, 2);
}

public function registerPostTypes()
public function registerPostTypes(): void
{
$productInvisibleMetas = [
'price',
'sku_code',
'barcode',
'quantity',
'inventory_management',
'disable_out_of_stock',
];

HookAction::registerPostType(
'products',
[
Expand All @@ -96,6 +105,11 @@ public function registerPostTypes()
'category',
'tag'
],
'metas' => collect($productInvisibleMetas)
->mapWithKeys(
fn ($item) => [$item => ['visible' => false]]
)
->toArray(),
]
);

Expand All @@ -118,7 +132,7 @@ public function registerPostTypes()
);
}

public function registerConfigs()
public function registerConfigs(): void
{
HookAction::registerConfig(
[
Expand All @@ -128,10 +142,10 @@ public function registerConfigs()
);
}

public function addFormProduct($model)
public function addFormProduct($model): void
{
$variant = ProductVariant::findByProduct($model->id);
if (empty($variant)) {
if ($variant === null) {
$variant = new ProductVariant();
}

Expand Down Expand Up @@ -183,7 +197,7 @@ public function saveDataProduct($model, $data): void
$variantData['names'] = ['Default'];
$variantData['post_id'] = $model->id;

$variant = ProductVariant::updateOrCreate(
ProductVariant::updateOrCreate(
['id' => $variant->id ?? 0],
$variantData
);
Expand Down Expand Up @@ -308,14 +322,20 @@ public function registerEmailHooks(): void
);
}

public function addVariantsProductDetail(array $data): array
public function addWithVariantsProductDetail(array $with): array
{
$data['post']['metas']['variants'] = ProductVariant::cacheFor(
$with['variants'] = fn ($q) => $q->cacheFor(
config('juzaweb.performance.query_cache.lifetime')
)
->wherePostId($data['post']['id'])
->get()
->toArray();
);

return $with;
}

public function addVariantsProductDetail(array $data, Post $resource): array
{
$data['variants'] = ProductVariantResource::collection($resource->variants)
->response()
->getData(true)['data'];
return $data;
}
}
2 changes: 1 addition & 1 deletion src/Http/Requests/AddToCartRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class AddToCartRequest extends FormRequest
{
public function rules()
public function rules(): array
{
return [
'variant_id' => [
Expand Down
33 changes: 33 additions & 0 deletions src/Http/Resources/ProductVariantResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\Ecommerce\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ProductVariantResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->resource->id,
'sku_code' => $this->resource->sku_code,
'barcode' => $this->resource->barcode,
'title' => $this->resource->title,
'thumbnail' => $this->resource->thumbnail,
'description' => $this->resource->description,
'names' => $this->resource->names,
'images' => $this->resource->images,
'price' => $this->resource->price,
'compare_price' => $this->resource->compare_price,
'type' => $this->resource->type,
];
}
}
19 changes: 19 additions & 0 deletions src/Observes/ProductObserve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\Ecommerce\Observes;

class ProductObserve
{
public function creating()
{

}
}
19 changes: 19 additions & 0 deletions src/Providers/EcommerceServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Juzaweb\Ecommerce\Http\Middleware\EcommerceTheme;
use Juzaweb\Ecommerce\Models\Order;
use Juzaweb\Ecommerce\Models\OrderItem;
use Juzaweb\Ecommerce\Models\ProductVariant;
use Juzaweb\Ecommerce\Observes\ProductObserve;
use Juzaweb\Ecommerce\Repositories\CartRepository;
use Juzaweb\Ecommerce\Repositories\CartRepositoryEloquent;
use Juzaweb\Ecommerce\Repositories\ProductRepository;
Expand Down Expand Up @@ -80,6 +82,21 @@ function () {
);
}
);

MacroableModel::addMacro(
Post::class,
'variants',
function () {
/**
* @var Post $this
*/
return $this->hasMany(
ProductVariant::class,
'post_id',
'id'
);
}
);
}

public function register(): void
Expand Down Expand Up @@ -117,5 +134,7 @@ function ($app) {
);
}
);

//Post::observe(ProductObserve::class);
}
}

0 comments on commit 83a741c

Please sign in to comment.