41, 'crosssells' => 42, 'upsells' => 43, 'related' => 44, 'semantic_related' => 45, ); const EFFECTIVE_LIMIT = 8; const SAME_NEEDS_LIMIT = 6; public static function in_layer() { return class_exists( 'Rooster\Core\Defer' ) && method_exists( 'Rooster\Core\Defer', 'is_full' ) && \Rooster\Core\Defer::is_full(); } public static function serves( $surface ) { return in_array( (string) $surface, self::SURFACES, true ) && self::in_layer() && ! ( class_exists( 'AIEO_Reco_Experiment' ) && AIEO_Reco_Experiment::is_running() ); } public static function ids( $surface, $product_id ) { $product_id = (int) $product_id; if ( $product_id <= 0 || ! function_exists( 'wc_get_product' ) ) { return array(); } if ( class_exists( 'AIEO_DMM_Product_Display' ) && ! AIEO_DMM_Product_Display::reco_surface_visible( (string) $surface ) ) { return array(); } switch ( (string) $surface ) { case 'upsells': return self::surfacable( self::collected( $product_id, 'upsells', '_upsell_ids' ), $product_id ); case 'crosssells': return self::surfacable( self::collected( $product_id, 'crosssells', '_crosssell_ids' ), $product_id ); case 'related': $ids = self::surfacable( self::collected( $product_id, 'related', null ), $product_id ); shuffle( $ids ); return $ids; case 'same_needs': if ( ! class_exists( 'AI_eShop_Need_Suggestions' ) && file_exists( __DIR__ . '/class-need-suggestions.php' ) ) { require_once __DIR__ . '/class-need-suggestions.php'; } if ( ! class_exists( 'AI_eShop_Need_Suggestions' ) ) { return array(); } $same = AI_eShop_Need_Suggestions::get_instance()->get_suggestions( $product_id ); return array_slice( self::surfacable( is_array( $same ) ? $same : array(), $product_id ), 0, self::SAME_NEEDS_LIMIT ); case 'semantic_related': if ( ! class_exists( 'AIEO_Semantic_Reco_Source' ) ) { return array(); } $limit = class_exists( 'AIEO_DMM_Settings' ) ? (int) AIEO_DMM_Settings::get( 'semantic.related_block_limit', 6 ) : 6; $limit = max( 1, min( 24, $limit ) ); $ids = (array) apply_filters( 'aieo_semantic_reco_ids', (array) AIEO_Semantic_Reco_Source::ids_for( $product_id, $limit * 2 ), $product_id, $limit ); return array_slice( self::surfacable( array_values( array_filter( array_map( 'intval', $ids ) ) ), $product_id ), 0, $limit ); } return array(); } public static function html( $surface, array $ids ) { $surface = (string) $surface; $ids = array_values( array_unique( array_filter( array_map( 'intval', $ids ) ) ) ); if ( array() === $ids || ! isset( self::QUERY_IDS[ $surface ] ) || ! function_exists( 'render_block' ) ) { return ''; } $titles = is_callable( array( 'aieo_display_recommendations', 'section_titles' ) ) ? (array) aieo_display_recommendations::section_titles() : array(); $title = isset( $titles[ $surface ] ) ? (string) $titles[ $surface ] : ''; $collection = array( 'queryId' => self::QUERY_IDS[ $surface ], 'query' => array( 'perPage' => count( $ids ), 'pages' => 1, 'offset' => 0, 'postType' => 'product', 'order' => 'asc', 'orderBy' => 'post__in', 'search' => '', 'exclude' => array(), 'inherit' => false, 'taxQuery' => array(), 'isProductCollectionBlock' => true, 'featured' => false, 'woocommerceOnSale' => false, 'woocommerceStockStatus' => array( 'instock', 'onbackorder' ), 'woocommerceAttributes' => array(), 'woocommerceHandPickedProducts' => $ids, 'filterable' => false, ), 'tagName' => 'div', 'displayLayout' => array( 'type' => 'flex', 'columns' => 4, 'shrinkColumns' => true ), 'dimensions' => array( 'widthType' => 'fill' ), 'collection' => 'woocommerce/product-collection/hand-picked', 'hideControls' => array( 'inherit', 'filterable' ), 'queryContextIncludes' => array( 'collection' ), 'align' => 'wide', 'className' => 'is-style-rooster-slider', ); $head = '' === $title ? '' : '' . '
' . '

' . esc_html( $title ) . '

' . '
'; $card = '' . '' . '
' . '' . '' . '' . '' . '' . '
' . ''; $markup = '' . '
' . $head . $card . '
' . ''; $post_was = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; $product_was = isset( $GLOBALS['product'] ) ? $GLOBALS['product'] : null; $html = ''; foreach ( parse_blocks( $markup ) as $block ) { if ( ! empty( $block['blockName'] ) ) { $html .= render_block( $block ); } } $GLOBALS['post'] = $post_was; $GLOBALS['product'] = $product_was; $tags = new WP_HTML_Tag_Processor( $html ); if ( false === strpos( $html, 'wc-block-product' ) || ! $tags->next_tag( array( 'class_name' => 'wp-block-woocommerce-product-collection' ) ) ) { return ''; } $tags->set_attribute( 'data-section', $surface ); return $tags->get_updated_html(); } private static function collected( $product_id, $surface, $meta_key ) { if ( class_exists( 'AIEO_Effective_Recos' ) && AIEO_Effective_Recos::is_enabled() ) { $audience = class_exists( 'AIEO_Identity_Bridge' ) ? AIEO_Identity_Bridge::audience_key_for_current_user() : 'guest'; $ids = (array) AIEO_Effective_Recos::get_top( $product_id, $surface, $audience, self::EFFECTIVE_LIMIT ); if ( array() !== $ids ) { return array_map( 'intval', $ids ); } } if ( null === $meta_key ) { return function_exists( 'wc_get_related_products' ) ? array_map( 'intval', (array) wc_get_related_products( $product_id, self::EFFECTIVE_LIMIT ) ) : array(); } $raw = get_post_meta( $product_id, $meta_key, true ); return is_array( $raw ) ? array_map( 'intval', $raw ) : array(); } private static function surfacable( array $ids, $product_id ) { $out = array(); foreach ( array_unique( array_map( 'intval', $ids ) ) as $id ) { $item = ( $id > 0 && $id !== (int) $product_id ) ? wc_get_product( $id ) : null; if ( $item instanceof WC_Product && ! $item->is_type( 'variation' ) && 'publish' === $item->get_status() && $item->is_in_stock() && $item->is_visible() ) { $out[] = $id; } } return array_values( array_filter( array_map( 'intval', (array) apply_filters( 'aieo_reco_filter_candidates', $out, $ids ) ) ) ); } } endif;