Skip to content

Commit

Permalink
fix update
Browse files Browse the repository at this point in the history
  • Loading branch information
xxl4 committed Dec 18, 2024
1 parent 4ada414 commit 3fbc8df
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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',
Expand All @@ -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'),
]);

Expand All @@ -346,19 +381,27 @@ 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([
'message' => trans('admin::app.marketing.promotions.cart-rules.product-not-found')
], 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)
Expand Down
6 changes: 5 additions & 1 deletion src/Routes/V1/Admin/marketing-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');




});
Expand Down

0 comments on commit 3fbc8df

Please sign in to comment.