triggerName = 'fluent_cart/order_paid_done'; $this->actionArgNum = 3; $this->priority = 20; parent::__construct(); } public function getBlock() { return [ 'title' => __('Order Paid (Payment/Subscription)', 'fluent-crm'), 'description' => __('This will run once new order will be placed as paid in FluentCRM', 'fluent-crm'), 'svg' => '', 'settings' => [ 'product_ids' => [], 'product_categories' => [], 'type' => 'required', 'can_enter' => 'yes' ] ]; } public function getDefaultSettings() { return [ 'product_ids' => [], 'product_categories' => [], 'type' => 'required', 'can_enter' => 'yes' ]; } public function getBlockFields($funnel) { return [ 'title' => __('New Order Paid in FluentCart', 'fluent-crm'), 'sub_title' => __('This will run once new order will be placed as paid in FluentCart', 'fluent-crm'), 'fields' => [ 'product_ids' => [ 'type' => 'rest_selector', 'label' => __('Target Products', 'fluent-crm'), 'option_key' => 'fluent_cart_products', 'is_multiple' => true, 'help' => __('Select for which products this automation will run', 'fluent-crm'), 'inline_help' => __('Keep it blank to run to any product purchase', 'fluent-crm') ], 'product_categories' => [ 'type' => 'rest_selector', 'label' => __('Or Target Product Categories', 'fluent-crm'), 'option_key' => 'fluent_cart_product_categories', 'is_multiple' => true, 'help' => __('Select for which product category the automation will run', 'fluent-crm'), 'inline_help' => __('Keep it blank to run to any category products', 'fluent-crm') ], 'type' => $this->benchmarkTypeField(), 'can_enter' => $this->canEnterField() ] ]; } public function handle($benchMark, $originalArgs) { $orderData = $originalArgs[0] ?? []; $order = Arr::get($orderData, 'order', []); $customer = Arr::get($orderData, 'customer', []); $settings = $benchMark->settings;; if (!$this->checkConditions($settings, $order)) { return; } $subscriberData = CartHelper::prepareSubscriberData($customer); if (!is_email($subscriberData['email'])) { return; } $subscriberData['status'] = 'subscribed'; $subscriber = FunnelHelper::createOrUpdateContact($subscriberData); $funnelProcessor = new FunnelProcessor(); $funnelProcessor->startFunnelFromSequencePoint($benchMark, $subscriber, [], [ 'benchmark_value' => $order->total_paid, // converted to cents 'benchmark_currency' => $order->currency, ]); } private function checkConditions($conditions, $order) { $orderItems = Arr::get($order, 'order_items', []); // Post IDs of ordered products are the product IDs in FluentCart $orderedProductIds = []; foreach ($orderItems as $item) { $productId = $item->post_id; if ($productId) { $orderedProductIds[] = $productId; } } $orderProductCategories = CartHelper::getProductCategoriesByIds($orderedProductIds); $selectedProductIds = Arr::get($conditions, 'product_ids', []); $selectedProductCategories = Arr::get($conditions, 'product_categories', []); // If no products or categories are selected, return true if (empty($selectedProductIds) && empty($selectedProductCategories)) { return true; } // Check for matches in product IDs and categories $productMatch = !empty($selectedProductIds) && !empty(array_intersect($selectedProductIds, $orderedProductIds)); $categoryMatch = !empty($selectedProductCategories) && !empty(array_intersect($selectedProductCategories, $orderProductCategories)); // Return true if either matches return $productMatch || $categoryMatch; } }