在相关产品列表中显示自定义属性“可变产品”

德米特里

基于如何向Woocommerce产品差异下拉列表中添加差异库存状态答案,
我编写了一些代码来显示可变产品的自定义属性及其在商店中的存在:

// 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

萨加尔·巴哈杜尔·塔曼(Sagar Bahadur Tamang)

试试这个代码,我迷上了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。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

可变产品属性:自定义每个显示的单选按钮的文本值

将可变产品的自定义属性添加到WooCommerce中的购物车

WooCommerce可变产品的产品页面问题上显示的自定义文本字段

在WooCommerce电子邮件订购项目中显示可变产品的产品自定义字段

在单个产品页面上显示相关产品中的自定义属性

WooCommerce中可变产品的条件自定义价格后缀

在常规单个产品页面的自定义选项卡中显示“相关产品”

也可以使用ACF在WooCommerce购物车中为可变产品获取自定义字段

自定义格式的最低价格,包括特定WooCommerce可变产品的税费

WooCommerce可变产品:使用自定义标签仅保留“最低”价格

自定义 Woocommerce 可变产品价格范围,用于多个活动变体

在自定义家庭和产品类别档案中显示WooCommerce产品属性

在WooCommerce产品页面中显示自定义产品属性值

设置Woocommerce中可变产品的最低单位显示价格

在Woocommerce中的可变产品页面上显示变化权重?

WooCommerce获取可变产品属性标签

Woocommerce可变产品未正确显示

WooCommerce 仅显示可变产品的价格?

如何使用自定义属性过滤产品列表

显示WooCommerce产品属性的自定义分类术语图像

替换WooCommerce中的可变产品定价

使用短代码显示产品价格,包括 WooCommerce 中的可变产品

从WooCommerce相关产品自定义WP查询中删除缺货的产品

显示在admin Woocommerce产品列表页面中的自定义价格计算

从Magento 1.9中的产品获取自定义属性

magento虚拟产品中的自定义属性

可变产品价格未显示在列表和详细信息页面中WooCommerce价格(基于国家)

获取产品自定义属性以在WooCommerce产品循环中显示它们

显示WooCommerce自定义产品属性和单个产品上的所有术语