Skip to content

Commit 9a93ea5

Browse files
committed
feat: wooCommerce add to cart trigger added
1 parent f725611 commit 9a93ea5

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

includes/Triggers/WC/Hooks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
Hooks::add('woocommerce_checkout_order_processed', [WCController::class, 'handle_variable_product_order'], 10, 2);
2525
Hooks::add('woocommerce_update_coupon', [WCController::class, 'handle_coupon_created'], 10, 2);
26+
Hooks::add('woocommerce_add_to_cart', [WCController::class, 'handle_add_to_cart'], 10, 6);
2627

2728
// Secondary hook form order create checkout
2829
Hooks::add('woocommerce_store_api_checkout_order_processed', [WCController::class, 'handle_order_checkout'], 10, 1);

includes/Triggers/WC/WCController.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,17 +507,43 @@ public static function handle_coupon_created($couponId, $coupon)
507507

508508
$flows = Flow::exists('WC', static::NEW_COUPON_CREATED);
509509
if (empty($flows)) {
510-
return false;
510+
return;
511511
}
512512

513513
$couponData = WCHelper::getCouponData($couponId, $coupon);
514514
Flow::execute('WC', static::NEW_COUPON_CREATED, $couponData, $flows);
515515
}
516516

517+
public static function handle_add_to_cart($cartItemKey, $productId, $quantity, $variationId, $variation, $cartItemData)
518+
{
519+
if (empty($cartItemKey) || empty($productId)) {
520+
return;
521+
}
522+
523+
$flows = Flow::exists('WC', static::PRODUCT_ADD_TO_CART);
524+
if (empty($flows)) {
525+
return;
526+
}
527+
528+
$cart = WC()->cart;
529+
$cartData = [
530+
'cart_item_key' => $cartItemKey,
531+
'product_id' => $productId,
532+
'quantity' => $quantity,
533+
'variation_id' => $variationId,
534+
'variation' => $variation,
535+
'cart_item_data' => $cartItemData,
536+
'cart_total' => $cart->get_cart_contents_total(),
537+
'cart_line_items' => WCHelper::getCartLineItems(),
538+
];
539+
540+
Flow::execute('WC', static::PRODUCT_ADD_TO_CART, $cartData, $flows);
541+
}
542+
517543
public static function handle_order_create($order_id, $fields)
518544
{
519545
if (!static::isActivate()) {
520-
return false;
546+
return;
521547
}
522548

523549
$data = WCHelper::processOrderData($order_id);

includes/Triggers/WC/WCHelper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,29 @@ public static function getCouponData($couponId, $coupon)
166166
];
167167
}
168168

169+
public static function getCartLineItems()
170+
{
171+
$cart = WC()->cart;
172+
$cartLineItems = array_map(
173+
function ($cartItem) {
174+
return [
175+
'product' => $cartItem['data'],
176+
'product_id' => $cartItem['product_id'],
177+
'variation_id' => $cartItem['variation_id'],
178+
'quantity' => $cartItem['quantity'],
179+
'product_name' => $cartItem['data']->get_name(),
180+
'tax_class' => $cartItem['data']->get_tax_class(),
181+
'tax_status' => $cartItem['data']->get_tax_status(),
182+
'product_sku' => $cartItem['data']->get_sku(),
183+
'product_unit_price' => $cartItem['data']->get_price(),
184+
];
185+
},
186+
$cart->get_cart()
187+
);
188+
189+
return wp_json_encode(array_values($cartLineItems));
190+
}
191+
169192
public static function processProductData($postId)
170193
{
171194
$product = wc_get_product($postId);

0 commit comments

Comments
 (0)