variant fix

This commit is contained in:
merdan 2022-02-10 14:41:13 +05:00
parent b3efe23ee4
commit c9fe47660d
3 changed files with 49 additions and 21 deletions

View File

@ -8,12 +8,6 @@ use Webkul\Marketplace\Repositories\ProductRepository;
class CartItemResource extends JsonResource
{
public function __construct($resource)
{
$this->sellerProductRepository = app(ProductRepository::class);
parent::__construct($resource);
}
/**
* Transform the resource into an array.
*
@ -22,10 +16,8 @@ class CartItemResource extends JsonResource
*/
public function toArray($request)
{
$seller = $this->sellerProductRepository->getSellerByProductId($this->product_id);
return [
'id' => $this->id,
'vendor' => $this->when($seller,$seller->shop_title),
'quantity' => $this->quantity,
'name' => $this->name,
'base_total_weight' => $this->base_total_weight,

View File

@ -4,10 +4,16 @@ namespace Sarga\API\Http\Resources\Checkout;
use Illuminate\Http\Resources\Json\JsonResource;
use Sarga\API\Http\Resources\Customer\AddressResource;
use Webkul\Marketplace\Repositories\ProductRepository;
use Webkul\Tax\Helpers\Tax;
class CartResource extends JsonResource
{
public function __construct($resource)
{
$this->sellerProductRepository = app(ProductRepository::class);
parent::__construct($resource);
}
/**
* Transform the resource into an array.
*
@ -30,33 +36,33 @@ class CartResource extends JsonResource
'items_count' => $this->items_count,
'items_qty' => $this->items_qty,
'grand_total' => $this->grand_total,
'formatted_grand_total' => core()->formatPrice($this->grand_total, $this->cart_currency_code),
'formatted_grand_total' => core()->formatPrice($this->grand_total, $this->cart_currency_code),
'base_grand_total' => $this->base_grand_total,
'formatted_base_grand_total' => core()->formatBasePrice($this->base_grand_total),
'formatted_base_grand_total' => core()->formatBasePrice($this->base_grand_total),
'sub_total' => $this->sub_total,
'formatted_sub_total' => core()->formatPrice($this->sub_total, $this->cart_currency_code),
'formatted_sub_total' => core()->formatPrice($this->sub_total, $this->cart_currency_code),
'base_sub_total' => $this->base_sub_total,
'formatted_base_sub_total' => core()->formatBasePrice($this->base_sub_total),
'formatted_base_sub_total' => core()->formatBasePrice($this->base_sub_total),
'tax_total' => $this->tax_total,
'formatted_tax_total' => core()->formatPrice($this->tax_total, $this->cart_currency_code),
'formatted_tax_total' => core()->formatPrice($this->tax_total, $this->cart_currency_code),
'base_tax_total' => $this->base_tax_total,
'formatted_base_tax_total' => core()->formatBasePrice($this->base_tax_total),
'formatted_base_tax_total' => core()->formatBasePrice($this->base_tax_total),
'discount' => $this->discount_amount,
'formatted_discount' => core()->formatPrice($this->discount_amount, $this->cart_currency_code),
'formatted_discount' => core()->formatPrice($this->discount_amount, $this->cart_currency_code),
'base_discount' => $this->base_discount_amount,
'formatted_base_discount' => core()->formatBasePrice($this->base_discount_amount),
'formatted_base_discount' => core()->formatBasePrice($this->base_discount_amount),
'checkout_method' => $this->checkout_method,
'items' => CartItemResource::collection($this->items),
'vendors' => $this->groupByVendors($this->items),
'selected_shipping_rate' => new CartShippingRateResource($this->selected_shipping_rate),
'payment' => new CartPaymentResource($this->payment),
'billing_address' => new AddressResource($this->billing_address),
'shipping_address' => new AddressResource($this->shipping_address),
'taxes' => json_encode($taxes, JSON_FORCE_OBJECT),
'formatted_taxes' => json_encode($formatedTaxes, JSON_FORCE_OBJECT),
'formatted_taxes' => json_encode($formatedTaxes, JSON_FORCE_OBJECT),
'base_taxes' => json_encode($baseTaxes, JSON_FORCE_OBJECT),
'formatted_base_taxes' => json_encode($formatedBaseTaxes, JSON_FORCE_OBJECT),
'formatted_discounted_sub_total' => core()->formatPrice($this->sub_total - $this->discount_amount, $this->cart_currency_code),
'formatted_base_discounted_sub_total' => core()->formatPrice($this->base_sub_total - $this->base_discount_amount, $this->cart_currency_code),
'formatted_base_taxes' => json_encode($formatedBaseTaxes, JSON_FORCE_OBJECT),
'formatted_discounted_sub_total' => core()->formatPrice($this->sub_total - $this->discount_amount, $this->cart_currency_code),
'formatted_base_discounted_sub_total'=> core()->formatPrice($this->base_sub_total - $this->base_discount_amount, $this->cart_currency_code),
];
}
@ -81,4 +87,13 @@ class CartResource extends JsonResource
return $result;
}
private function groupByVendors($items){
$data = array();
foreach($items as $item){
$seller = $this->sellerProductRepository->getSellerByProductId($item->product_id);
$data[$seller->shop_title ?? 'default'][] = CartItemResource::make($item);
}
return $data;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Sarga\API\Http\Resources\Checkout;
class VendorItemsResource extends \Illuminate\Http\Resources\Json\JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
return [
'shop_title' => $this->shop_title,
'items' => CartItemResource::collection($this->items),
];
}
}