отображение по количеству на складе woocommorce

#php #wordpress

#php #wordpress

Вопрос:

Я готовлю торговый сайт с помощью woocommerce. Как и в примере ссылки, я показываю информацию о наличии товара в выпадающем меню. это код, который я использую:

 $html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

if ( ! empty( $options ) ) {
if ( $product amp;amp; 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 ) ) {

$stock_status = get_variation_stock_status( $product, $name, $term->slug );

$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ).$stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}

$html .= '</select>';

return $html;
}

function get_variation_stock_status( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$variation_obj = wc_get_product( $variation['variation_id'] );
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}

return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme') : ' - ' . $stock_qty . '  ' .  __(pll__('adet Stokta'), 'mytheme-');

}
 

Если количество на складе превышает 20, я не хочу показывать эту информацию. как я могу это сделать?

Комментарии:

1. alexander-st.com/tr/urun/leopar-desenli-gomlek

2. Можете ли вы опубликовать полный код, чтобы иметь возможность протестировать код?

3. Всем привет. тот же код на этой странице. : springerdigital.co.uk /…

Ответ №1:

Просто добавьте проверку в get_variation_stock_status функцию:

 function get_variation_stock_status( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if ( $variation['attributes'][$name] == $term_slug ) {
            $variation_obj = wc_get_product( $variation['variation_id'] );

            // if the stock of the product is greater than 20 it returns
            if ( $variation_obj->get_stock_quantity() > 20 ) {
                return '';
            }
            
            $stock_qty = $variation_obj->get_stock_quantity();
            break;
        }
    }
    return $stock_qty == 0 ? ' - (Out Of Stock)' : ' - ' . $stock_qty . ' In Stock';
}
 

Весь код (по ссылке в вашем комментарии) был протестирован и работает. Добавьте его в свою активную тему functions.php .

Комментарии:

1. Я хочу реализовать это в простом продукте.