get_items(); $recommend_history = ''; if ( isset( $_COOKIE['v_r_h'] ) ) { $recommend_history = sanitize_text_field( wp_unslash( @$_COOKIE['v_r_h'] ) ); } $recommend_array = explode( '_', $recommend_history ); if ( $recommend_array ) { $aiprreli_productsNumFromRecommend = 0; $aiprreli_purchasedMoneyFromRecommend = 0; foreach ( $items as $item ) { $product_name = $item->get_name(); $product_id = $item->get_product_id(); foreach ( $recommend_array as $recommend ) { $visited_item_array = explode( '>', $recommend ); if ( $product_id == $visited_item_array[0] ) { // maybe need identification of date. $product = $item->get_product(); $active_price = $product->get_price(); $item_quantity = $item->get_quantity(); $aiprreli_purchasedMoneyFromRecommend += $active_price * $item_quantity; ++$aiprreli_productsNumFromRecommend; break; } } } $order->update_meta_data( 'aiprreli_productsNumFromRecommend', $aiprreli_productsNumFromRecommend ); $order->update_meta_data( 'aiprreli_purchasedMoneyFromRecommend', $aiprreli_purchasedMoneyFromRecommend ); $order->save(); } } add_filter( 'woocommerce_before_single_product', function () { $aiprreli_insert_productPage = get_option( 'aiprreli_insert_productPage' ); $aiprreli_hideRelatedProducts = get_option( 'aiprreli_hideRelatedProducts' ); if ( $aiprreli_insert_productPage && $aiprreli_hideRelatedProducts ) { remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); } } ); add_action( 'woocommerce_after_single_product', function () { $aiprreli_insert_productPage = get_option( 'aiprreli_insert_productPage' ); if ( $aiprreli_insert_productPage ) { $aiprreli_shortchode = esc_html( get_option( 'PROBOT-rating' ) ); $shortcodeStr = '[PROBOT_shortcode rating="' . $aiprreli_shortchode . '"]'; if ( shortcode_exists( 'PROBOT_shortcode' ) ) { echo '

Recommend Products

'; echo do_shortcode( $shortcodeStr ); } } } ); /** * Plugin's Enqueue front file. */ function aiprreli_sty_add() { wp_enqueue_script( 'PROBOT-front-script', AIPRRELI_PROBOT_URL . 'front_assets/js/swiper-bundle.min.js', array(), '1.0.0', true ); wp_enqueue_script( 'PROBOT-script', AIPRRELI_PROBOT_URL . 'functions/assets/js/PROBOT-script.js', array(), '1.0.0', true ); wp_enqueue_style( 'PROBOT-swiper-style', AIPRRELI_PROBOT_URL . 'front_assets/css/swiper-bundle.min.css' ); wp_enqueue_style( 'PROBOT-style', AIPRRELI_PROBOT_URL . 'functions/assets/css/PROBOT-style.css' ); wp_enqueue_style( 'PROBOT-front-style', AIPRRELI_PROBOT_URL . 'front_assets/css/front-style.css', array(), '1.0.0' ); } /** * Plugin's Frontend Script. */ function aiprreli_script_add() { $script_SliderData = array( 'aiprreli_num_products_desktop' => esc_js( get_option( 'aiprreli_num_products_desktop' ) ), 'aiprreli_num_products_tablet' => esc_js( get_option( 'aiprreli_num_products_tablet' ) ), 'aiprreli_num_products_mobile' => esc_js( get_option( 'aiprreli_num_products_mobile' ) ), ); if ( get_option( 'aiprreli_enable_loop_slider' ) == 1 ) { $script_SliderData['aiprreli_enable_loop_slider'] = '1'; } else { $script_SliderData['aiprreli_enable_loop_slider'] = '0'; } wp_localize_script( 'PROBOT-script', 'PROBOT_SliderData', $script_SliderData ); // if product page. if ( is_product() ) { global $post; $postId = $post->ID; $terms = ( get_option( 'aiprreli_suggestType' ) == 2 ) ? wp_get_post_terms( $postId, 'product_tag', array( 'fields' => 'ids', ) ) : wp_get_post_terms( $postId, 'product_cat', array( 'fields' => 'ids', ) ); $tags_cookie = isset( $_COOKIE['AIPRRELI_Tags'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['AIPRRELI_Tags'] ) ) : ''; $categories_cookie = isset( $_COOKIE['AIPRRELI_Categories'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['AIPRRELI_Categories'] ) ) : ''; $cats = ( get_option( 'aiprreli_suggestType' ) == 2 ) ? sanitize_text_field( $tags_cookie ) : sanitize_text_field( $categories_cookie ); $visited_history = ''; $recommend_history = ''; if ( isset( $_COOKIE['v_p_h'] ) ) { $visited_history = sanitize_text_field( wp_unslash( @$_COOKIE['v_p_h'] ) ); } if ( isset( $_COOKIE['v_r_h'] ) ) { $recommend_history = sanitize_text_field( wp_unslash( @$_COOKIE['v_r_h'] ) ); } $newVisited_history = ''; if ( $visited_history ) { $visited_array = explode( '_', $visited_history ); $first_flag = true; $replaceFlag = false; foreach ( $visited_array as $visited_item ) { $visited_item_array = explode( '>', $visited_item ); $newVisitedItem = $visited_item; if ( $postId == $visited_item_array[0] ) { $updated_num = (int) ( $visited_item_array[1] ) + 1; $newVisitedItem = $visited_item_array[0] . '>' . $updated_num; $replaceFlag = true; } if ( $first_flag ) { $newVisited_history = $newVisitedItem; } else { $newVisited_history .= '_' . $newVisitedItem; } $first_flag = false; } if ( ! $replaceFlag ) { $newVisited_history = '_' . $postId . '>1'; } } else { $newVisited_history = $postId . '>1'; } $new_recommend_history = ''; if ( array_key_exists( 'recom', $_GET ) && 1 == $_GET['recom'] ) { $product = wc_get_product( $postId ); $aiprreli_clickedNumFromRecommendLinks = $product->get_meta( 'aiprreli_clickedNumFromRecommendLinks' ); $newNum = 0; if ( $aiprreli_clickedNumFromRecommendLinks ) { $newNum = (int) $aiprreli_clickedNumFromRecommendLinks + 1; } else { ++$newNum; } update_post_meta( $postId, 'aiprreli_clickedNumFromRecommendLinks', $newNum ); $today = gmdate( 'Y-m-d' ); if ( $recommend_history ) { global $wpdb; $table_name_qa = $wpdb->prefix . 'log_click_recommend'; $wpdb->insert( $table_name_qa, array( 'ID' => null, 'clickedTime' => current_time( 'mysql' ), 'productId' => $postId, ) ); $recommend_array = explode( '_', $recommend_history ); $first_flag = true; $replaceFlag = false; $newVisitedItem = ''; foreach ( $recommend_array as $visited_item ) { $visited_item_array = explode( '>', $visited_item ); $newVisitedItem = $visited_item; if ( $postId == $visited_item_array[0] ) { $newVisitedItem = $visited_item_array[0] . '>' . $today; $replaceFlag = true; } if ( $first_flag ) { $new_recommend_history = $newVisitedItem; } else { $new_recommend_history .= '_' . $newVisitedItem; } $first_flag = false; } if ( ! $replaceFlag ) { $new_recommend_history .= '_' . $postId . '>' . $today; } } else { $new_recommend_history .= $postId . '>' . $today; } } $catsName = ( get_option( 'aiprreli_suggestType' ) == 2 ) ? 2 : 1; $cookieTime = ( ! empty( get_option( 'aiprreli_suggestTime' ) ) ) ? get_option( 'aiprreli_suggestTime' ) : 2629743; $aiprreli_idLimit = ( ! empty( get_option( 'aiprreli_idLimit' ) ) ) ? get_option( 'aiprreli_idLimit' ) : 10; $script_data = array( 'IDType' => $catsName, 'cookieTime' => $cookieTime, 'idLimit' => $aiprreli_idLimit, 'defaultID' => $terms ? : array(), 'readyID' => $cats ? : 'undefined', 'visited_history' => isset($newVisited_history) ? esc_js($newVisited_history) : 'undefined', 'recommend_history' => isset($new_recommend_history) ? esc_js($new_recommend_history) : 'undefined', ); wp_localize_script( 'PROBOT-script', 'customData', $script_data ); $inline_script = "document.addEventListener('DOMContentLoaded', function(event) { categoryRun(customData.IDType, customData.defaultID, customData.readyID, customData.cookieTime, customData.idLimit, customData.visited_history, customData.recommend_history); });"; wp_add_inline_script( 'PROBOT-script', $inline_script, 'after' ); } } add_action( 'wp_footer', 'aiprreli_script_add' ); add_action( 'wp_enqueue_scripts', 'aiprreli_sty_add' ); /** * Get rating function. * * @param string $show_rating Field name attribute. * @param string $layout Field id attribute. * * @return string Input field box shadow HTML */ function aiprreli_get_suggest_loop( $show_rating ) { if ( ! is_admin() ) { // Query Arguments. $getCookie = array(); if ( get_option( 'aiprreli_suggestType' ) == 2 ) { if ( isset( $_COOKIE['AIPRRELI_Tags'] ) ) { $getCookie = @json_decode( sanitize_text_field( wp_unslash( $_COOKIE['AIPRRELI_Tags'] ) ) ); } $taxonomy1 = 'product_tag'; $taxonomy2 = 'exclude-from-tag'; } elseif ( get_option( 'aiprreli_suggestType' ) == 1 ) { if ( isset( $_COOKIE['AIPRRELI_Categories'] ) ) { $getCookie = @json_decode( sanitize_text_field( wp_unslash( $_COOKIE['AIPRRELI_Categories'] ) ) ); } $taxonomy1 = 'product_cat'; $taxonomy2 = 'exclude-from-catalog'; } // purch check. if ( ! empty( get_option( 'aiprreli_purchProducts' ) ) ) { $purchCheck = get_option( 'aiprreli_purchProducts' ); } else { $purchCheck = 1; } // purch and cookie check. if ( 1 == $purchCheck && ! empty( aiprreli_pItems() ) && ! empty( $getCookie ) ) { $getCookie = array_reduce( array( $getCookie, aiprreli_pItems(), ), 'array_merge', array() ); } // cart and cookie check - ADDED 1.1.2. if ( 1 == $purchCheck && ! empty( aiprreli_cItems() ) && ! empty( $getCookie ) ) { $getCookie = array_reduce( array( $getCookie, aiprreli_cItems(), ), 'array_merge', array() ); } // if not have any cookies. if ( empty( $getCookie ) ) { $termArray = false; } else { $termArray = array( array( 'taxonomy' => $taxonomy1, 'field' => 'term_id', // This is optional, as it defaults to 'term_id'. 'terms' => $getCookie, 'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => $taxonomy2, // Possibly 'exclude-from-search' too. 'operator' => 'NOT IN', ), ); } // rating check. if ( empty( $show_rating ) ) { $getRate = 0; } else { $getRate = $show_rating; } // style check. if ( empty( $wStyle ) ) { $getStyle = 0; } else { $getStyle = $wStyle; } // posts per page check. $checkPPP = 12; // posts per column check. if ( empty( $columnCount ) ) { $colCheck = 1; } else { $colCheck = $columnCount; } $visited_history = isset( $_COOKIE['v_p_h'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['v_p_h'] ) ) : ''; $decode_visited_array = array(); if ( $visited_history ) { $visited_array = explode( '_', $visited_history ); foreach ( $visited_array as $visited_item ) { $visited_item_array = explode( '>', $visited_item ); array_push( $decode_visited_array, $visited_item_array ); } } usort( $decode_visited_array, function ( $a, $b ) { if ( $a[1] == $b[1] ) { return 0; } return ( $b[1] < $a[1] ) ? -1 : 1; } ); $m_pItems = aiprreli_p_t_Items(); $m_cItems = aiprreli_c_t_Items(); if ( $m_cItems == null ) $m_cItems = []; global $AIPRRELI_recommendIds, $AIPRRELI_addedRecommends, $AIPRRELI_carts; $AIPRRELI_carts = $m_cItems; if ( $AIPRRELI_recommendIds == null ) $AIPRRELI_recommendIds = []; if ( $m_pItems ) { foreach ( $m_pItems as $m_pItem ) { $inCartflag = false; if ( $m_cItems ) { foreach ( $m_cItems as $m_cItem ) { if ( $m_pItem['id'] == $m_cItem ) { $inCartflag = true; break; } } } ; if ( !$inCartflag && count ( $AIPRRELI_recommendIds ) < $checkPPP ) { array_push ( $AIPRRELI_recommendIds, $m_pItem['id'] ); $AIPRRELI_recommendIds = array_unique ( $AIPRRELI_recommendIds, SORT_NUMERIC ); } if ( count ( $AIPRRELI_recommendIds ) >= $checkPPP ) { break; } } } if ( is_array( $AIPRRELI_recommendIds ) && count( $AIPRRELI_recommendIds ) < $checkPPP ) { foreach ( $decode_visited_array as $decode_visited_item ) { $inCartflag = false; if ( $m_cItems ) { foreach ( $m_cItems as $m_cItem ) { if ( $decode_visited_item[0] == $m_cItem ) { $inCartflag = true; break; } } } if ( ! $inCartflag && count( $AIPRRELI_recommendIds ) < $checkPPP ) { array_push( $AIPRRELI_recommendIds, $decode_visited_item[0] ); $AIPRRELI_recommendIds = array_unique( $AIPRRELI_recommendIds, SORT_NUMERIC ); } if ( count( $AIPRRELI_recommendIds ) >= $checkPPP ) { break; } } } if ( $AIPRRELI_recommendIds || $m_cItems && is_array( $AIPRRELI_recommendIds ) && is_array( $m_cItems ) ) { $lackNum = $checkPPP - count( $AIPRRELI_recommendIds ); $averageNum = (int) ( $lackNum / ( count( $AIPRRELI_recommendIds ) + count( $m_cItems ) ) ); $averageRedisue = $lackNum % ( count( $AIPRRELI_recommendIds ) + count( $m_cItems ) ); $i = 0; $t_r = $AIPRRELI_recommendIds; if ( $m_cItems ) { foreach ( $m_cItems as $m_cItem ) { if ( $i < $averageRedisue ) { aiprreli_getRecommendProductIds( $m_cItem, $averageNum + 1 ); } elseif ( 0 != $averageNum ) { aiprreli_getRecommendProductIds( $m_cItem, $averageNum ); } ++$i; } } if ( $AIPRRELI_recommendIds ) { foreach ( $t_r as $recommendId ) { if ( $i < $averageRedisue ) { aiprreli_getRecommendProductIds( $recommendId, $averageNum + 1 ); } elseif ( 0 != $averageNum ) { aiprreli_getRecommendProductIds( $recommendId, $averageNum ); } ++$i; } } } $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $checkPPP, 'post__in' => $AIPRRELI_recommendIds, 'orderby' => 'post__in', ); $PROBOTPost = new WP_Query( $args ); $totalProducts = $PROBOTPost->found_posts; $i = 0; $content = ''; // The Query. if ( $PROBOTPost->have_posts() ) : while ( $PROBOTPost->have_posts() ) : $PROBOTPost->the_post(); include AIPRRELI_PROBOT_DIR . 'templates/grid.php'; endwhile; wp_reset_postdata(); return $content; endif; } } /** * Get product item. */ function aiprreli_pItems() { if ( get_option( 'aiprreli_purchProducts' ) == 1 ) { // GET CURR USER. $current_user = wp_get_current_user(); if ( 0 == $current_user->ID ) { return; } // GET USER ORDERS. $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $current_user->ID, 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_is_paid_statuses() ), ) ); // GET Purchased Items IDs. if ( ! $customer_orders ) { return; } $product_ids = array(); foreach ( $customer_orders as $customer_order ) { $order = wc_get_order( $customer_order->ID ); $items = $order->get_items(); foreach ( $items as $item ) { $product_postId = $item->get_product_id(); $product_postIds[] = $product_postId; foreach ( $product_postIds as $product_pId ) { $product_ids[] = ( get_option( 'aiprreli_suggestType' ) == 2 ) ? wp_get_post_terms( $product_pId, 'product_tag', array( 'fields' => 'ids', ) ) : wp_get_post_terms( $product_pId, 'product_cat', array( 'fields' => 'ids', ) ); } } } $product_ids = array_values( array_unique( call_user_func_array( 'array_merge', $product_ids ) ) ); return $product_ids; } } /** * Retrieve product IDs from the cart based on selected suggest type (tag or category). */ function aiprreli_cItems() { if ( is_admin() ) { return; // Exit early if in admin area. } // Check if cartProducts option is enabled if ( get_option( 'aiprreli_cartProducts' ) != 1 ) { return; // Exit early if cartProducts option is not enabled. } // Initialize an empty array to store product IDs. $product_ids = array(); // Get the WooCommerce cart. if (! WC()->cart) return; $cart = WC()->cart->get_cart(); // Loop through each item in the cart. foreach ( $cart as $cart_item_key => $cart_item ) { // Get the product data from the cart item. $_product = $cart_item['data']; // Determine which taxonomy to use based on suggestType option. $taxonomy = ( get_option( 'aiprreli_suggestType' ) == 2 ) ? 'product_tag' : 'product_cat'; // Get the terms (tags or categories) associated with the product. $terms = get_the_terms( $_product->get_id(), $taxonomy ); // If terms exist, loop through them to extract term IDs. if ( is_array( $terms ) ) { foreach ( $terms as $term ) { $product_ids[] = $term->term_id; } } } // Return the array of product IDs. return $product_ids; } /** * Get options Purchase product item. */ function aiprreli_p_t_Items() { if ( get_option( 'aiprreli_purchProducts' ) == 1 ) { // GET CURR USER. $current_user = wp_get_current_user(); if ( 0 == $current_user->ID ) { return; } // GET USER ORDERS. $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $current_user->ID, 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_is_paid_statuses() ), ) ); // GET Purchased Items IDs. if ( ! $customer_orders ) { return; } $product_ids = array(); $pProducts = array(); foreach ( $customer_orders as $customer_order ) { $order = wc_get_order( $customer_order->ID ); $items = $order->get_items(); foreach ( $items as $item ) { $product_postId = $item->get_product_id(); $product_postIds[] = $product_postId; foreach ( $product_postIds as $product_pId ) { if ( array_key_exists( $product_pId, $pProducts ) ) { ++$pProducts[ $product_pId ]; } else { $pProducts[ $product_pId ] = 1; } } } } $re_pProducts = array(); foreach ( $pProducts as $key => $pProduct ) { $itemproduct = array(); $itemproduct['id'] = $key; $itemproduct['num'] = $pProduct; array_push( $re_pProducts, $itemproduct ); } usort( $re_pProducts, function ( $a, $b ) { if ( $a['num'] == $b['num'] ) { return 0; } return ( $b['num'] < $a['num'] ) ? -1 : 1; } ); return $re_pProducts; } } /** * Retrieve product IDs from the cart. */ function aiprreli_c_t_Items() { // Exit early if in admin area or cartProducts option is not enabled. if ( is_admin() || get_option( 'aiprreli_cartProducts' ) != 1 ) { return array(); } // Initialize an empty array to store product IDs. $product_ids = array(); // Get the WooCommerce cart. if (! WC()->cart) return; $cart = WC()->cart->get_cart(); // Loop through each item in the cart foreach ( $cart as $cart_item_key => $cart_item ) { // Get the product data from the cart item. $_product = $cart_item['data']; // Get the product ID and add it to the array. $product_ids[] = $_product->get_id(); } // Return the array of product IDs. return $product_ids; } add_action( 'woocommerce_admin_process_product_object', 'aiprreli_set_product_description_vector', 10, 1 ); /** * Initialize an array of all font weight. * * @param string $product get product id field. */ function aiprreli_set_product_description_vector( $product ) { $product_ID = $product->get_id(); $product_description = $product->get_description(); $product_embedding = aiprreli_getembedding( strip_tags( $product_description ) ); if ( 'false' == $product_embedding ) { return false; } update_post_meta( $product_ID, 'aiprreli_product_description_embedding', $product_embedding ); return true; } /** * Initialize an array of all font weight. * * @param string $input input text field. */ function aiprreli_getembedding( $input ) { $apiKey = trim( get_option( 'aiprreli_open_ai_key' ) ); $apiUrl = 'https://api.openai.com/v1/embeddings'; // Prepare request arguments $args = array( 'body' => wp_json_encode( array( 'input' => $input, 'num_completions' => 1, 'model' => 'text-embedding-ada-002', 'token_max_length' => 2000, ) ), 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $apiKey, ), 'timeout' => 30, // Set timeout to prevent long waits ); $response = wp_remote_post( $apiUrl, $args ); // Check for errors if ( is_wp_error( $response ) ) { return 'false'; } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); // Validate and extract the embedding data if ( isset( $data['data'][0]['embedding'] ) ) { return json_encode( $data['data'][0]['embedding'], true ); } return 'false'; } add_action( 'wp_ajax_aiprreli_save_short_code_info_action', 'aiprreli_save_short_code_info' ); /** * Save shortcode information function. */ function aiprreli_save_short_code_info() { // Check if nonce is set and verify it. if ( ! isset( $_POST['probot_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['probot_nonce'] ) ), 'probot_ajax_nonce' ) ) { echo 'Nonce verification failed!'; wp_die(); } $PROBOT_autoInsertRcommend = -1; $aiprreli_shortchode = ''; $user_id = get_current_user_id(); $errorMsg = ''; if ( $user_id ) { if ( array_key_exists( 'shortchode', $_POST ) ) { $aiprreli_shortchode = sanitize_text_field( wp_unslash( $_POST['shortchode'] ) ); } if ( '' != $aiprreli_shortchode ) { update_option( 'aiprreli_shortchode', $aiprreli_shortchode ); } } else { $errorMsg = 'Dont exist logged in user'; } if ( '' == $errorMsg ) { $errorMsg = 'ok'; } echo esc_js( $errorMsg ); wp_die(); } add_action( 'wp_ajax_import_all_production_embedding_action', 'aiprreli_import_create_ajax_detail_response' ); /** * Import ajax details function. */ function aiprreli_import_create_ajax_detail_response() { $args = array( 'status' => array( 'draft', 'pending', 'private', 'publish', ), 'type' => array_merge( array_keys( wc_get_product_types() ) ), 'parent' => null, 'sku' => '', 'category' => array(), 'tag' => array(), 'limit' => -1, // -1 for unlimited 'offset' => null, 'page' => 1, 'include' => array(), 'exclude' => array(), 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', 'paginate' => false, 'shipping_class' => array(), ); // Array of product objects. $products = wc_get_products( $args ); // Loop through list of products. $importedcount = 0; foreach ( $products as $product ) { if ( ! aiprreli_set_product_description_vector( $product ) ) { echo 'Importing faild! please check your api key again and retry.'; break; } else { ++$importedcount; } } if ( $importedcount > 0 ) { echo 'Succeded! The embedding data of ' . esc_js( $importedcount ) . ' products were imported!'; } wp_die(); } $aiprreli_original_embedding = array(); /** * Initialize an array of all font weight. * * @param string $productId Product ID number field. * * @param string $num number field. */ function aiprreli_getRecommendProductIds( $productId, $num ) { global $AIPRRELI_recommendIds, $AIPRRELI_originalId, $aiprreli_original_embedding; $AIPRRELI_originalId = $productId; $original_product = wc_get_product( $productId ); if ( ! $original_product ){ return; } $aiprreli_original_embedding = json_decode( $original_product->get_meta( 'aiprreli_product_description_embedding' ), true ); $termIds = array(); $taxonomy1 = 'product_cat'; if ( get_option( 'aiprreli_suggestType' ) == 1 ) { $taxonomy1 = 'product_cat'; $terms = get_the_terms( $productId, 'product_cat' ); foreach ( $terms as $term ) { $termIds[] = $term->term_id; } } elseif ( get_option( 'aiprreli_suggestType' ) == 2 ) { $taxonomy1 = 'product_tag'; $terms = wp_get_post_terms( $productId, 'product_tag' ); foreach ( $terms as $term ) { $termIds[] = $term->term_id; } } $termArray = array( array( 'taxonomy' => $taxonomy1, 'field' => 'term_id', // This is optional, as it defaults to 'term_id'. 'terms' => $termIds, 'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'. ), ); $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'rand', 'tax_query' => $termArray, ); $products = wc_get_products( $args ); aiprreli_extractProductsInSimilarityOrder( $products, $num ); } /** * Get Recommend products functins. * * @param string $productId Product ID number field. */ function aiprreli_getRecommendProductsFromAll( $productId ) { $original_product = wc_get_product( $product_id ); $aiprreli_original_embedding = json_decode( $original_product->get_meta( 'aiprreli_product_description_embedding' ), true ); $aiprreli_idLimit = ( ! empty( get_option( 'aiprreli_idLimit' ) ) ) ? get_option( 'aiprreli_idLimit' ) : 10; $args = array( 'status' => array( 'private', 'publish', ), 'type' => array_merge( array_keys( wc_get_product_types() ) ), 'parent' => null, 'sku' => '', 'category' => array(), 'tag' => array(), 'limit' => -1, // -1 for unlimited 'offset' => null, 'page' => 1, 'include' => array(), 'exclude' => array(), 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', 'paginate' => false, 'shipping_class' => array(), ); // Array of product objects. $products = wc_get_products( $args ); return aiprreli_extractProductsInSimilarityOrder( $products, $aiprreli_original_embedding, $aiprreli_idLimit ); return false; } /** * Get Unimported Recommend MetaData Product Number */ function aiprreli_GetUnimportedRecommendMetaDataProductNum() { $i = 0; $args = array( 'status' => array( 'private', 'publish', ), 'type' => array_merge( array_keys( wc_get_product_types() ) ), 'parent' => null, 'sku' => '', 'category' => array(), 'tag' => array(), 'limit' => -1, // -1 for unlimited 'offset' => null, 'page' => 1, 'include' => array(), 'exclude' => array(), 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', 'paginate' => false, 'shipping_class' => array(), ); $products = wc_get_products( $args ); foreach ( $products as $product ) { $product_embedding = $product->get_meta( 'aiprreli_product_description_embedding' ); if ( ! $product_embedding ) { ++$i; } } return $i; } /** * Extract Products In Similarity Order. * * @param string $products Product ID number field. * @param string $n number field. */ function aiprreli_extractProductsInSimilarityOrder( $products, $n ) { // Sort the array based on the 'o' key in descending order. global $AIPRRELI_recommendIds, $AIPRRELI_addedRecommends, $AIPRRELI_originalId, $AIPRRELI_carts, $aiprreli_original_embedding; usort( $products, function ( $a, $b ) { global $aiprreli_original_embedding; $product_embedding_a = json_decode( $a->get_meta( 'aiprreli_product_description_embedding' ), true ); $product_embedding_b = json_decode( $b->get_meta( 'aiprreli_product_description_embedding' ), true ); $score_a = aiprreli_dot_product( $aiprreli_original_embedding, $product_embedding_a ) * 10000; $score_b = aiprreli_dot_product( $aiprreli_original_embedding, $product_embedding_b ) * 10000; if ( $score_b == $score_a ) { return 0; } return ( $score_b < $score_a ) ? -1 : 1; } ); // Extract the first n elements. $extractedElements = array(); $i = 0; foreach ( $products as $product ) { $inFlag = false; $productId = $product->get_id(); foreach ( $AIPRRELI_carts as $PROBOT_cart ) { if ( $PROBOT_cart == $productId ) { $inFlag = true; } } foreach ( $AIPRRELI_recommendIds as $recommendItem ) { if ( $recommendItem == $productId ) { $inFlag = true; } } if ( ! $inFlag ) { $addeitem = array( 'id' => $productId, 'origianlId' => $AIPRRELI_originalId, ); if( ! $AIPRRELI_addedRecommends ) $AIPRRELI_addedRecommends=[]; array_push( $AIPRRELI_addedRecommends, $addeitem ); array_push( $AIPRRELI_recommendIds, $productId ); ++$i; } if ( $i == $n ) { break; } } } /** * Get Recommend Products From Category */ function aiprreli_getRecommendProductsFromCat() { return false; } /** * Get Recommend Products From Tax */ function aiprreli_getRecommendProductsFromTax() { return false; } /** * Get dot product * * @param string $a number field. * @param string $b number field. */ function aiprreli_dot_product( $a, $b ) { if ( is_array( $a ) && is_array( $b ) ) { $result = array_map( function ( $x, $y ) { return $x * $y; }, $a, $b ); return array_sum( $result ); } return 0; } }