简体   繁体   English

在 Woocommerce 购物车页面上显示特定的产品属性

[英]Display specific product attribute on Woocommerce cart page

I want to display a specific product attribute on the Woocommerce cart page and checkout page, below the product name in the table.我想在 Woocommerce 购物车页面和结帐页面上,在表格中的产品名称下方显示特定的产品属性。 Is this somehow possible with这以某种方式可能与

custom_display_attribute

? ?

You can use the below filter to display the attributes on the cart page.您可以使用以下过滤器在购物车页面上显示属性。 Reference link: https://isabelcastillo.com/show-woocommerce-product-attributes-on-cart-page参考链接: https : //isabelcastillo.com/show-woocommerce-product-attributes-on-cart-page

/**
* WooCommerce: show all product attributes, separated by comma, on cart page
*/
function isa_woo_cart_attribute_values( $cart_item, $cart_item_key ) {
  
    $item_data = $cart_item_key['data'];
    $attributes = $item_data->get_attributes();
      
    if ( ! $attributes ) {
        return $cart_item;
    }
      
    $out = $cart_item . '<br />';
      
    $count = count( $attributes );
      
    $i = 0;
    foreach ( $attributes as $attribute ) {
   
        // skip variations
        if ( $attribute->get_variation() ) {
             continue;
        }
   
        $name = $attribute->get_name();          
        if ( $attribute->is_taxonomy() ) {
 
            $product_id = $item_data->get_id();
            $terms = wp_get_post_terms( $product_id, $name, 'all' );
               
            // get the taxonomy
            $tax = $terms[0]->taxonomy;
               
            // get the tax object
            $tax_object = get_taxonomy($tax);
               
            // get tax label
            if ( isset ( $tax_object->labels->singular_name ) ) {
                $tax_label = $tax_object->labels->singular_name;
            } elseif ( isset( $tax_object->label ) ) {
                $tax_label = $tax_object->label;
                // Trim label prefix since WC 3.0
                $label_prefix = 'Product ';
                if ( 0 === strpos( $tax_label,  $label_prefix ) ) {
                    $tax_label = substr( $tax_label, strlen( $label_prefix ) );
                }
            }
            $out .= $tax_label . ': ';
 
            $tax_terms = array();              
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                array_push( $tax_terms, $single_term );
            }
            $out .= implode(', ', $tax_terms);
              
            if ( $count > 1 && ( $i < ($count - 1) ) ) {
                $out .= ', ';
            }
          
            $i++;
            // end for taxonomies
      
        } else {
  
            // not a taxonomy
              
            $out .= $name . ': ';
            $out .= esc_html( implode( ', ', $attribute->get_options() ) );
          
            if ( $count > 1 && ( $i < ($count - 1) ) ) {
                $out .= ', ';
            }
          
            $i++;
              
        }
    }
    echo $out;
}
add_filter( 'woocommerce_cart_item_name', isa_woo_cart_attribute_values, 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce购物车和结帐中显示特定的父产品属性 - Display specific parent product attribute in Woocommerce cart and checkout 为特定的 WooCommerce 产品属性显示页面上的术语列表 - Display for a specific WooCommerce product attribute the terms list on a page 获取产品类别并将其显示在购物车页面woocommerce上 - Get the product categories and display it on cart page woocommerce 在Woocommerce的购物车页面上显示产品摘录 - Display product excerpt on cart page in Woocommerce 在 WooCommerce 购物车页面上显示选定的产品变体 - Display selected product variation on WooCommerce cart page 在购物车和结帐页面上显示 Woocommerce 产品类别 - Display Woocommerce Product Category on cart and checkout page 在 woocommerce 产品页面中显示产品属性和分类 - display product attribute and taxonomy in woocommerce product page Woocommerce 在购物车页面上获取特定的属性值 - Woocommerce get specific attribute value on cart page 如何从购物车页面中删除特定产品属性WooCommerce的数量字段 - How to remove the quantity field from cart page for specific product attribute WooCommerce 在 WooCommerce 产品页面中显示数量变化的产品总数和购物车小计 - Display product total and cart subtotal on quantity change in WooCommerce product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM