Код: Выделить всё
add_action( 'woocommerce_before_shop_loop_item_title', 'new_template_loop_product_meta', 20 );
function new_template_loop_product_meta() {
global $product;
$attrs_by_cats = [
20 => [ 'pa_size' ],
];
$attr_list = [
'Size' => 'pa_size',
];
if ( ! is_object( $product ) ) {
$product = wc_get_product( get_the_id() );
}
$cats = $product->get_category_ids();
if ( ! is_array( $cats ) ) {
return;
}
$attrs = [];
foreach ( $cats as $cat ) {
if ( isset( $attrs_by_cats[ $cat ] ) ) {
$attrs[] = $attrs_by_cats[ $cat ];
}
}
$allowed_attrs = array_unique( array_merge( [], ...$attrs ) );
echo '';
foreach ( $attr_list as $attr_title => $attr_name ) {
if ( in_array( $attr_name, $allowed_attrs, true ) ) {
show_attribute( $product, $attr_title, $attr_name );
}
}
echo '';
}
/* Show attr */
function show_attribute( $product, $attr_title, $attr_name ) {
if ( 'sku' === $attr_name ) {
$attr = (string) esc_html( $product->get_sku() );
} else {
$attr = $product->get_attribute( $attr_name );
if ( ! $attr ) {
return;
}
$attr = explode( ', ', $attr ); // convert the coma separated string to an array
$attr_arr = []; // Initialize
// Loop through the term names
foreach ( $attr as $term_name ) {
// Embed each term in a span tag
$attr_arr[] = sprintf('%s', $term_name);
}
// Convert back the array of formatted html term names to a string
$attr = implode(' ', $attr_arr);
}
if ( '' === $attr ) {
return;
}
printf( '%s: %s', $attr_title, $attr);
}
При создании товара я добавил размеры S, M, L и автоматически создал вариации. Затем для каждого размера S, M и L я вручную установил наличие на складе 30.
Затем размер L весь распродан, и у меня на складе осталось 0 штук. За исключением того, что на странице списка товаров показаны все размеры, а должны быть показаны S и M.
Как исправить этот код, чтобы он работал с разными товарами?Заранее спасибо за помощь!
Подробнее здесь: https://stackoverflow.com/questions/790 ... erce-pages