Skip to content

Instantly share code, notes, and snippets.

@jjmontalban
Created January 18, 2023 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjmontalban/a6b2eb652a55550a19ba897cee2d1ce1 to your computer and use it in GitHub Desktop.
Save jjmontalban/a6b2eb652a55550a19ba897cee2d1ce1 to your computer and use it in GitHub Desktop.
Añadir atributos de producto en paginas woocommerce (ocultando los atributos de variaciones fuera de stock)
/**
* @snippet Añadir atributos de producto en las paginas woocommerce (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_after_shop_loop_item_title','mostrar_atributos_disponibles');
/**
* @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', 'pa_medida', 'pa_peso', 'pa_origen' );
$attr_output = array();
//Mostrar los tallos por paquete
$tallos = $product->get_meta( '_qty_args' );
if ( !empty( $tallos ) && isset( $tallos['qty_min']) && $product->get_id() != 5297 )
{
echo '<div class="product-attributes"><strong>Tallos por paquete: </strong>'. $tallos['qty_min'] .'</div>';
}
// variable products
// special product id 5297: Cajas de rosas completas
if ( $product->is_type('variable') && $product->get_id() != 5297 )
{
// Medidas
$taxonomy = 'pa_medida';
$medidas_array = [];
// 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->is_purchasable() && $variation->is_in_stock() )
{
$term_name = $variation->get_attribute( $taxonomy );
$medidas_array[$term_name] = $term_name;
}
}
if( !empty( $medidas_array[$term_name] ) )
{
echo '<span class="attribute-size"><strong>Medidas</strong>: ' . implode( ', ', $medidas_array ) . '</span>';
}
//Colores
$taxonomy = 'pa_color';
if( taxonomy_exists($taxonomy) )
{
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute($taxonomy);
if( !empty($value) )
{
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'"><strong>'.$label_name.'</strong>: '.$value.'</span>';
}
}
//Origen
$taxonomy = 'pa_origen';
if( taxonomy_exists($taxonomy) )
{
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute($taxonomy);
//Si el producto no tiene medida asignada no muestra nada
if( !empty($value) )
{
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'"><strong>'.$label_name.'</strong>: '.$value.'</span>';
}
}
}
else
{
// Loop through the array of simple product attributes
foreach( $product_attributes as $attribute )
{
if( taxonomy_exists( $attribute ) )
{
$label_name = get_taxonomy( $attribute )->labels->singular_name;
$value = $product->get_attribute($attribute);
if( !empty($value) )
{
// Storing attributes for output
$attr_output[] = '<span class="'.$attribute.'"><strong>'.$label_name.'</strong>: '.$value.'</span>';
}
}
}
}
//Caso especial de productos variables preservados. para mostrar la medida
if( $product->is_type('variable') && has_term( 'preservadas', 'product_cat' ) )
{
// Loop through the array of simple product attributes
if( taxonomy_exists( 'pa_medida' ) )
{
$label_name = get_taxonomy( 'pa_medida' )->labels->singular_name;
$value = $product->get_attribute( 'pa_medida' );
if( !empty( $value ) )
{
// Storing attributes for output
$attr_output[] = '<span class="pa_medida"><strong>'.$label_name.'</strong>: '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment