Created
March 3, 2023 22:18
-
-
Save jjmontalban/cd16836f3bfa247ec60e29ef5d8fa49f to your computer and use it in GitHub Desktop.
Mostrar atributos de productos en listados
This file contains hidden or 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
/** | |
* @snippet Añadir atributos de producto en las paginas de categorias (ocultando los atributos de variaciones fuera de stock) | |
* @author https://fsxperts.com/how-to-show-product-attributes-on-category-page/ | |
* @author https://stackoverflow.com/questions/30855309/how-to-check-product-have-variation-in-woocommerce | |
*/ | |
add_action('woocommerce_before_shop_loop_item_title','mostrar_atributos_disponibles'); | |
/* @visual_hook_guide: https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/*/ | |
/** | |
* @snippet // Add product attributes in summary (single product page). Entre el precio y descripcion corta | |
* @author https://stackoverflow.com/questions/13374883/get-custom-product-attributes-in-woocommerce | |
*/ | |
//add_action( 'woocommerce_single_product_summary', 'mostrar_atributos_disponibles', 15 ); | |
function mostrar_atributos_disponibles() | |
{ | |
global $product; | |
$product_attributes = array( 'pa_color'); | |
//$attr_output = array(); | |
//Colores | |
$color_rosa = 'pa_color'; | |
$color_teddy = 'pa_color-teddy'; | |
$array_colores = []; | |
$array_colores_teddy = []; | |
// Loop through available variation Ids for the variable product | |
foreach( $product->get_children() as $child_id ) | |
{ | |
$variation = wc_get_product( $child_id ); // Get the WC_Product_Variation object | |
if( $variation->variation_is_visible() && $variation->is_purchasable() && $variation->is_visible() ) | |
{ | |
$term_name_rosa = $variation->get_attribute( $color_rosa ); | |
$array_colores[$term_name_rosa] = $term_name_rosa; | |
$term_name_teddy = $variation->get_attribute( $color_teddy ); | |
$array_colores_teddy[$term_name_teddy] = $term_name_teddy; | |
} | |
} | |
if( !empty( $array_colores[$term_name_rosa] ) ) | |
{ | |
echo '<div class="attribute-color">' . count($array_colores) .' colores</div>'; | |
} | |
if( !empty( $array_colores_teddy[$term_name_teddy] ) ) | |
{ | |
echo '<span class="attribute-color">' . count($array_colores_teddy) .' colores</span>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment