diff --git a/src/Docs/V1/Admin/Controllers/Marketing/Promotions/CartRuleController.php b/src/Docs/V1/Admin/Controllers/Marketing/Promotions/CartRuleController.php index ff0ec1f..b16c2fa 100644 --- a/src/Docs/V1/Admin/Controllers/Marketing/Promotions/CartRuleController.php +++ b/src/Docs/V1/Admin/Controllers/Marketing/Promotions/CartRuleController.php @@ -648,7 +648,7 @@ public function getConditionAttributes() /** * @OA\Post( - * path="/api/v1/admin/promotions/cart-rules/{id}/create-product-quantity-rule", + * path="/api/v1/admin/promotions/cart-rules/{id}/product-quantity-rules", * operationId="createProductQuantityRule", * tags={"CartRules"}, * summary="Create product quantity rule", @@ -684,15 +684,18 @@ public function getConditionAttributes() * type="object", * description="Rules", * example={{ - * "value": "2", - * "operator": "==", - * "attribute": { - * "cart_item|item_qty", - * "product|attribute_family_id" - }, - * "attribute_type": "integer", - * "action_type": "by_percent", - * "price": "10.50" + * "id": 0, + * "action_tye": "by_percent", + * "price": "10.50", + * "attributes": [{ + * "attribute": "cart_item|item_qty", + * "operator": "==", + * "value": "2" + * },{ + * "attribute": "product|attribute_family_id", + * "operator": "==", + * "value": "1" + * }] * }}, * ), * required={"product_id", "quantity"} diff --git a/src/Http/Controllers/Api/V1/Admin/Marketing/Promotions/CartRuleController.php b/src/Http/Controllers/Api/V1/Admin/Marketing/Promotions/CartRuleController.php index 3cdfb29..d52d7ea 100644 --- a/src/Http/Controllers/Api/V1/Admin/Marketing/Promotions/CartRuleController.php +++ b/src/Http/Controllers/Api/V1/Admin/Marketing/Promotions/CartRuleController.php @@ -7,6 +7,7 @@ use Webkul\CartRule\Repositories\CartRuleRepository; use NexaMerchant\Apis\Http\Controllers\Api\V1\Admin\Marketing\MarketingController; use NexaMerchant\Apis\Http\Resources\Api\V1\Admin\Marketing\Promotions\CartRuleResource; +use Illuminate\Support\Facades\Redis; class CartRuleController extends MarketingController { @@ -303,8 +304,6 @@ public function createProductQuantityRule($product_id, Request $request){ ], 404); } - $product_family_id = $product->attribute_family_id; - // create a new cart rule for the product quantity $request->validate([ 'product_id' => 'required|integer', @@ -313,28 +312,64 @@ public function createProductQuantityRule($product_id, Request $request){ $cartRule = app(\Webkul\CartRule\Repositories\CartRuleRepository::class); - $cartRuleData = [ - 'name' => $product->name, - 'description' => $product->description, - 'channels' => [1], - 'customer_groups' => [1], - 'coupon_type' => 0, - 'use_auto_generation' => 1, - 'starts_from' => null, - 'ends_till' => null, - 'action_type' => 1, - 'discount_amount' => 0, - ]; + $rules = $request->rules; + + foreach($rules as $rule){ + + $cartRuleData = [ + 'name' => $product->name . $rule['action_type']. $rule['price'], + 'starts_from' => null, + 'ends_till' => null, + 'action_type' => $rule['action_type'], + 'discount_amount' => $rule['price'], + 'end_other_rules' => 1, + 'coupon_type' => 0, + 'use_auto_generation' => 0, + 'discount_step' => 0, + 'channels' => [1], + 'customer_groups' => [1], + ]; - // need match product quantity and product family id for this rule and create it. - Event::dispatch('promotions.cart_rule.create.before'); + $rulesAttributes = $rule['attributes']; - $cartRule = $this->getRepositoryInstance()->create($request->all()); + //var_dump($rule['attributes']);exit; - Event::dispatch('promotions.cart_rule.create.after', $cartRule); + foreach($rulesAttributes as $key=>$attribute){ + $cartRuleData['conditions'][] = [ + 'attribute' => $attribute['attribute'], + 'operator' => $attribute['operator'], + "attribute_type" => "integer", + 'value' => $attribute['value'], + ]; + } + + $id = $rule['id']; + + if($id > 0) { + + // update the rule + $cartRule = $this->getRepositoryInstance()->findOrFail($id); + + Event::dispatch('promotions.cart_rule.update.before', $id); + + $cartRule = $this->getRepositoryInstance()->update($cartRuleData, $id); + + Event::dispatch('promotions.cart_rule.update.after', $cartRule); + + + }else{ + Event::dispatch('promotions.cart_rule.create.before'); + + $cartRule = $this->getRepositoryInstance()->create($cartRuleData); + + Event::dispatch('promotions.cart_rule.create.after', $cartRule); + } + + Redis::sadd('product-quantity-rules-'.$product_id, $cartRule->id); + + } return response([ - 'data' => new CartRuleResource($cartRule), 'message' => trans('Apis::app.admin.marketing.promotions.cart-rules.create-success'), ]); @@ -346,9 +381,13 @@ public function createProductQuantityRule($product_id, Request $request){ * * @param int $product_id * + * @return \Illuminate\Http\Response * */ - public function getProductQuantityRules($product_id){ + public function getProductQuantityRules($product_id, Request $request){ + + + $product = app(\Webkul\Product\Repositories\ProductRepository::class)->findOrFail($product_id); if(!$product){ return response()->json([ @@ -356,9 +395,13 @@ public function getProductQuantityRules($product_id){ ], 404); } - $rules = app(\Webkul\CartRule\Repositories\CartRuleRepository::class)->findWhere([ - 'product_id' => $product_id - ]); + + // get all the rules for the product quantity + + $rules = Redis::smembers('product-quantity-rules-'.$product_id); + + $rules = $this->getRepositoryInstance()->findWhereIn('id', $rules); + return response()->json([ 'data' => CartRuleResource::collection($rules) diff --git a/src/Routes/V1/Admin/marketing-routes.php b/src/Routes/V1/Admin/marketing-routes.php index 970e245..d81e0d8 100644 --- a/src/Routes/V1/Admin/marketing-routes.php +++ b/src/Routes/V1/Admin/marketing-routes.php @@ -55,7 +55,11 @@ Route::delete('{id}', 'destroy'); - Route::post('{id}/create-product-quantity-rule', 'createProductQuantityRule'); + Route::post('{id}/product-quantity-rules', 'createProductQuantityRule'); + + Route::get('{id}/product-quantity-rules', 'getProductQuantityRules'); + + });