将变体库存数量和状态添加到 Woocommerce 产品变体下拉列表

Ště*_*žek 6 php wordpress variations stock woocommerce

基于在 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;
}
Run Code Online (Sandbox Code Playgroud)

但是我对这段代码有一个问题:
例如。我有一个尺寸(库存)的产品:S(库存数量 2)、L(0)、XL(0)。当我选择变体 S - 它显示数量 2 - 这是正确的,但即使我选择变体 L 或 XL,也会显示相同的数量。- 那是错误的,因为它们为零。有人可以帮我解决这个问题吗?谢谢!

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

Loi*_*tec 0

您的代码中存在一些错误,并且有更好的方法在产品变体下拉列表中显示库存数量+库存状态。

\n\n

第一个函数是一个自定义函数,您将在其中定义要在产品变型下拉列表中显示的库存文本添加,该下拉列表由第二个函数处理。

\n\n

自 Woocommerce 3 起,您的最后一个函数get_total_stock()已被弃用并由方法 代替get_stock_quantity()。此外,您还需要使用$product作为参数包含在挂钩函数中的变体对象。

\n\n
\n

注意:这仅适用于具有一个下拉菜单的可变产品 (一种为变体定义的产品属性)

\n
\n\n

这是重新审视的代码:

\n\n
// Function that will check the stock status and display the corresponding additional text\nfunction get_variation_stock_text( $product, $name, $term_slug ){\n    foreach ( $product->get_available_variations() as $variation ){\n        if($variation[\'attributes\'][$name] == $term_slug ){\n            $is_in_stock = $variation[\'is_in_stock\'];\n            $stock_qty   = get_post_meta($variation[\'variation_id\'], \'_stock\', true);\n        }\n    }\n    $in_stock     = \' (\'.$stock_qty.\' \' .__("Skladem", "woocommerce").\')\';\n    $out_of_stock = \' (\'.__("Vyprod\xc3\xa1no", "woocommerce").\')\';\n\n    return $is_in_stock == 1 ? $in_stock : $out_of_stock;\n}\n\n// The hooked function that will add the stock text to the dropdown options elements.\nadd_filter( \'woocommerce_dropdown_variation_attribute_options_html\', \'show_stock_status_in_dropdown\', 10, 2);\nfunction show_stock_status_in_dropdown( $html, $args ) {\n    // Only if there is a unique variation attribute (one dropdown)\n    if( sizeof($args[\'product\']->get_variation_attributes()) == 1 ) :\n\n    $options               = $args[\'options\'];\n    $product               = $args[\'product\'];\n    $attribute             = $args[\'attribute\']; // The product attribute taxonomy\n    $name                  = $args[\'name\'] ? $args[\'name\'] : \'attribute_\' . sanitize_title( $attribute );\n    $id                    = $args[\'id\'] ? $args[\'id\'] : sanitize_title( $attribute );\n    $class                 = $args[\'class\'];\n    $show_option_none      = $args[\'show_option_none\'] ? true : false;\n    $show_option_none_text = $args[\'show_option_none\'] ? $args[\'show_option_none\'] : __( \'Choose an option\', \'woocommerce\' );\n\n    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {\n        $attributes = $product->get_variation_attributes();\n        $options    = $attributes[ $attribute ];\n    }\n\n    $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\' ) . \'">\';\n    $html .= \'<option value="">\' . esc_html( $show_option_none_text ) . \'</option>\';\n\n    if ( ! empty( $options ) ) {\n        if ( $product && taxonomy_exists( $attribute ) ) {\n            $terms = wc_get_product_terms( $product->get_id(), $attribute, array( \'fields\' => \'all\' ) );\n\n            foreach ( $terms as $term ) {\n                if ( in_array( $term->slug, $options ) ) {\n                    // HERE Added the function to get the stock text\n                    $stock_text = get_variation_stock_text( $product, $name, $term->slug );\n\n                    $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>\';\n                }\n            }\n        } else {\n            foreach ( $options as $option ) {\n                $selected = sanitize_title( $args[\'selected\'] ) === $args[\'selected\'] ? selected( $args[\'selected\'], sanitize_title( $option ), false ) : selected( $args[\'selected\'], $option, false );\n                // HERE Added the function to get the stock text\n                $stock_text = get_variation_stock_text( $product, $name, $option );\n\n                $html .= \'<option value="\' . esc_attr( $option ) . \'" \' . $selected . \'>\' .\n                esc_html( apply_filters( \'woocommerce_variation_option_name\', $option ) . $stock_text ) . \'</option>\';\n            }\n        }\n    }\n    $html .= \'</select>\';\n\n    endif;\n\n    return $html;\n}\n\n// Change product availability text\nadd_filter( \'woocommerce_get_availability_text\', \'filter_product_availability_text\', 10, 2);\nfunction filter_product_availability_text( $availability, $product ) {\n    $stock = $product->get_stock_quantity();\n    return $product->is_in_stock() ? $stock . \' \' . __("Skladem", "woocommerce") : __("Vyprod\xc3\xa1no", "woocommerce");\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

\n\n

基于如何将变体库存状态添加到 Woocommerce 产品变体下拉列表

\n