{"id":223,"date":"2020-06-21T06:20:03","date_gmt":"2020-06-21T06:20:03","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/how-can-i-display-product-attribute-name-in-woocommerce-cart-page\/"},"modified":"2024-11-04T04:03:39","modified_gmt":"2024-11-04T04:03:39","slug":"how-can-i-display-product-attribute-name-in-woocommerce-cart-page","status":"publish","type":"quick-tips","link":"https:\/\/demo.wpartisan.net\/developmentsite\/how-can-i-display-product-attribute-name-in-woocommerce-cart-page\/","title":{"rendered":"How can i display product attribute name in WooCommerce cart page"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If we want to show the product attribute name for a variable or simple product on the cart page, you need to add the below code on your theme functions.php.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_action('woocommerce_after_cart_item_name', 'wa_woocommerce_after_cart_item_name',10,2);\n\nfunction wa_woocommerce_after_cart_item_name( $cart_item, $cart_item_key ){\n\t$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );\n\t$attributes = $_product->get_attributes();\n\n\tif ( !empty( $attributes ) ) {\n\t\t$attr_arr = [];\n\t\tforeach( $attributes as $attr => $attr_val ) {\n\n\t\t\tif ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {\n\n\t\t\t\t$term_name = $_product->get_attribute( $attr );\n\n\t\t\t\tif ( !empty( $term_name ) ) {\n\t\t\t\t\t$attr_arr[] = $term_name;\n\t\t\t\t}\n\n\t\t\t}\n\t\t}\n\t}\n\n\tif ( !empty( $attr_arr )) {\n\t\techo implode( ',', $attr_arr );\n\t}\n\t\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In my case, I added an attribute name after the product&#8217;s title using an action hook called woocommerce_after_cart_item_name.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To show product attributes names on the WooCommerce cart page, you need to use an action hook called woocommerce_after_cart_item_name.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"tags":[],"topics":[43,44,45],"class_list":["post-223","quick-tips","type-quick-tips","status-publish","hentry","topics-woocommerce","topics-woocommerce-query","topics-wordpress"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"To show product attributes names on the WooCommerce cart page, you need to use an action hook called woocommerce_after_cart_item_name.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/quick-tips"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=223"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=223"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/topics?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}