cart && function_exists( 'wc_load_cart' ) ) { wc_load_cart(); } if ( ! WC()->cart ) { return; } Bypass::set( false ); if ( WC()->cart->is_empty() ) { $added = self::add_product(); Log::info( $added ? 'Preview: a product was put in the cart.' : 'Preview: no product could be put in the cart.', array( 'product' => $added ) ); if ( ! $added ) { wp_safe_redirect( add_query_arg( 'aiolos-preview-failed', '1', Admin::url( 'start' ) ) ); exit; } } nocache_headers(); wp_safe_redirect( wc_get_checkout_url() ); exit; } /** * Adds the first product that can be bought (shipped products first, so the shipping step * shows). Returns its id, 0 when none could be added. */ private static function add_product(): int { /** * Filters the product put in the cart for the preview (0: the plugin picks one). * * @since 1.6.0 * * @param int $product_id Product id. */ $chosen = absint( apply_filters( 'aiolos_preview_product_id', 0 ) ); $ids = $chosen ? array( $chosen ) : array(); if ( ! $ids ) { $ids = wc_get_products( array( 'status' => 'publish', 'type' => array( 'simple', 'variable' ), 'stock_status' => 'instock', 'visibility' => 'catalog', 'limit' => 30, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'ids', ) ); } $candidates = array(); foreach ( (array) $ids as $id ) { $product = wc_get_product( (int) $id ); if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() ) { continue; } // Shipped, simple products first; then shipped variable ones; then the rest. $rank = ( $product->needs_shipping() ? 0 : 2 ) + ( $product->is_type( 'variable' ) ? 1 : 0 ); $candidates[] = array( $rank, $product ); } usort( $candidates, static fn( $a, $b ) => $a[0] <=> $b[0] ); foreach ( $candidates as $candidate ) { $product = $candidate[1]; try { if ( $product->is_type( 'variable' ) ) { $added = self::add_variation( $product ); } else { $added = (bool) WC()->cart->add_to_cart( $product->get_id(), 1 ); } } catch ( \Exception $e ) { $added = false; } if ( $added ) { // The preview should not leave "added to your cart" notices behind. wc_clear_notices(); return (int) $product->get_id(); } } wc_clear_notices(); return 0; } /** * Adds the first variation that can be bought, with "any" attributes set to their first value. * * @param \WC_Product $product Variable product. */ private static function add_variation( $product ): bool { $options = method_exists( $product, 'get_variation_attributes' ) ? (array) $product->get_variation_attributes() : array(); foreach ( $product->get_children() as $child_id ) { $variation = wc_get_product( $child_id ); if ( ! $variation || ! $variation->is_purchasable() || ! $variation->is_in_stock() ) { continue; } $attributes = array(); foreach ( (array) $variation->get_variation_attributes() as $name => $value ) { if ( '' === (string) $value ) { foreach ( $options as $attribute => $values ) { if ( 'attribute_' . sanitize_title( (string) $attribute ) === $name && $values ) { $value = (string) reset( $values ); } } } $attributes[ $name ] = (string) $value; } if ( WC()->cart->add_to_cart( $product->get_id(), 1, (int) $child_id, $attributes ) ) { return true; } } return false; } }