Created
December 1, 2020 18:15
-
-
Save lucasstark/676912766a00f547839d982f1300cb30 to your computer and use it in GitHub Desktop.
Add WooCommerce Variation Swatches and Photos to the Archive Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wc_swatches_shop_loop_item_href( $product, $attribute_name, $attribute_value ) { | |
$url = add_query_arg( array( | |
$attribute_name => $attribute_value | |
), $product->get_permalink() ); | |
return $url; | |
} | |
add_filter( 'woocommerce_loop_add_to_cart_link', 'wc_swatches_maybe_override_add_to_cart', 10, 2 ); | |
function wc_swatches_maybe_override_add_to_cart( $url, $product ) { | |
if ( $product->get_type() == 'variable' ) { | |
$attributes = $product->get_variation_attributes(); | |
foreach ( $attributes as $attribute => $options ) { | |
$config = new WC_Swatches_Attribute_Configuration_Object( $product, $attribute ); | |
if ( $config->get_type() != 'default' ) { | |
return false; | |
} | |
} | |
} | |
return $url; | |
} | |
add_action( 'woocommerce_after_shop_loop_item', 'wc_swatches_after_shop_loop_item', 11 ); | |
function wc_swatches_after_shop_loop_item() { | |
// Get selected value. | |
$override_add_to_cart = false; | |
$product = wc_get_product(); | |
if ( $product->get_type() != 'variable' ) { | |
return; | |
} | |
$attributes = $product->get_variation_attributes(); | |
foreach ( $attributes as $attribute => $options ) { | |
$args = array(); | |
$args['selected'] = false; | |
$name = 'attribute_' . sanitize_title( $attribute ); | |
$id = $product->get_id() . '_' . sanitize_title( $attribute ); | |
$config = new WC_Swatches_Attribute_Configuration_Object( $product, $attribute ); | |
if ( $config->get_type() != 'default' ) : | |
do_action( 'woocommerce_swatches_before_picker', $config ); | |
echo '<div id="picker_' . esc_attr( $id ) . '" class="select swatch-control">'; | |
$args['hide'] = true; | |
do_action( 'woocommerce_swatches_before_picker_items', $config ); //added by TDG | |
if ( !empty( $options ) ) { | |
$override_add_to_cart = true; | |
if ( $product && taxonomy_exists( $attribute ) ) { | |
// Get terms if this is a taxonomy - ordered. We need the names too. | |
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) ); | |
foreach ( $terms as $term ) { | |
if ( in_array( $term->slug, $options ) ) { | |
if ( $config->get_type() == 'term_options' ) { | |
$swatch_term = new WC_Swatch_Term( $config, $term->term_id, $attribute, $args['selected'] == $term->slug, $config->get_size() ); | |
} elseif ( $config->get_type() == 'product_custom' ) { | |
$swatch_term = new WC_Product_Swatch_Term( $config, $term->term_id, $attribute, $args['selected'] == $term->slug, $config->get_size() ); | |
} | |
do_action( 'woocommerce_swatches_before_picker_item', $swatch_term ); | |
$url = wc_swatches_shop_loop_item_href( $product, $name, $term->slug ); | |
add_filter( 'woocommerce_swatches_get_swatch_href', function ( $source_url ) use ( $url ) { | |
return $url; | |
}, 10, 1 ); | |
echo $swatch_term->get_output(); | |
remove_all_filters( 'woocommerce_swatches_get_swatch_href' ); | |
do_action( 'woocommerce_swatches_after_picker_item', $swatch_term ); | |
} | |
} | |
} else { | |
foreach ( $options as $option ) { | |
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized. | |
$swatch_term = new WC_Product_Swatch_Term( $config, $option, $name, false, $config->get_size() ); | |
$url = wc_swatches_shop_loop_item_href( $product, $name, $option ); | |
add_filter( 'woocommerce_swatches_get_swatch_href', function ( $source_url ) use ( $url ) { | |
return $url; | |
}, 10, 1 ); | |
do_action( 'woocommerce_swatches_before_picker_item', $swatch_term ); | |
echo $swatch_term->get_output(); | |
do_action( 'woocommerce_swatches_after_picker_item', $swatch_term ); | |
remove_all_filters( 'woocommerce_swatches_get_swatch_href' ); | |
} | |
} | |
} | |
do_action( 'woocommerce_swatches_after_picker_items', $config ); //added by TDG | |
echo '</div>'; | |
do_action( 'woocommerce_swatches_after_picker', $config ); | |
endif; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment