Cart api
This commit is contained in:
parent
0d44c66293
commit
a943db9faf
|
|
@ -0,0 +1,175 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Controllers;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Sarga\API\Http\Resources\Checkout\CartResource;
|
||||
use Webkul\Checkout\Facades\Cart;
|
||||
use Webkul\Checkout\Repositories\CartItemRepository;
|
||||
use Webkul\Customer\Repositories\WishlistRepository;
|
||||
use Webkul\RestApi\Http\Controllers\V1\Shop\Customer\CartController;
|
||||
|
||||
class Carts extends CartController
|
||||
{
|
||||
/**
|
||||
* Get the customer cart.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return response([
|
||||
'data' => $cart ? new CartResource($cart) : null,
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* Add item to the cart.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Webkul\Customer\Repositories\WishlistRepository $wishlistRepository
|
||||
* @param int $productId
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function add(Request $request, WishlistRepository $wishlistRepository, int $productId)
|
||||
{
|
||||
$customer = $request->user();
|
||||
|
||||
try {
|
||||
Event::dispatch('checkout.cart.item.add.before', $productId);
|
||||
|
||||
$result = Cart::addProduct($productId, $request->all());
|
||||
|
||||
if (is_array($result) && isset($result['warning'])) {
|
||||
return response([
|
||||
'message' => $result['warning'],
|
||||
], 400);
|
||||
}
|
||||
|
||||
$wishlistRepository->deleteWhere(['product_id' => $productId, 'customer_id' => $customer->id]);
|
||||
|
||||
Event::dispatch('checkout.cart.item.add.after', $result);
|
||||
|
||||
Cart::collectTotals();
|
||||
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return response([
|
||||
'data' => $cart ? new \Webkul\RestApi\Http\Resources\V1\Shop\Checkout\CartResource($cart) : null,
|
||||
'message' => __('rest-api::app.checkout.cart.item.success'),
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return response([
|
||||
'message' => $e->getMessage(),
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the cart.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Webkul\Checkout\Repositories\CartItemRepository $cartItemRepository
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, CartItemRepository $cartItemRepository)
|
||||
{
|
||||
$this->validate($request, [
|
||||
'qty' => 'required|array',
|
||||
]);
|
||||
|
||||
foreach ($request->qty as $qty) {
|
||||
if ($qty <= 0) {
|
||||
return response([
|
||||
'message' => __('rest-api::app.checkout.cart.quantity.illegal'),
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($request->qty as $itemId => $qty) {
|
||||
$item = $cartItemRepository->findOneByField('id', $itemId);
|
||||
|
||||
Event::dispatch('checkout.cart.item.update.before', $itemId);
|
||||
|
||||
Cart::updateItems(['qty' => $request->qty]);
|
||||
|
||||
Event::dispatch('checkout.cart.item.update.after', $item);
|
||||
}
|
||||
|
||||
Cart::collectTotals();
|
||||
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return response([
|
||||
'data' => $cart ? new CartResource($cart) : null,
|
||||
'message' => __('rest-api::app.checkout.cart.quantity.success'),
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* Remove item from the cart.
|
||||
*
|
||||
* @param int $cartItemId
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function removeItem($cartItemId)
|
||||
{
|
||||
Event::dispatch('checkout.cart.item.delete.before', $cartItemId);
|
||||
|
||||
Cart::removeItem($cartItemId);
|
||||
|
||||
Event::dispatch('checkout.cart.item.delete.after', $cartItemId);
|
||||
|
||||
Cart::collectTotals();
|
||||
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return response([
|
||||
'data' => $cart ? new CartResource($cart) : null,
|
||||
'message' => __('rest-api::app.checkout.cart.item.success'),
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* Empty the cart.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
function empty() {
|
||||
Event::dispatch('checkout.cart.delete.before');
|
||||
|
||||
Cart::deActivateCart();
|
||||
|
||||
Event::dispatch('checkout.cart.delete.after');
|
||||
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return response([
|
||||
'data' => $cart ? new CartResource($cart) : null,
|
||||
'message' => __('rest-api::app.checkout.cart.item.success-remove'),
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* Move cart item to wishlist.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function moveToWishlist($cartItemId)
|
||||
{
|
||||
Event::dispatch('checkout.cart.item.move-to-wishlist.before', $cartItemId);
|
||||
|
||||
Cart::moveToWishlist($cartItemId);
|
||||
|
||||
Event::dispatch('checkout.cart.item.move-to-wishlist.after', $cartItemId);
|
||||
|
||||
Cart::collectTotals();
|
||||
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return response([
|
||||
'data' => $cart ? new CartResource($cart) : null,
|
||||
'message' => __('rest-api::app.checkout.cart.move-wishlist.success'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Sarga\API\Http\Resources\Checkout\CartResource;
|
||||
use Sarga\API\Http\Resources\Checkout\CartShippingRateResource;
|
||||
use Webkul\Checkout\Facades\Cart;
|
||||
use Webkul\Checkout\Http\Requests\CustomerAddressForm;
|
||||
use Webkul\Payment\Facades\Payment;
|
||||
use Webkul\RestApi\Http\Controllers\V1\Shop\Customer\CheckoutController;
|
||||
use Webkul\Shipping\Facades\Shipping;
|
||||
|
||||
class Checkout extends CheckoutController
|
||||
{
|
||||
/**
|
||||
* Save customer address.
|
||||
*
|
||||
* @param \Webkul\Checkout\Http\Requests\CustomerAddressForm $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function saveAddress(CustomerAddressForm $request)
|
||||
{
|
||||
$data = $request->all();
|
||||
|
||||
$data['billing']['address1'] = implode(PHP_EOL, array_filter($data['billing']['address1']));
|
||||
|
||||
$data['shipping']['address1'] = implode(PHP_EOL, array_filter($data['shipping']['address1']));
|
||||
|
||||
if (isset($data['billing']['id']) && str_contains($data['billing']['id'], 'address_')) {
|
||||
unset($data['billing']['id']);
|
||||
unset($data['billing']['address_id']);
|
||||
}
|
||||
|
||||
if (isset($data['shipping']['id']) && Str::contains($data['shipping']['id'], 'address_')) {
|
||||
unset($data['shipping']['id']);
|
||||
unset($data['shipping']['address_id']);
|
||||
}
|
||||
|
||||
if (Cart::hasError() || ! Cart::saveCustomerAddress($data) || ! Shipping::collectRates()) {
|
||||
abort(400);
|
||||
}
|
||||
|
||||
$rates = [];
|
||||
|
||||
foreach (Shipping::getGroupedAllShippingRates() as $code => $shippingMethod) {
|
||||
$rates[] = [
|
||||
'carrier_title' => $shippingMethod['carrier_title'],
|
||||
'rates' => CartShippingRateResource::collection(collect($shippingMethod['rates'])),
|
||||
];
|
||||
}
|
||||
|
||||
Cart::collectTotals();
|
||||
|
||||
return response([
|
||||
'data' => [
|
||||
'rates' => $rates,
|
||||
'cart' => new CartResource(Cart::getCart()),
|
||||
],
|
||||
'message' => 'Address saved successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save shipping method.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function saveShipping(Request $request)
|
||||
{
|
||||
$shippingMethod = $request->get('shipping_method');
|
||||
|
||||
if (Cart::hasError()
|
||||
|| ! $shippingMethod
|
||||
|| ! Cart::saveShippingMethod($shippingMethod)
|
||||
) {
|
||||
abort(400);
|
||||
}
|
||||
|
||||
Cart::collectTotals();
|
||||
|
||||
return response([
|
||||
'data' => [
|
||||
'methods' => Payment::getPaymentMethods(),
|
||||
'cart' => new CartResource(Cart::getCart()),
|
||||
],
|
||||
'message' => 'Shipping method saved successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save payment method.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function savePayment(Request $request)
|
||||
{
|
||||
$payment = $request->get('payment');
|
||||
|
||||
if (Cart::hasError() || ! $payment || ! Cart::savePaymentMethod($payment)) {
|
||||
abort(400);
|
||||
}
|
||||
|
||||
return response([
|
||||
'data' => [
|
||||
'cart' => new CartResource(Cart::getCart()),
|
||||
],
|
||||
'message' => 'Payment method saved successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for minimum order.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function checkMinimumOrder()
|
||||
{
|
||||
$minimumOrderAmount = (float) core()->getConfigData('sales.orderSettings.minimum-order.minimum_order_amount') ?? 0;
|
||||
|
||||
$status = Cart::checkMinimumOrder();
|
||||
|
||||
return response([
|
||||
'data' => [
|
||||
'cart' => new CartResource(Cart::getCart()),
|
||||
'status' => ! $status ? false : true,
|
||||
],
|
||||
'message' => ! $status ? __('rest-api::app.checkout.minimum-order-message', ['amount' => core()->currency($minimumOrderAmount)]) : 'Success',
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Resources\Checkout;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Sarga\API\Http\Resources\Catalog\Product as ProductResource;
|
||||
|
||||
class CartItemResource extends JsonResource
|
||||
{
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->sellerProductRepository = app(Webkul\Marketplace\Repositories\ProductRepository::class);
|
||||
parent::__construct($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
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,
|
||||
'price' => $this->price,
|
||||
'formatted_price' => core()->formatPrice($this->price, $this->cart->cart_currency_code),
|
||||
'base_price' => $this->base_price,
|
||||
'formatted_base_price' => core()->formatBasePrice($this->base_price),
|
||||
'custom_price' => $this->custom_price,
|
||||
'formatted_custom_price' => core()->formatPrice($this->custom_price, $this->cart->cart_currency_code),
|
||||
'total' => $this->total,
|
||||
'formatted_total' => core()->formatPrice($this->total, $this->cart->cart_currency_code),
|
||||
'base_total' => $this->base_total,
|
||||
'formatted_base_total' => core()->formatBasePrice($this->base_total),
|
||||
'tax_percent' => $this->tax_percent,
|
||||
'tax_amount' => $this->tax_amount,
|
||||
'formatted_tax_amount' => core()->formatPrice($this->tax_amount, $this->cart->cart_currency_code),
|
||||
'base_tax_amount' => $this->base_tax_amount,
|
||||
'formatted_base_tax_amount' => core()->formatBasePrice($this->base_tax_amount),
|
||||
'discount_percent' => $this->discount_percent,
|
||||
'discount_amount' => $this->discount_amount,
|
||||
'formatted_discount_amount' => core()->formatPrice($this->discount_amount, $this->cart->cart_currency_code),
|
||||
'base_discount_amount' => $this->base_discount_amount,
|
||||
'formatted_base_discount_amount'=> core()->formatBasePrice($this->base_discount_amount),
|
||||
'additional' => is_array($this->resource->additional)
|
||||
? $this->resource->additional
|
||||
: json_decode($this->resource->additional, true),
|
||||
'child' => new self($this->child),
|
||||
'product' => $this->when($this->product_id, new ProductResource($this->product)),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Resources\Checkout;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CartPaymentResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'method' => $this->method,
|
||||
'method_title' => core()->getConfigData('sales.paymentmethods.' . $this->method . '.title'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Resources\Checkout;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Sarga\API\Http\Resources\Customer\AddressResource;
|
||||
use Webkul\Tax\Helpers\Tax;
|
||||
|
||||
class CartResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
$taxes = Tax::getTaxRatesWithAmount($this, false);
|
||||
$baseTaxes = Tax::getTaxRatesWithAmount($this, true);
|
||||
|
||||
$formatedTaxes = $this->formatTaxAmounts($taxes, false);
|
||||
$formatedBaseTaxes = $this->formatTaxAmounts($baseTaxes, true);
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'shipping_method' => $this->shipping_method,
|
||||
'coupon_code' => $this->coupon_code,
|
||||
'is_gift' => $this->is_gift,
|
||||
'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),
|
||||
'base_grand_total' => $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),
|
||||
'base_sub_total' => $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),
|
||||
'base_tax_total' => $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),
|
||||
'base_discount' => $this->base_discount_amount,
|
||||
'formatted_base_discount' => core()->formatBasePrice($this->base_discount_amount),
|
||||
'checkout_method' => $this->checkout_method,
|
||||
'items' => CartItemResource::collection($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),
|
||||
'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),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Format tax amounts.
|
||||
*
|
||||
* @param array $taxes
|
||||
* @param bool $isBase
|
||||
* @return array
|
||||
*/
|
||||
private function formatTaxAmounts(array $taxes, bool $isBase = false): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($taxes as $taxRate => $taxAmount) {
|
||||
if ($isBase === true) {
|
||||
$result[$taxRate] = core()->formatBasePrice($taxAmount);
|
||||
} else {
|
||||
$result[$taxRate] = core()->formatPrice($taxAmount, $this->cart_currency_code);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Resources\Checkout;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Webkul\Checkout\Facades\Cart;
|
||||
|
||||
class CartShippingRateResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$cart = Cart::getCart();
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'carrier' => $this->carrier,
|
||||
'carrier_title' => $this->carrier_title,
|
||||
'method' => $this->method,
|
||||
'method_title' => $this->method_title,
|
||||
'method_description' => $this->method_description,
|
||||
'price' => $this->price,
|
||||
'formatted_price' => core()->formatPrice($this->price, $cart->cart_currency_code),
|
||||
'base_price' => $this->base_price,
|
||||
'formatted_base_price' => core()->formatBasePrice($this->base_price),
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Sarga\API\Http\Controllers\Addresses;
|
||||
use Sarga\API\Http\Controllers\Carts;
|
||||
use Sarga\API\Http\Controllers\Checkout;
|
||||
use Sarga\API\Http\Controllers\Customers;
|
||||
use Sarga\API\Http\Controllers\Categories;
|
||||
use Sarga\API\Http\Controllers\Channels;
|
||||
|
|
@ -77,6 +79,38 @@ Route::group(['prefix' => 'api'], function () {
|
|||
Route::post('recipients', [Addresses::class, 'createRecipient']);
|
||||
Route::put('recipients/{id}', [Addresses::class, 'updateRecipient']);
|
||||
Route::delete('recipients/{id}', [Addresses::class, 'destroy']);
|
||||
|
||||
/**
|
||||
* Customer cart routes.
|
||||
*/
|
||||
Route::get('customer/cart', [Carts::class, 'get']);
|
||||
|
||||
Route::post('customer/cart/add/{productId}', [Carts::class, 'add']);
|
||||
|
||||
Route::put('customer/cart/update', [Carts::class, 'update']);
|
||||
|
||||
Route::delete('customer/cart/remove/{cartItemId}', [Carts::class, 'removeItem']);
|
||||
|
||||
Route::delete('customer/cart/empty', [Carts::class, 'empty']);
|
||||
|
||||
Route::post('customer/cart/move-to-wishlist/{cartItemId}', [Carts::class, 'moveToWishlist']);
|
||||
|
||||
Route::post('customer/cart/coupon', [Carts::class, 'applyCoupon']);
|
||||
|
||||
Route::delete('customer/cart/coupon', [Carts::class, 'removeCoupon']);
|
||||
|
||||
/**
|
||||
* Customer checkout routes.
|
||||
*/
|
||||
Route::post('customer/checkout/save-address', [Checkout::class, 'saveAddress']);
|
||||
|
||||
Route::post('customer/checkout/save-shipping', [Checkout::class, 'saveShipping']);
|
||||
|
||||
Route::post('customer/checkout/save-payment', [Checkout::class, 'savePayment']);
|
||||
|
||||
Route::post('customer/checkout/check-minimum-order', [Checkout::class, 'checkMinimumOrder']);
|
||||
|
||||
Route::post('customer/checkout/save-order', [Checkout::class, 'saveOrder']);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\Admin\DataGrids;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Webkul\Customer\Models\CustomerAddress;
|
||||
use Webkul\Customer\Repositories\CustomerRepository;
|
||||
use Webkul\Ui\DataGrid\DataGrid;
|
||||
use Webkul\Ui\DataGrid\Traits\ProvideDataGridPlus;
|
||||
|
||||
class RecipientsDataGrid extends DataGrid
|
||||
{
|
||||
use ProvideDataGridPlus;
|
||||
|
||||
/**
|
||||
* Index.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $index = 'address_id';
|
||||
|
||||
/**
|
||||
* Sort order.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $sortOrder = 'desc';
|
||||
|
||||
/**
|
||||
* Customer repository instance.
|
||||
*
|
||||
* @var \Webkul\Customer\Repositories\CustomerRepository
|
||||
*/
|
||||
protected $customerRepository;
|
||||
|
||||
/**
|
||||
* Create a new datagrid instance.
|
||||
*
|
||||
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(CustomerRepository $customerRepository)
|
||||
{
|
||||
$this->customerRepository = $customerRepository;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare query builder.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function prepareQueryBuilder()
|
||||
{
|
||||
$customer = $this->customerRepository->find(request('id'));
|
||||
|
||||
$queryBuilder = DB::table('addresses as ca')
|
||||
->leftJoin('customers as c', 'ca.customer_id', '=', 'c.id')
|
||||
->addSelect('ca.id as address_id', 'ca.first_name', 'ca.last_name', 'ca.phone')
|
||||
->where('ca.address_type', 'recipient')
|
||||
->where('c.id', $customer->id);
|
||||
|
||||
$this->addFilter('first_name', 'ca.first_name');
|
||||
$this->addFilter('last_name', 'ca.last_name');
|
||||
$this->addFilter('phone', 'ca.phone');
|
||||
$this->setQueryBuilder($queryBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add columns.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addColumns()
|
||||
{
|
||||
$this->addColumn([
|
||||
'index' => 'address_id',
|
||||
'label' => trans('admin::app.datagrid.id'),
|
||||
'type' => 'number',
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
'filterable' => true,
|
||||
]);
|
||||
|
||||
$this->addColumn([
|
||||
'index' => 'first_name',
|
||||
'label' => trans('admin::app.customers.customers.first_name'),
|
||||
'type' => 'string',
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
'filterable' => true,
|
||||
]);
|
||||
|
||||
$this->addColumn([
|
||||
'index' => 'last_name',
|
||||
'label' => trans('admin::app.customers.customers.last_name'),
|
||||
'type' => 'string',
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
'filterable' => true,
|
||||
]);
|
||||
|
||||
$this->addColumn([
|
||||
'index' => 'phone',
|
||||
'label' => trans('admin::app.customers.customers.phone'),
|
||||
'type' => 'string',
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
'filterable' => true,
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare actions.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function prepareActions()
|
||||
{
|
||||
$this->addAction([
|
||||
'title' => trans('admin::app.datagrid.edit'),
|
||||
'method' => 'GET',
|
||||
'route' => 'admin.customer.addresses.edit',
|
||||
'icon' => 'icon pencil-lg-icon',
|
||||
]);
|
||||
|
||||
$this->addAction([
|
||||
'title' => trans('admin::app.datagrid.delete'),
|
||||
'method' => 'POST',
|
||||
'route' => 'admin.customer.addresses.delete',
|
||||
'confirm_text' => trans('ui::app.datagrid.massaction.delete', ['resource' => 'address']),
|
||||
'icon' => 'icon trash-icon',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare mass actions.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function prepareMassActions()
|
||||
{
|
||||
$this->addMassAction([
|
||||
'type' => 'delete',
|
||||
'label' => trans('admin::app.customers.addresses.delete'),
|
||||
'action' => route('admin.customer.addresses.massdelete', request('id')),
|
||||
'method' => 'POST',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\Admin\Http\Controllers;
|
||||
|
||||
use Sarga\Admin\DataGrids\AddressDataGrid;
|
||||
use Sarga\Admin\DataGrids\RecipientsDataGrid;
|
||||
|
||||
class Addresses extends \Webkul\Admin\Http\Controllers\Customer\AddressController
|
||||
{
|
||||
/**
|
||||
* Fetch address by customer id.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function index($id)
|
||||
{
|
||||
$customer = $this->customerRepository->find($id);
|
||||
|
||||
if (request()->ajax()) {
|
||||
return app(AddressDataGrid::class)->toJson();
|
||||
}
|
||||
|
||||
return view($this->_config['view'], compact('customer'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch address by customer id.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function recipients($id)
|
||||
{
|
||||
$customer = $this->customerRepository->find($id);
|
||||
|
||||
if (request()->ajax()) {
|
||||
return app(RecipientsDataGrid::class)->toJson();
|
||||
}
|
||||
|
||||
return view($this->_config['view'], compact('customer'));
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,8 @@ class AdminServiceProvider extends ServiceProvider
|
|||
|
||||
$this->publishes([
|
||||
__DIR__ . '/../Resources/views/catalog/categories' => resource_path('views/vendor/admin/catalog/categories'),
|
||||
__DIR__ . '/../Resources/views/customers/addresses' => resource_path('views/vendor/admin/customers/addresses'),
|
||||
__DIR__ . '/../Resources/views/customers/edit.blade.php' => resource_path('views/vendor/admin/customers/edit.blade.php'),
|
||||
]);
|
||||
|
||||
$this->app->register(EventServiceProvider::class);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
@extends('admin::layouts.content')
|
||||
|
||||
@section('page_title')
|
||||
{{ __('admin::app.customers.addresses.title', ['customer_name' => $customer->first_name . ' ' . $customer->last_name]) }}
|
||||
@stop
|
||||
|
||||
@section('content')
|
||||
<div class="content">
|
||||
<div class="page-header">
|
||||
<div class="page-title">
|
||||
<h1>
|
||||
<i class="icon angle-left-icon back-link" onclick="window.location = '{{ route('admin.customer.edit', ['id' => $customer->id]) }}'"></i>
|
||||
|
||||
{{ __('admin::app.customers.addresses.title', ['customer_name' => $customer->first_name . ' ' . $customer->last_name]) }}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
<div class="page-action">
|
||||
<a href="{{ route('admin.customer.addresses.create', ['id' => $customer->id]) }}" class="btn btn-lg btn-primary">
|
||||
{{ __('admin::app.customers.addresses.create-btn-title') }}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.addresses.list.before') !!}
|
||||
|
||||
<div class="page-content">
|
||||
|
||||
{!! app('Sarga\Admin\DataGrids\AddressDataGrid')->render() !!}
|
||||
|
||||
</div>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.addresses.list.after') !!}
|
||||
</div>
|
||||
@stop
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
@extends('admin::layouts.content')
|
||||
|
||||
@section('page_title')
|
||||
{{ __('admin::app.customers.addresses.title', ['customer_name' => $customer->first_name . ' ' . $customer->last_name]) }}
|
||||
@stop
|
||||
|
||||
@section('content')
|
||||
<div class="content">
|
||||
<div class="page-header">
|
||||
<div class="page-title">
|
||||
<h1>
|
||||
<i class="icon angle-left-icon back-link" onclick="window.location = '{{ route('admin.customer.edit', ['id' => $customer->id]) }}'"></i>
|
||||
|
||||
{{ __('admin::app.customers.addresses.title', ['customer_name' => $customer->first_name . ' ' . $customer->last_name]) }}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
<div class="page-action">
|
||||
<a href="{{ route('admin.customer.addresses.create', ['id' => $customer->id]) }}" class="btn btn-lg btn-primary">
|
||||
{{ __('admin::app.customers.addresses.create-btn-title') }}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.addresses.list.before') !!}
|
||||
|
||||
<div class="page-content">
|
||||
|
||||
{!! app('Sarga\Admin\DataGrids\AddressDataGrid')->render() !!}
|
||||
|
||||
</div>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.addresses.list.after') !!}
|
||||
</div>
|
||||
@stop
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
@extends('admin::layouts.content')
|
||||
|
||||
@section('page_title')
|
||||
{{ __('admin::app.customers.customers.edit-title') }}
|
||||
@stop
|
||||
|
||||
@section('content')
|
||||
<div class="content">
|
||||
<div class="page-header">
|
||||
<div class="page-title">
|
||||
<h1>
|
||||
<i class="icon angle-left-icon back-link" onclick="window.location = '{{ route('admin.customer.index') }}'"></i>
|
||||
{{ $customer->first_name . " " . $customer->last_name }}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
<div class="page-action"></div>
|
||||
</div>
|
||||
|
||||
<tabs>
|
||||
{!! view_render_event('bagisto.admin.customer.edit.before', ['customer' => $customer]) !!}
|
||||
|
||||
<tab name="{{ __('admin::app.sales.orders.info') }}" :selected="true">
|
||||
<div class="sale-container">
|
||||
@include('admin::customers.general')
|
||||
</div>
|
||||
</tab>
|
||||
|
||||
<tab name="{{ __('admin::app.customers.customers.addresses') }}" :selected="false">
|
||||
<div class="page-content">
|
||||
<div class="page-content-button">
|
||||
<a href="{{ route('admin.customer.addresses.create', ['id' => $customer->id]) }}" class="btn btn-lg btn-primary">
|
||||
{{ __('admin::app.customers.addresses.create-btn-title') }}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="page-content-datagrid">
|
||||
<datagrid-plus src="{{ route('admin.customer.addresses.index', $customer->id) }}"></datagrid-plus>
|
||||
</div>
|
||||
</div>
|
||||
</tab>
|
||||
<tab name="Recipients" :selected="false">
|
||||
<div class="page-content">
|
||||
|
||||
<div class="page-content-datagrid">
|
||||
<datagrid-plus src="{{ route('admin.customer.addresses.recipients', $customer->id) }}"></datagrid-plus>
|
||||
</div>
|
||||
</div>
|
||||
</tab>
|
||||
|
||||
<tab name="{{ __('admin::app.layouts.invoices') }}" :selected="false">
|
||||
<div class="page-content">
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.invoices.list.before') !!}
|
||||
|
||||
<datagrid-plus src="{{ route('admin.customer.invoices.data', $customer->id) }}"></datagrid-plus>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.invoices.list.after') !!}
|
||||
</div>
|
||||
</tab>
|
||||
|
||||
<tab name="{{ __('admin::app.customers.orders.title') }}" :selected="false">
|
||||
<div class="page-content">
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.orders.list.before') !!}
|
||||
|
||||
<datagrid-plus src="{{ route('admin.customer.orders.data', $customer->id) }}"></datagrid-plus>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.orders.list.after') !!}
|
||||
</div>
|
||||
</tab>
|
||||
|
||||
{!! view_render_event('bagisto.admin.customer.edit.after', ['customer' => $customer]) !!}
|
||||
</tabs>
|
||||
</div>
|
||||
@stop
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Sarga\Admin\Http\Controllers\Addresses;
|
||||
|
||||
/**
|
||||
* Customers routes.
|
||||
*/
|
||||
Route::group(['middleware' => ['web', 'admin', 'admin_locale'], 'prefix' => config('app.admin_url')], function () {
|
||||
|
||||
/**
|
||||
* Customer's addresses routes.
|
||||
*/
|
||||
Route::get('customers/{id}/addresses', [Addresses::class, 'index'])->defaults('_config', [
|
||||
'view' => 'admin::customers.addresses.index',
|
||||
])->name('admin.customer.addresses.index');
|
||||
|
||||
Route::get('customers/{id}/recipients', [Addresses::class, 'recipients'])->defaults('_config', [
|
||||
'view' => 'admin::customers.addresses.recipients',
|
||||
])->name('admin.customer.addresses.recipients');
|
||||
});
|
||||
|
|
@ -4,3 +4,8 @@
|
|||
* Settings routes.
|
||||
*/
|
||||
require 'settings-routes.php';
|
||||
|
||||
/**
|
||||
* Customer routes
|
||||
*/
|
||||
require 'customer-routes.php';
|
||||
Loading…
Reference in New Issue