【问题标题】:Show the custom attribute "variable product" in the list of related products在相关产品列表中显示自定义属性“可变产品”
【发布时间】:2019-03-24 18:10:40
【问题描述】:

基于"How to add variation stock status to Woocommerce product variation dropdown" 的回答,
我编写了一些代码来显示可变产品的自定义属性及其在商店中的存在:

// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text($product, $name, $term_slug) {
    foreach($product - > get_available_variations() as $variation) {
            if ($variation['attributes'][$name] == $term_slug)
                    $stock = $variation['is_in_stock'];
    }

    return $stock == 1 ? '' : ' - out of stock';
}

// The hooked function that will add the stock status 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'] : __('Select size', '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 text status
                                    $stock_status = get_stock_status_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_status).
                                    '</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 text status
                            $stock_status = get_the_stock_status($product, $name, $option);
                            $html. = '<option value="'.esc_attr($option).
                            '" '.$selected.
                            '>'.esc_html(apply_filters('woocommerce_variation_option_name', $option).$stock_status).
                            '</option>';
                    }
            }
    }
    $html. = '</select>';

    endif;

    return $html;
}

在单个产品页面上,我展示了相关产品。如何在相关产品中以标签的形式显示这个属性(它在商店中的存在)?

例如:

产品名称

价格 - 25 美元

可用尺码:L、XL

【问题讨论】:

    标签: wordpress woocommerce attributes


    【解决方案1】:

    试试这段代码,我连接到woocommerce_after_shop_loop_item 并显示产品属性。现在它只适用于 size 属性。它显示大、中、小。如果要显示 L、XL、M 等,只需编辑 size 属性即可。

    add_action( 'woocommerce_after_shop_loop_item', 'mamham_woocommerce_after_shop_loop_item', 10);
    function mamham_woocommerce_after_shop_loop_item() {
        global $product;
    
        // Bail if not in the single product page.
        if ( ! is_product() ) return;
    
        // Prduct attributes which you want to show.
        // Attributes to show in the slug.
        // 'attribute slug' => 'Text shown';
        $show_attributes = array(
            'size' => 'Sizes available:',
        );
    
        // Get the slugs.
        $show_slugs = array_keys( $show_attributes );
    
        // Get product type.
        $product_type = $product->get_type();
    
        if( 'simple' === $product_type ) {  
            $product_attributes = $product->get_attributes();
            foreach( $product_attributes as $key => $product_attribute ) {
                $slug = substr( $key, 3 );
                if ( in_array( $slug, $show_slugs, TRUE ) ) {
                    $terms = $product_attribute->get_terms();
                    $terms_output = wp_list_pluck( $terms, 'name');
                    $terms_output = implode(', ', $terms_output );
                    echo esc_html( "${show_attributes[$slug]} ${terms_output}" );
                }
            }
    
        } elseif ( 'variable' === $product_type ) {
            $product_variations = $product->get_available_variations();     
            foreach ( $product_variations as $product_variation ) {
                if ( $product_variation['is_in_stock'] && isset( $product_variation['attributes']['attribute_pa_size'] )) {
                    $terms_output[] = ucwords( $product_variation['attributes']['attribute_pa_size'] );
                }           
            }       
            if ( ! empty( $terms_output) ) {
                $terms_output = implode(', ', $terms_output );
                echo esc_html( "Sizes available: ${terms_output}" );
            }       
        }
    }
    

    这是截图,我已经安装了店面主题和 WooCommerce。

    【讨论】:

    • 感谢您的回答。但是我的可变产品属性没有显示。您是显示所有产品属性还是仅显示有库存的产品属性?能否展示一下产品的属性是如何配置的?
    • 如果要显示可变产品和库存产品的属性,那么我必须更新代码。您要显示一个属性还是全部?
    • 我只想显示那些可用的属性。例如,我有不同尺寸的产品:S、M、L、XL、XXL。只有 L 和 XL 可用。这些是我想展示的尺寸。
    • 非常感谢您的帮助!一切正常。我会测试这个功能一段时间。
    • @Dmitry 如果发生任何事情,请告诉我。
    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多