diff --git a/beike/Models/Product.php b/beike/Models/Product.php index bfe5faa1..dd594ccb 100644 --- a/beike/Models/Product.php +++ b/beike/Models/Product.php @@ -40,8 +40,11 @@ class Product extends Base public function getNumPricesByNum($num) { $descNumPrices = array_reverse($this->numprices->toArray()); + $multiple = 1; + if($this->sales_method == 'batches') $multiple = (int)$this->piece_to_batch; + foreach($descNumPrices as $numprice){ - if($num >= $numprice['num']){ + if($num >= ($numprice['num'] * $multiple)){ return $numprice['price']; } } diff --git a/beike/Shop/Http/Controllers/CartController.php b/beike/Shop/Http/Controllers/CartController.php index 84f323ea..9caca756 100644 --- a/beike/Shop/Http/Controllers/CartController.php +++ b/beike/Shop/Http/Controllers/CartController.php @@ -96,6 +96,7 @@ class CartController extends Controller */ public function update(CartRequest $request, $cartId): array { + // return json_fail("错误"); try { $customer = current_customer(); $quantity = (int) $request->get('quantity'); @@ -107,7 +108,6 @@ class CartController extends Controller $data = hook_filter('cart.update.data', $data); return json_success(trans('common.updated_success'), $data); - } catch (\Exception $e) { return json_fail($e->getMessage()); } diff --git a/beike/Shop/Http/Resources/NumPricesDetail.php b/beike/Shop/Http/Resources/NumPricesDetail.php index f2f884c3..8adcdc0e 100644 --- a/beike/Shop/Http/Resources/NumPricesDetail.php +++ b/beike/Shop/Http/Resources/NumPricesDetail.php @@ -13,15 +13,15 @@ namespace Beike\Shop\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; -class NumPricesDetail extends JsonResource -{ - public function toArray($request): array - { +class NumPricesDetail extends JsonResource{ + + public function toArray($request): array{ return [ 'id' => $this->id, - 'num' => $this->num, + 'num' => $this->sales_method == 'piece' ? $this->num : $this->num * $this->piece_to_batch, 'price' => $this->price, 'price_format' => currency_format($this->price), ]; + } } diff --git a/beike/Shop/Http/Resources/ProductDetail.php b/beike/Shop/Http/Resources/ProductDetail.php index 183c8f5b..e1c1970c 100644 --- a/beike/Shop/Http/Resources/ProductDetail.php +++ b/beike/Shop/Http/Resources/ProductDetail.php @@ -31,31 +31,45 @@ class ProductDetail extends JsonResource ]; } + $salesMethod = $this->sales_method ?? 'piece';// 销售方式:piece=按件卖,batches=按批卖 + $pieceToBatch = $this->piece_to_batch ?? 1;// 按批卖,每批等于多少件 + + + $this->numprices = $this->numprices->map(function($numprices) use ($salesMethod,$pieceToBatch){ + $numprices->sales_method = $salesMethod; + $numprices->piece_to_batch = $pieceToBatch; + + return $numprices; + }); + + return [ 'id' => $this->id, - 'name' => $this->description->name ?? '', - 'unit' => $this->description->unit ?? '', - 'description' => $this->description->content ?? '', - 'meta_title' => $this->description->meta_title ?? '', - 'meta_keywords' => $this->description->meta_keywords ?? '', + 'name' => $this->description->name ?? '', + 'unit' => $this->description->unit ?? '', + 'description' => $this->description->content ?? '', + 'meta_title' => $this->description->meta_title ?? '', + 'meta_keywords' => $this->description->meta_keywords ?? '', 'meta_description' => $this->description->meta_description ?? '', - 'brand_id' => $this->brand->id ?? 0, - 'brand_name' => $this->brand->name ?? '', - 'video' => $this->video ?? '', - 'images' => array_map(function ($image) { + 'brand_id' => $this->brand->id ?? 0, + 'brand_name' => $this->brand->name ?? '', + 'video' => $this->video ?? '', + 'images' => array_map(function($image){ return [ - 'preview' => image_resize($image, 500, 500), - 'popup' => image_resize($image, 800, 800), - 'thumb' => image_resize($image, 150, 150), + 'preview' => image_resize($image,500,500), + 'popup' => image_resize($image,800,800), + 'thumb' => image_resize($image,150,150), ]; - }, $this->images ?? []), + },$this->images ?? []), 'attributes' => $attributes, 'variables' => $this->decodeVariables($this->variables), 'skus' => SkuDetail::collection($this->skus)->jsonSerialize(), 'in_wishlist' => $this->inCurrentWishlist->id ?? 0, - 'active' => (bool) $this->active, + 'active' => (bool)$this->active, 'price_setting' => $this->price_setting ?? '', 'minimum_order' => $this->minimum_order ?? 0, + 'sales_method' => $this->sales_method ?? 'piece', + 'piece_to_batch' => $this->piece_to_batch ?? 1, 'numPrices' => NumPricesDetail::collection($this->numprices)->jsonSerialize() ?? '', ]; } diff --git a/resources/beike/shop/default/js/app.js b/resources/beike/shop/default/js/app.js index 9776a8b2..93ed5f01 100644 --- a/resources/beike/shop/default/js/app.js +++ b/resources/beike/shop/default/js/app.js @@ -33,4 +33,4 @@ $(document).ready(function ($) { const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl)) }); -bk.getCarts(); // 页面初始加载购物车数据 \ No newline at end of file +bk.getCarts(); // 页面初始加载购物车数据 diff --git a/resources/lang/en/product.php b/resources/lang/en/product.php index 01e74f3b..035b7434 100644 --- a/resources/lang/en/product.php +++ b/resources/lang/en/product.php @@ -37,4 +37,8 @@ return [ 'one_batch_is_equal_to' => 'one batch is equal to', 'piece' => 'piece', 'batches' => 'batches', + 'pieces_per_batch' => ':num :unit per batch', + 'sales_method_piece_unit' => 'Sell by :unit', + 'total_num' => ':num :unit in total', + 'multiple_error' => 'The purchase quantity must be a multiple of :num', ]; diff --git a/resources/lang/zh_cn/product.php b/resources/lang/zh_cn/product.php index 8bfa4fbb..b7ac2a06 100644 --- a/resources/lang/zh_cn/product.php +++ b/resources/lang/zh_cn/product.php @@ -38,4 +38,8 @@ return [ 'one_batch_is_equal_to' => '1批等于', 'piece' => '件', 'batches' => '批', + 'pieces_per_batch' => '每批:num:unit', + 'sales_method_piece_unit' => '按:unit卖', + 'total_num' => '共:num:unit', + 'multiple_error' => '采购数量必须是:num的倍数', ]; diff --git a/themes/default/product/product.blade.php b/themes/default/product/product.blade.php index bcd554a6..b6ca6f7c 100644 --- a/themes/default/product/product.blade.php +++ b/themes/default/product/product.blade.php @@ -231,7 +231,21 @@ @endhookwrapper