簡體   English   中英

添加變體庫存數量和狀態到Woocommerce產品差異下拉列表

[英]Add variation stock quantity and status to Woocommerce product variation dropdown

基於在WooCommerce變量產品中顯示每個屬性值旁邊的庫存狀態 ,我有以下代碼,顯示產品變化下拉列表中的庫存數量+庫存狀態以及顯示的產品可用性文本:

add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1 );
function customizing_variations_terms_name( $term_name ){

    if(is_admin())
        return $term_name;

    global $product;
    $second_loop_stoped = false;

    // Get available product variations
    $product_variations = $product->get_available_variations();

    // Iterating through each available product variation
    foreach($product_variations as $variation){

        $variation_id = $variation['variation_id'];
        $variation_obj = new WC_Product_Variation( $variation_id );

        ## WOOCOMMERCE RETRO COMPATIBILITY ##
        if ( version_compare( WC_VERSION, '3.0', '<' ) ) # BEFORE Version 3 (older)
        {
            $stock_status = $variation_obj->stock_status;
            $stock_qty = intval($variation_obj->stock);

            // The attributes WC slug key and slug value for this variation
            $attributes_arr = $variation_obj->get_variation_attributes();
        }
        else # For newest verions: 3.0+ (and Up)
        {
            $stock_status = $variation_obj->get_stock_status();
            $stock_qty = $variation_obj->get_stock_quantity();

            // The attributes taxonomy key and slug value for this variation
            $attributes_arr = $variation_obj->get_attributes();
        }

        if(count($attributes_arr) != 1) // Works only for 1 attribute set in the product
            return $term_name;

        // Get the terms for this attribute
        foreach( $attributes_arr as $attr_key => $term_slug){
            // Get the attribute taxonomy
            $term_key = str_replace('attribute_', '', $attr_key );

            // get the corresponding term object
            $term_obj = get_term_by( 'slug', $term_slug, $term_key );
            if( $term_obj->name == $term_name ){ // If the term name matches we stop the loops
                $second_loop_stoped = true;
                break;
            }
        }
        if($second_loop_stoped)
            break;
    }
    if( $stock_qty>0 )
        return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
    else
        return $term_name .= ' - ' . $stock_status . ' (Vyprodáno)';
}

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $_product ) {
    global $product;
    $stock = $product->get_total_stock();

    if ( $_product->is_in_stock() ) $availability['availability'] = __($stock . ' Skladem', 'woocommerce');
    if ( !$_product->is_in_stock() ) $availability['availability'] = __('Vyprodáno', 'woocommerce');

  return $availability;
}

但是這個代碼有問題:
對於前 我有一個尺寸(股票)的產品:S(instock數量2),L(0),XL(0)。 當我選擇變量S - 它顯示數量2 - 這是正確的,但即使我選擇變量L或XL,也會顯示相同的數量。 - 這是錯誤的,因為它們在ZERO上。 任何人都可以幫我嗎? 謝謝!

你可以在這里看到它: https//dogworld.cz/produkt/pelisek-pro-psa-reedog-beige-paw/

您的代碼中存在一些錯誤,並且在產品變體下拉列表中顯示庫存數量+庫存狀態的更好方法。

第一個功能是自定義功能,您可以在其中定義要在產品變體下拉列表中顯示的庫存文本添加,該下拉菜單由第二個功能處理。

在您的上一個函數中,自Woocommerce 3以來,不推薦使用get_total_stock()並將其替換為方法get_stock_quantity() 此外,您還需要使用在鈎子函數中作為參數包含的變體$product對象。

注意:這僅適用於帶有一個下拉列表的變量產品 (變體的一個已定義的產品屬性)

這是重新訪問的代碼:

// Function that will check the stock status and display the corresponding additional text
function get_variation_stock_text( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if($variation['attributes'][$name] == $term_slug ){
            $is_in_stock = $variation['is_in_stock'];
            $stock_qty   = get_post_meta($variation['variation_id'], '_stock', true);
        }
    }
    $in_stock     = ' ('.$stock_qty.' ' .__("Skladem", "woocommerce").')';
    $out_of_stock = ' ('.__("Vyprodáno", "woocommerce").')';

    return $is_in_stock == 1 ? $in_stock : $out_of_stock;
}

// The hooked function that will add the stock text to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
    // Only if there is a unique variation attribute (one dropdown)
    if( sizeof($args['product']->get_variation_attributes()) == 1 ) :

    $options               = $args['options'];
    $product               = $args['product'];
    $attribute             = $args['attribute']; // The product attribute taxonomy
    $name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
    $id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );
    $class                 = $args['class'];
    $show_option_none      = $args['show_option_none'] ? true : false;
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );

    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
        $attributes = $product->get_variation_attributes();
        $options    = $attributes[ $attribute ];
    }

    $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 ) ) {
            $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

            foreach ( $terms as $term ) {
                if ( in_array( $term->slug, $options ) ) {
                    // HERE Added the function to get the stock text
                    $stock_text = get_variation_stock_text( $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_text ) . '</option>';
                }
            }
        } else {
            foreach ( $options as $option ) {
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
                // HERE Added the function to get the stock text
                $stock_text = get_variation_stock_text( $product, $name, $option );

                $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' .
                esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_text ) . '</option>';
            }
        }
    }
    $html .= '</select>';

    endif;

    return $html;
}

// Change product availability text
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
    $stock = $product->get_stock_quantity();
    return $product->is_in_stock() ? $stock . ' ' . __("Skladem", "woocommerce") : __("Vyprodáno", "woocommerce");
}

代碼位於活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

基於如何在Woocommerce產品變化下拉列表中添加變體庫存狀態

暫無
暫無

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

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