schedule(); if ( count( $this->settingsDTO->getPixels() ) < 1 ) { return; } $this->setupEventListeners(); } private function schedule() { $actionName = 'generate_adsmurai_feed'; add_action( $actionName, array( $this, 'generateProductFeed' ) ); if ( ! wp_next_scheduled( $actionName ) ) { wp_schedule_event( time(), 'daily', $actionName ); } } private function getPluginPath() { $pluginPath = plugin_dir_path( __FILE__ ); $tmp = explode("/", $pluginPath); $tmp[count($tmp) - 2] = "woocommerce"; return join("/", $tmp); } private function setupEventListeners() { $events = array( array( 'action' => 'woocommerce_thankyou', 'method' => 'onPurchase', 'name' => EventDTO::EVENT_PURCHASE, ), array( 'action' => 'woocommerce_add_to_cart', 'method' => 'addToCart', 'name' => EventDTO::EVENT_ADD_TO_CART, ), array( 'action' => 'woocommerce_ajax_added_to_cart', 'method' => 'addToCart', 'name' => EventDTO::EVENT_ADD_TO_CART, ), array( 'action' => 'woocommerce_checkout_init', 'method' => 'onCheckoutStart', 'name' => EventDTO::EVENT_INITIATE_CHECKOUT, ), array( 'action' => 'template_redirect', 'method' => 'onItemView', 'name' => EventDTO::EVENT_VIEW_CONTENT, ), ); foreach ( $events as $event ) { if ( $this->isEventEnabled( $event['name'] ) ) { add_action( $event['action'], array( $this, $event['method'] ), 10, 1 ); } } } function onCheckoutStart( $checkout ) { global $woocommerce; if ( empty( $woocommerce->cart ) ) { return; } $dto = new EventDTO( EventDTO::EVENT_INITIATE_CHECKOUT ); $dto->setCurrency( get_woocommerce_currency() ); $this->fillFromCart( $dto ); $dto->userData->autoFillData( $checkout->get_posted_data() ); if ( $dto->getValue() <= 0 ) { return; } $this->sendEvent( $dto ); } private function fillFromCart( $dto ) { global $woocommerce; if ( empty( $woocommerce->cart ) ) { return; } $contentIds = array(); $contents = array(); foreach ( $woocommerce->cart->cart_contents as $product ) { $contentIds[] = $product['product_id']; $contentDTO = ( new ContentDTO() ) ->setId( $product['product_id'] ) // $product['data']->get_sku() ->setQuantity( $product['quantity'] ) ->setItemPrice( $product['data']->get_price() ); $contents[] = $contentDTO; } $dto->setContents( $contents ); $dto->setContentIds( $contentIds ); $dto->setValue( $woocommerce->cart->get_totals()['total'] ); return $this; } function onItemView() { if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; } global $post; if ( empty( $post->ID ) ) { return; } $productId = $post->ID; $product = wc_get_product( $productId ); $dto = new EventDTO( EventDTO::EVENT_VIEW_CONTENT ); $dto->setCurrency( get_woocommerce_currency() ) ->setContentIds( array( $productId ) ) ->setValue( $product->get_price() ) ->userData->autoFillData( array() ); $contentDTO = ( new ContentDTO() ) ->setId( $productId ) ->setQuantity( 1 ) ->setItemPrice( $product->get_price() ); $dto->setContents( array( $contentDTO ) ); $this->sendEvent( $dto ); } function onPurchase( $order_id ) { $order = wc_get_order( $order_id ); $data = $order->get_data(); $dto = new EventDTO( EventDTO::EVENT_PURCHASE ); $dto->setCurrency( get_woocommerce_currency() ) ->setValue( $order->get_total() ) ->setOrderId( $order_id ); $dto->userData->autoFillData( ! empty( $data['billing'] ) ? $data['billing'] : $data['shipping'] ); $contentIds = array(); $contents = array(); foreach ( $data['line_items'] as $product ) { $contentIds[] = $product->get_product_id(); $contentDTO = ( new ContentDTO() ) ->setId( $product->get_product_id() ) // $product['data']->get_sku() ->setQuantity( $product->get_quantity() ) ->setItemPrice( $product->get_total() ); $contents[] = $contentDTO; } $dto->setContents( $contents ); $dto->setContentIds( $contentIds ); $this->sendEvent( $dto ); } function addToCart( $cartId ) { $dto = new EventDTO( EventDTO::EVENT_ADD_TO_CART ); $dto->setCurrency( get_woocommerce_currency() ); $this->fillFromCart( $dto ); $dto->userData->autoFillData( array() ); $this->sendEvent( $dto ); } public function getProductFeedUrl() { return wp_upload_dir()['baseurl'] . '/' . self::UPLOAD_FOLDER_NAME . '/feed.jsonl'; } public function generateProductFeed() { $uploadFolder = wp_upload_dir()['basedir'] . '/' . self::UPLOAD_FOLDER_NAME; $url = $this->getProductFeedUrl(); $file = $uploadFolder . '/feed.jsonl'; if ( ! file_exists( $uploadFolder ) ) { wp_mkdir_p( $uploadFolder ); } if ( file_exists( $file ) && gmdate( 'Y-m-d', filemtime( $file ) ) == gmdate( 'Y-m-d' ) ) { return $url; } $functionsFile = $this->getPluginPath() . 'includes/wc-product-functions.php'; if ( ! function_exists( 'wc_get_products' ) && file_exists( $functionsFile ) ) { include_once $functionsFile; } if ( ! function_exists( 'wc_get_products' ) ) { return ''; } file_put_contents( $file, '' ); $args = array( 'limit' => -1, // Get all products ); $products = wc_get_products( $args ); foreach ( $products as $product ) { // Get the product images $image_ids = $product->get_gallery_image_ids(); $image_ids[] = $product->get_image_id(); $images = array(); foreach ( $image_ids as $image_id ) { $images[] = wp_get_attachment_url( $image_id ); } $categories = get_the_terms( $product->get_id(), 'product_cat' ); $categoryNames = array(); foreach ( $categories as $category ) { $categoryNames[] = $category->name; } $brands = $this->getProductBrands( $product->get_id() ); $formattedProduct = array( 'id' => $product->get_id(), 'title' => $product->get_name(), 'description' => $product->get_description(), 'price' => ( $product->get_regular_price() ? $product->get_regular_price() : $product->get_price() ) . ' ' . get_woocommerce_currency(), 'sale_price' => $product->get_sale_price() . ' ' . get_woocommerce_currency(), 'sku' => $product->get_sku(), 'availability' => strpos( $product->get_stock_status(), 'out' ) === false ? 'out of stock' : 'in stock', 'images' => $images, 'category_ids' => $product->get_category_ids(), 'category_names' => $categoryNames, 'link' => get_permalink( $product->get_id() ), ); if ( ! empty( $brands['names'] ) ) { $formattedProduct['brand'] = $brands['names'][0]; $formattedProduct['brand_id'] = $brands['ids'][0]; } file_put_contents( $file, json_encode( $formattedProduct ) . "\n", FILE_APPEND ); } return $url; } private function getProductBrands( $productId ) { $taxonomies = array( 'product_brand', 'yith_product_brand', 'pa_brand', ); $output = array( 'ids' => array(), 'names' => array(), ); foreach ( $taxonomies as $taxonomy ) { $brands = wp_get_post_terms( $productId, $taxonomy, array( 'fields' => 'all' ) ); if ( ! empty( $brands ) ) { foreach ( $brands as $brand ) { if ( empty( $brand->term_id ) || empty( $brand->name ) ) { continue; } $output['ids'][] = $brand->term_id; $output['names'][] = $brand->name; } break; } } return $output; } } }