tSearchQuery, 'orderby' => 'title', 'order' => 'ASC', ]; $woocommerceProducts = new \WP_Query($args); $woocommerceProducts = $woocommerceProducts->get_posts(); /** @var \WP_Post[] $woocommerceProducts */ if (empty($woocommerceProducts)) { $this->loggerFactory->getLogger(self::SLUG)->info( 'no products found', ['search_query' => $productSearchQuery] ); return; } $woocommerceProducts = array_map(function($product) { return [ 'id' => $product->ID, 'name' => $product->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps ]; }, $woocommerceProducts); return $woocommerceProducts; } public function scheduleEmailWhenProductIsPurchased($orderId) { $orderDetails = $this->helper->wcGetOrder($orderId); if (!$orderDetails || !$orderDetails->get_billing_email()) { $this->loggerFactory->getLogger(self::SLUG)->info( 'Email not scheduled because the order customer was not found', ['order_id' => $orderId] ); return; } $customerEmail = $orderDetails->get_billing_email(); $subscriber = $this->subscribersRepository->getWooCommerceSegmentSubscriber($customerEmail); if (!$subscriber instanceof SubscriberEntity) { $this->loggerFactory->getLogger(self::SLUG)->info( 'Email not scheduled because the customer was not found as WooCommerce list subscriber', ['order_id' => $orderId, 'customer_email' => Helpers::maskEmail($customerEmail)] ); return; } $orderedProducts = array_map(function($product) { return ($product instanceof \WC_Order_Item_Product) ? $product->get_product_id() : null; }, $orderDetails->get_items()); $orderedProducts = array_values(array_filter($orderedProducts)); $schedulingCondition = function(NewsletterEntity $automaticEmail) use ($orderedProducts, $subscriber) { $matchedProducts = $this->getProductIdsMatchingNewsletterTrigger($automaticEmail, $orderedProducts); if (empty($matchedProducts)) { return false; } if ($this->repository->wasScheduledForSubscriber((int)$automaticEmail->getId(), (int)$subscriber->getId())) { $sentAllProducts = $this->repository->alreadySentAllProducts((int)$automaticEmail->getId(), (int)$subscriber->getId(), 'orderedProducts', $matchedProducts); if ($sentAllProducts) return false; } return true; }; $this->loggerFactory->getLogger(self::SLUG)->info( 'Email scheduled', [ 'order_id' => $orderId, 'customer_email' => Helpers::maskEmail($customerEmail), 'subscriber_id' => $subscriber->getId(), ] ); return $this->scheduler->scheduleAutomaticEmail( WooCommerce::SLUG, self::SLUG, $schedulingCondition, $subscriber, ['orderedProducts' => $orderedProducts], [$this, 'metaModifier'] ); } public function metaModifier(NewsletterEntity $newsletter, array $meta): array { $orderedProductIds = $meta['orderedProducts'] ?? null; if (empty($orderedProductIds)) { return $meta; } $meta['orderedProducts'] = $this->getProductIdsMatchingNewsletterTrigger($newsletter, $orderedProductIds); return $meta; } private function getProductIdsMatchingNewsletterTrigger(NewsletterEntity $automaticEmail, array $orderedProductIds): array { $automaticEmailMetaValue = $automaticEmail->getOptionValue(NewsletterOptionFieldEntity::NAME_META); $optionValue = Helpers::isJson($automaticEmailMetaValue) ? json_decode($automaticEmailMetaValue, true) : $automaticEmailMetaValue; if (!is_array($optionValue) || empty($optionValue['option']) || !is_array($optionValue['option'])) { return []; } $emailTriggeringProductIds = array_column($optionValue['option'], 'id'); return array_intersect($emailTriggeringProductIds, $orderedProductIds); } }