is_type( 'variable' ) && class_exists( 'AIEO_DMM_Swatches_Runtime', false )
&& false !== has_filter( 'woocommerce_dropdown_variation_attribute_options_html' ) ) {
$runtime = AIEO_DMM_Swatches_Runtime::instance();
$draws = method_exists( $runtime, 'swatches_draw' ) ? $runtime->swatches_draw( $product->get_id() ) : true;
}
if ( ! $draws && class_exists( 'AIEO_Option_Groups_Frontend', false ) && method_exists( 'AIEO_Option_Groups_Frontend', 'config_for_product' ) ) {
$draws = (bool) AIEO_Option_Groups_Frontend::config_for_product( $product->get_id() );
}
return (bool) apply_filters( 'aieo_block_parity_classic_add_to_cart', $draws, $product );
}
public static function classic_add_to_cart( $pre, $parsed_block ) {
if ( null !== $pre || empty( $parsed_block['blockName'] ) || 'woocommerce/add-to-cart-with-options' !== $parsed_block['blockName'] ) {
return $pre;
}
if ( is_admin() || ! function_exists( 'woocommerce_template_single_add_to_cart' ) || ! is_singular( 'product' ) ) { return $pre; }
$product = wc_get_product( get_queried_object_id() );
if ( ! $product || ! self::draws_add_to_cart( $product ) ) { return $pre; }
if ( class_exists( 'AIEO_Catalogue_Mode' ) && AIEO_Catalogue_Mode::cart_off( $product ) ) { return $pre; }
$previous = isset( $GLOBALS['product'] ) ? $GLOBALS['product'] : null;
$GLOBALS['product'] = $product;
$line = $product->is_type( 'variable' );
if ( $line ) {
add_action( 'woocommerce_after_variations_form', array( __CLASS__, 'print_stock_line' ), 5 );
}
if ( WP_Block_Type_Registry::get_instance()->is_registered( 'woocommerce/add-to-cart-form' ) ) {
$form = ( new WP_Block(
array(
'blockName' => 'woocommerce/add-to-cart-form',
'attrs' => array( 'quantitySelectorStyle' => 'stepper' ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
),
array( 'postId' => $product->get_id(), 'postType' => 'product' )
) )->render();
$form = preg_replace( '/^
' . $form . '
';
}
if ( $line ) {
remove_action( 'woocommerce_after_variations_form', array( __CLASS__, 'print_stock_line' ), 5 );
}
$GLOBALS['product'] = $previous;
if ( '' === $form ) { return $pre; }
if ( class_exists( 'AIEO_Theme_Adapters' ) && method_exists( 'AIEO_Theme_Adapters', 'draw' ) ) {
AIEO_Theme_Adapters::draw( 'variation_form', 'classic form inside the Add to Cart with Options block' );
}
return $form;
}
const NAMED_SOLD_OUT = 3;
public static function stock_line( $product ) {
$none = array( 'text' => '', 'class' => '' );
if ( ! $product instanceof WC_Product || ! $product->is_type( 'variable' ) ) {
return $none;
}
$ids = $product->get_visible_children();
if ( array() === $ids ) {
return $none;
}
$availability = $product->get_availability();
$text = trim( (string) ( $availability['availability'] ?? '' ) );
$class = (string) ( $availability['class'] ?? '' );
if ( 'in-stock' !== $class ) {
return '' === $text ? $none : array( 'text' => $text, 'class' => $class );
}
$in = 0;
$names = array();
foreach ( $ids as $id ) {
$variation = wc_get_product( $id );
if ( ! $variation instanceof WC_Product ) {
continue;
}
$status = (string) $variation->get_stock_status();
if ( 'instock' === $status ) {
++$in;
} elseif ( 'outofstock' === $status ) {
$names[] = wc_get_formatted_variation( $variation, true, false );
}
}
$out = count( $names );
if ( $out > 0 && $in > 0 ) {
$all = count( $ids );
$one = count( array_filter( (array) $product->get_variation_attributes() ) ) <= 1;
$words = $one
? sprintf( _n( '%1$s of %2$s shade in stock', '%1$s of %2$s shades in stock', $all, 'ai-eshop-optimizer' ), number_format_i18n( $in ), number_format_i18n( $all ) )
: sprintf( _n( '%1$s of %2$s option in stock', '%1$s of %2$s options in stock', $all, 'ai-eshop-optimizer' ), number_format_i18n( $in ), number_format_i18n( $all ) );
$line = class_exists( 'AIEO_Dispatch_Cutoff' ) ? (string) AIEO_Dispatch_Cutoff::availability( $words, $product ) : $words;
if ( $line === $words ) {
$list = $one && $out <= self::NAMED_SOLD_OUT ? wp_sprintf_l( '%l', array_map( 'wp_strip_all_tags', $names ) ) : number_format_i18n( $out );
$line .= ' ยท ' . sprintf(
1 === $out
? _x( '%s sold out', 'one sold-out shade', 'ai-eshop-optimizer' )
: _x( '%s sold out', 'several sold-out shades', 'ai-eshop-optimizer' ),
$list
);
}
return array( 'text' => $line, 'class' => 'in-stock is-low' );
}
if ( '' === $text ) {
$text = _x( 'In stock', 'the stock line, before when an order is shipped', 'ai-eshop-optimizer' );
}
$low = $product->managing_stock() && $product->get_stock_quantity() > 0 && (int) $product->get_stock_quantity() <= (int) wc_get_low_stock_amount( $product );
return array( 'text' => $text, 'class' => $low ? 'in-stock is-low' : 'in-stock' );
}
public static function stock_line_html( $product ) {
$line = self::stock_line( $product );
if ( '' === $line['text'] ) {
return '';
}
return '' . wp_kses_post( $line['text'] ) . '
';
}
public static function print_stock_line() {
global $product;
echo self::stock_line_html( $product );
}
}
}