cts->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' => $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' => $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'])) { return []; } $emailTriggeringProductIds = array_column($optionValue['option'], 'id'); return array_intersect($emailTriggeringProductIds, $orderedProductIds); } }
Fatal error: Uncaught Error: Class "MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedProduct" not found in /htdocs/wp-content/plugins/mailpoet/generated/FreeCachedContainer.php:1401 Stack trace: #0 /htdocs/wp-content/plugins/mailpoet/vendor-prefixed/symfony/dependency-injection/Container.php(122): MailPoetGenerated\FreeCachedContainer->getPurchasedProductService() #1 /htdocs/wp-content/plugins/mailpoet/vendor-prefixed/symfony/dependency-injection/Container.php(110): MailPoetVendor\Symfony\Component\DependencyInjection\Container->make('MailPoet\\Automa...', 1) #2 /htdocs/wp-content/plugins/mailpoet/lib/DI/ContainerWrapper.php(39): MailPoetVendor\Symfony\Component\DependencyInjection\Container->get('MailPoet\\Automa...') #3 /htdocs/wp-content/plugins/mailpoet/lib/AutomaticEmails/WooCommerce/WooCommerceEventFactory.php(37): MailPoet\DI\ContainerWrapper->get('MailPoet\\Automa...') #4 /htdocs/wp-content/plugins/mailpoet/lib/AutomaticEmails/WooCommerce/WooCommerce.php(82): MailPoet\AutomaticEmails\WooCommerce\WooCommerceEventFactory->createEvent('PurchasedProduc...') #5 /htdocs/wp-includes/class-wp-hook.php(324): MailPoet\AutomaticEmails\WooCommerce\WooCommerce->setupEvents(Array) #6 /htdocs/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array) #7 [internal function]: apply_filters('mailpoet_woocom...', Array) #8 /htdocs/wp-content/plugins/mailpoet/lib/WP/Functions.php(65): call_user_func_array('apply_filters', Array) #9 /htdocs/wp-content/plugins/mailpoet/lib/AutomaticEmails/WooCommerce/WooCommerce.php(70): MailPoet\WP\Functions->applyFilters('mailpoet_woocom...', Array) #10 /htdocs/wp-includes/class-wp-hook.php(324): MailPoet\AutomaticEmails\WooCommerce\WooCommerce->setupGroup(Array) #11 /htdocs/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array) #12 [internal function]: apply_filters('mailpoet_automa...', Array) #13 /htdocs/wp-content/plugins/mailpoet/lib/WP/Functions.php(65): call_user_func_array('apply_filters', Array) #14 /htdocs/wp-content/plugins/mailpoet/lib/AutomaticEmails/AutomaticEmails.php(49): MailPoet\WP\Functions->applyFilters('mailpoet_automa...', Array) #15 /htdocs/wp-content/plugins/mailpoet/lib/Config/Initializer.php(560): MailPoet\AutomaticEmails\AutomaticEmails->getAutomaticEmails() #16 /htdocs/wp-content/plugins/mailpoet/lib/Config/Initializer.php(380): MailPoet\Config\Initializer->setupAutomaticEmails() #17 /htdocs/wp-includes/class-wp-hook.php(324): MailPoet\Config\Initializer->initialize('') #18 /htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #19 /htdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #20 /htdocs/wp-settings.php(704): do_action('init') #21 /htdocs/wp-config.php(85): require_once('/htdocs/wp-sett...') #22 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #23 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #24 /htdocs/index.php(17): require('/htdocs/wp-blog...') #25 {main} thrown in /htdocs/wp-content/plugins/mailpoet/generated/FreeCachedContainer.php on line 1401