ID, self::META, true ); $badge = get_post_meta( $variation->ID, self::META_BADGE, true ); ?>

true ) ); if ( is_singular( 'product' ) ) { self::enqueue(); } } public static function enqueue() { wp_enqueue_style( 'variantly' ); wp_enqueue_script( 'variantly' ); } /** Maps of attribute value => color / badge, taken from the first variation that uses that value. */ private static function data_for( $product, $attribute ) { $key = 'attribute_' . sanitize_title( $attribute ); $colors = array(); $badges = array(); $ids = $product->get_children(); // One query loads every variation's meta, instead of one object per variation. update_meta_cache( 'post', $ids ); foreach ( $ids as $variation_id ) { $value = (string) get_post_meta( $variation_id, $key, true ); $color = get_post_meta( $variation_id, self::META, true ); if ( '' === $value || ! $color ) { continue; } if ( ! isset( $colors[ $value ] ) ) { $colors[ $value ] = $color; $badges[ $value ] = (string) get_post_meta( $variation_id, self::META_BADGE, true ); } elseif ( strtolower( $colors[ $value ] ) !== strtolower( $color ) ) { // The same value maps to different colors (e.g. "Small" across a Red and a Blue variation), // so this is not the color attribute. Leave it as a normal dropdown. return array( array(), array() ); } } return array( $colors, $badges ); } /** Option values whose variations are all out of stock. */ private static function out_of_stock_values( $product, $attribute ) { $key = 'attribute_' . sanitize_title( $attribute ); $seen = array(); foreach ( $product->get_children() as $variation_id ) { $value = (string) get_post_meta( $variation_id, $key, true ); if ( '' === $value ) { continue; } $in_stock = 'outofstock' !== get_post_meta( $variation_id, '_stock_status', true ); $seen[ $value ] = ! empty( $seen[ $value ] ) || $in_stock; } return array_keys( array_filter( $seen, function ( $in_stock ) { return ! $in_stock; } ) ); } public static function render_swatches( $html, $args ) { $product = $args['product']; $attribute = $args['attribute']; if ( ! $product || empty( $args['options'] ) ) { return $html; } list( $colors, $badges ) = self::data_for( $product, $attribute ); // Only swap the dropdown when every option has a color, otherwise some choices would vanish. foreach ( $args['options'] as $option ) { if ( empty( $colors[ $option ] ) ) { return $html; } } $opts = Variantly_Settings::get(); $oos_list = self::out_of_stock_values( $product, $attribute ); $items = ''; foreach ( $args['options'] as $option ) { $label = taxonomy_exists( $attribute ) ? ( ( $term = get_term_by( 'slug', $option, $attribute ) ) ? $term->name : $option ) : $option; $selected = sanitize_title( $args['selected'] ) === sanitize_title( $option ); $badge = ! empty( $badges[ $option ] ) ? '' . esc_html( $badges[ $option ] ) . '' : ''; $oos = in_array( (string) $option, array_map( 'strval', $oos_list ), true ); $oos_text = ( $oos && 'text' === $opts['oos_mode'] ) ? '' . esc_html( $opts['oos_text'] ) . '' : ''; $items .= sprintf( '', $selected ? 'true' : 'false', esc_attr( $label ), esc_attr( $option ), esc_attr( $label ), ( $selected ? ' selected' : '' ) . ( $oos ? ' variantly-oos' . ( 'text' === $opts['oos_mode'] ? ' variantly-oos-label' : ' variantly-oos-cross' ) : '' ), esc_attr( $colors[ $option ] ), $badge, $oos_text, esc_html( $label ) ); } self::enqueue(); // Keep the original %s', esc_attr( wc_attribute_label( $attribute ) ), esc_attr( sanitize_title( $attribute ) ), $items ); } } add_action( 'before_woocommerce_init', function () { if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); } } ); add_action( 'plugins_loaded', function () { if ( class_exists( 'WooCommerce' ) ) { Variantly::init(); require_once __DIR__ . '/includes/class-variantly-gallery.php'; Variantly_Gallery::init(); require_once __DIR__ . '/includes/class-variantly-settings.php'; Variantly_Settings::init(); } } );