簡體   English   中英

按庫存數量顯示

[英]display by stock quantity woocommorce

我正在准備一個帶有 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 && 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,我不想顯示此信息。 我怎樣才能做到這一點?

只需在get_variation_stock_status function 中添加檢查:

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';
}

整個代碼(來自您評論中的鏈接)已經過測試並且可以工作。 將其添加到您的活動主題的功能中。php。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM