For Woocommerce grouped products I am displaying parent product name in cart and checkout pages using the code bellow:
// Adding the grouped product ID custom hidden field data in Cart object
add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty( $_REQUEST['add-to-cart'] ) && $product_id != $_REQUEST['add-to-cart'] ) {
$cart_item_data['custom_data']['grouped_product_id'] = $_REQUEST['add-to-cart'];
$data['grouped_product_id'] = $_REQUEST['add-to-cart'];
// below statement make sure every add to cart action as unique line item
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $data );
}
return $cart_item_data;
}
// Add the parent grouped product name to cart items names
add_filter( 'woocommerce_cart_item_name', 'custom_product_title_name', 10, 3 );
function custom_product_title_name( $cart_item_name, $cart_item, $cart_item_key ){
// Only in cart and checkout pages
if ( is_cart() || is_checkout() )
{
// The product object from cart item
$product = $cart_item['data'];
$product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';
// The parent product name and data
if( ! empty( $cart_item['custom_data']['grouped_product_id'] ) ){
$group_prod_id = $cart_item['custom_data']['grouped_product_id'];
$group_prod = wc_get_product($group_prod_id);
if ( ! $group_prod->is_type( 'grouped' ) ) return $cart_item_name;
$parent_product_name = $group_prod->get_name();
$group_prod_permalink = $group_prod->is_visible() ? $group_prod->get_permalink() : '';
if ( ! $product_permalink )
return $parent_product_name . ' > ' . $product->get_name();
else
return sprintf( '%s > %s', esc_url( $group_prod_permalink ), $parent_product_name, esc_url( $product_permalink ), $product->get_name() );
}
else
return $cart_item_name;
}
else
return $cart_item_name;
}
Code comes from here: Add the parent product name to each cart item names in WooCommerce
Now I would like to display also this parent product name in orders details and in back-end too.
I will be grateful for any help on this.
I have revisited the original code answer a bit and I have enabled the display of those parent products linked names on orders and email notifications:
// Adding the grouped product ID custom hidden field data in Cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 20, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty($_REQUEST['add-to-cart']) && $product_id != $_REQUEST['add-to-cart']
&& is_numeric($_REQUEST['add-to-cart']) ){
$group_prod = wc_get_product($_REQUEST['add-to-cart']);
if ( ! $group_prod->is_type( 'grouped' ) )
return $cart_item_data; // Exit
$cart_item_data['grouped_product'] = array(
'id' => $_REQUEST['add-to-cart'],
'name' => $group_prod->get_name(),
'link' => $group_prod->get_permalink(),
'visible' => $group_prod->is_visible(),
);
// Below statement make sure every add to cart action as unique line item
$cart_item_data['grouped_product']['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// Add the parent grouped product name to cart items names
add_filter( 'woocommerce_cart_item_name', 'custom_product_title_name', 20, 3 );
function custom_product_title_name( $cart_item_name, $cart_item, $cart_item_key ){
// The product object from cart item
$product = $cart_item['data'];
$product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';
// The parent product name and data
if( isset( $cart_item['grouped_product'] ) ){
$group_product = $cart_item['grouped_product'];
$group_prod_link = $product->is_visible() && is_cart() ? $group_product['link'] : '';
if ( ! $group_prod_link )
return $group_product['name'] . ' > ' . $product->get_name();
else
return sprintf(
'%s > %s',
esc_url( $group_prod_link ),
$group_product['name'],
esc_url( $product_permalink ),
$product->get_name()
);
}
else
return $cart_item_name;
}
// Save grouped product data in order item meta
add_action( 'woocommerce_checkout_create_order_line_item', 'added_grouped_order_item_meta', 20, 4 );
function added_grouped_order_item_meta( $item, $cart_item_key, $values, $order ) {
if( isset($values['grouped_product']) ){
$item_id = $item->get_id();
$grouped_data = $values['grouped_product'];
unset($grouped_data['unique_key']);
$item->update_meta_data( '_grouped_product', $grouped_data );
}
}
// Display grouped product linked names in order items (+ email notifications)
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 20, 3 );
function custom_order_item_name( $item_name, $item, $is_visible ) {
$product = $item->get_product();
$product_id = $item->get_product_id();
$product_permalink = $is_visible ? $product->get_permalink( $item ) : '';
$grouped_data = wc_get_order_item_meta( $item->get_id(), '_grouped_product', true );
if( empty($grouped_data) ){
$item_name = $product_permalink ? sprintf(
'%s',
esc_url( $product_permalink),
$item->get_name()
) : $item->get_name();
} else {
$item_name = $product_permalink ? sprintf(
'%s > %s',
$grouped_data['link'],
$grouped_data['name'],
esc_url( $product_permalink) ,
$item->get_name()
) : $grouped_data['name'] . ' > ' . $item->get_name();
}
return $item_name;
}
// Display on backend order edit pages
add_action( 'woocommerce_before_order_itemmeta', 'backend_order_item_name_grouped', 20, 3 );
function backend_order_item_name_grouped( $item_id, $item, $product ){
if( ! ( is_admin() && $item->is_type('line_item') ) ) return;
$grouped_data = wc_get_order_item_meta( $item_id, '_grouped_product', true );
if( empty($grouped_data) ) return;
$product_link = admin_url( 'post.php?post=' . $grouped_data['id'] . '&action=edit' );
$grouped_name_html = '' . esc_html( $grouped_data['name'] ) . '';
echo '<br><br><div class="wc-order-item-name">
<small><strong>'.__('Grouped parent').':</strong></small><br>
' . $grouped_name_html . '
</div>';
}
Code goes in function.php file of your active child theme (or theme);
Tested and works.
Related
I want to display the category & attribute (brand) of each product under the product name in the Cart/checkout page
Example:
"Name of product"
"Category | Brand"
As shown in this image from shop
I would like to display it the same way on the cart page
(and also Thank you page + Order details, but these are lower priority)
I have this code which adds the Category to the cart page, but how can i add the attribute/brand next to it? Refer to this image
add_filter( 'woocommerce_cart_item_name', 'category_under_at_cart', 99, 3);
function category_under_at_cart( $name, $cart_item, $cart_item_key ) {
$product_item = $cart_item['data'];
// make sure to get parent product if variation
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
}
$cat_ids = $product_item->get_category_ids();
$attributes = $product_item->get_attributes();
// if product has categories, concatenate cart item name with them
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( count( $cat_ids )) . ' ', ' | ','</span>');
return $name;
}
The following will display the formatted product category(ies) and product attribute "brand" term names on minicart, cart, checkout, customer order and email notifications:
// Custom funtion that return the formatted category(ies) and attribute 'brand' term names
function get_categories_and_brand_html( $product_id ){
$product = wc_get_product($product_id);
$cat_names = (array) wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );
$brand_name = $product->get_attribute('brand');
$output = '';
if ( ! empty($cat_names) || ! empty($brand_name) ) {
$output .= '</br><span class="posted_in">';
if ( ! empty($cat_names) ) {
$output .= implode(', ', $cat_names);
}
if ( ! empty($cat_names) && ! empty($brand_name) ) {
$output .= ' | ';
}
if ( ! empty($brand_name) ) {
$output .= $brand_name;
}
$output .= '</span>';
}
return $output;
}
// Display term names in minicart and cart page
add_filter( 'woocommerce_cart_item_name', 'category_brand_after_cart_item_name', 100, 3 );
function category_brand_after_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
$terms_html = get_categories_and_brand_html( $cart_item['product_id'] );
if ( ! is_checkout() && ! empty($terms_html) ) {
$item_name .= $terms_html;
}
return $item_name;
}
// Display term names in checkout page
add_filter( 'woocommerce_checkout_cart_item_quantity', 'category_brand_after_checkout_item_name', 100, 3 );
function category_brand_after_checkout_item_name( $quantity, $cart_item, $cart_item_key ) {
$terms_html = get_categories_and_brand_html( $cart_item['product_id'] );
if ( is_checkout() && ! empty($terms_html) ) {
$quantity .= $terms_html;
}
return $quantity;
}
// Display term names on customer orders
add_filter( 'woocommerce_order_item_quantity_html', 'category_brand_after_order_item_name', 100, 2 );
function category_brand_after_order_item_name( $item_name, $item ) {
$terms_html = get_categories_and_brand_html( $item->get_product_id() );
if ( is_wc_endpoint_url() && ! empty($terms_html) ) {
$item_name .= $terms_html;
}
return $item_name;
}
// Display term names on email notifications
add_filter( 'woocommerce_order_item_name', 'category_brand_after_email_item_name', 100, 3 );
function category_brand_after_email_item_name( $item_name, $item, $is_visible ) {
$terms_html = get_categories_and_brand_html( $item->get_product_id() );
if ( ! is_wc_endpoint_url() && ! empty($terms_html) ) {
$item_name .= $terms_html;
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I'm working on woocommerce project and I want to add a new class on Add to Cart button if product is already added in cart, Can you help me to find out with this.
I'm changing right now using this code
add_filter( 'woocommerce_product_single_add_to_cart_text', 'bbloomer_custom_add_cart_button_single_product' );
function bbloomer_custom_add_cart_button_single_product( $label ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if( get_the_ID() == $product->get_id() ) {
$label = __('Added', 'woocommerce');
}
}
return $label; }
text is changing but I want add class so I can add my css style
please help Thank You
The add to cart template which is in woocommerce/templates/loop/add-to-cart.php has a filter. So we can also over write the class using add_filter function in functions.php.
function woocommerce_custom_add_to_cart_class ( $html, $product, $args ) {
// Define the classes to be added
$class_to_append = "this_guys_in_cart";
// Check if product is in cart
$in_cart = WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) );
if ( $in_cart != '' ) {
// Append the extra class
$args['class'] = $args['class']." {$class_to_append}";
$html = sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() )
);
}
// Return Add to cart button
return $html;
}
add_filter( "woocommerce_loop_add_to_cart_link", "woocommerce_custom_add_to_cart_class", 10, 3 );
Note: This method is not recommended if add-to-cart.php filte is modified in theme templates
Bellow I have a custom shortcode function based on this answer code that displays a block of product data for both Simple products and each variation of a Variable product. The output of the variation blocks seems to be ordered by the ID of the variation itself.
For example, this is a screenshot of the frontend output:
Which you can see matches the order of the variation IDs (from smallest to largest) screenshot:
What I would like is to sort the variations by their prices instead (NOT by their IDs) from lowest to highest. Any help is appreciated.
This is the current customized code that I have:
add_shortcode("price_variation_table", "fs_custom_available_variations_table");
function fs_custom_available_variations_table( $atts ) {
global $post;
// Attributes
$atts = shortcode_atts(
array(
'id' => $post->ID
),
$atts, 'price_variation_table'
);
if( is_admin() ) return; // Only on front end
$product = wc_get_product($atts['id']); // Get the WC_Product Object
$output = '<div class="fs-product-data-wrapper">';
// Variable products
if( $product->is_type('variable'))
{
// Get available variations in the variable product
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
foreach( $available_variations as $variation )
$output .= fs_format_product_data_output( $variation['variation_id'] );
}
}
// Simple products
elseif( $product->is_type('simple'))
{
$output .= fs_format_product_data_output( $product->get_id() );
}
else return; // Exit
return $output .= '</div>'; // return always for a shortcode
}
// Utility funtion: getting and formtting product data
function fs_format_product_data_output( $the_id ){
$empty = __( '<em>(empty)</em>', 'woocommerce' );
// Get an instance of the WC_Product_Variation object
$product = wc_get_product( $the_id );
// Only wc_get_price_to_display() respect if product is to be displayed with or without including taxes
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
$sale_price = ! empty( $sale_price ) ? wc_price($sale_price) : $empty;
// can use this class is there is no sale price set
if ( ! $product->is_on_sale() ) {
$no_sale_price = ' no-sale-price';
}
$size = $product->get_attribute( 'pa_size' );
$size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;
$stock_qty = $product->get_stock_quantity();
$stock_qty = ! empty( $stock_qty ) ? $stock_qty : '0';
if ( $stock_qty <= 0 ) {
$stock_status = 'stock-sold-out';
}
else {
$stock_status = 'stock-available';
}
$output = '
<ul class="'. $stock_status .'">
<li class="fs-data-price">'.$price.' ea.</li>
<li class="fs-data-size">Size: '.$size.'</li>
<li class="fs-data-sale'. $no_sale_price .'">'.$sale_price.' ea. Preferred customer price</li>
<li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
<li class="fs-data-notice">Quantities change quickly!</li>
</ul>';
return $output;
}
Try the following lightly changed code, where each displayed variations will be sorted by regular price (low to high):
add_shortcode("price_variation_table", "custom_available_variations_table");
function custom_available_variations_table( $atts ) {
global $post;
// Attributes
$atts = shortcode_atts(
array(
'id' => $post->ID
),
$atts, 'price_variation_table'
);
if( is_admin() ) return; // Only on front end
$product = wc_get_product($atts['id']); // Get the WC_Product Object
$output = '<div class="fs-product-data-wrapper">';
// Variable products
if( $product->is_type('variable'))
{
// Get available variations in the variable product
$available_variations = $product->get_available_variations();
if( count($available_variations) > 0 ){
$variations_ids = array();
// First loop - set variations Ids in an array with regular prices
foreach( $available_variations as $variation ){
$product = wc_get_product( $variation['variation_id'] );
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$variations_ids[$variation['variation_id']] = $price;
}
// Sorting variation Ids using prices from lower to highest
natsort($variations_ids);
// 2nd Loop - Display formatted variation data
foreach( array_keys($variations_ids) as $variations_id ){
$output .= format_product_data_output( $variations_id );
}
}
}
// Simple products
elseif( $product->is_type('simple'))
{
$output .= format_product_data_output( $product->get_id() );
}
else return; // Exit
return $output .= '</div>'; // return always for a shortcode
}
// Utility funtion: getting and formatting product data
function format_product_data_output( $the_id ){
$empty = __( '<em>(empty)</em>', 'woocommerce' );
// Get an instance of the WC_Product_Variation object
$product = wc_get_product( $the_id );
// Only wc_get_price_to_display() respect if product is to be displayed with or without including taxes
$price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
$sale_price = ! empty( $sale_price ) ? wc_price($sale_price) : $empty;
$size = $product->get_attribute( 'pa_size' );
$size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;
$no_sale_price = ! $product->is_on_sale() ? ' no-sale-price' : '';
$size = $product->get_attribute( 'pa_size' );
$size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;
$stock_qty = $product->get_stock_quantity();
$stock_qty = ! empty( $stock_qty ) ? $stock_qty : '0';
$stock_status = $stock_qty <= 0 ? 'stock-sold-out' : 'stock-available';
$output = '
<ul class="'. $stock_status .'">
<li class="fs-data-price">'.$price.' ea.</li>
<li class="fs-data-size">Size: '.$size.'</li>
<li class="fs-data-sale'. $no_sale_price .'">'.$sale_price.' ea. Preferred customer price</li>
<li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
<li class="fs-data-notice">Quantities change quickly!</li>
</ul>';
return $output;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
In woocommerce, I have maisd wome customizations and I can add a custom text field in my products, Display the values in cart and checkout (see the screenshots below).
Text field in product page:
Value in the cart:
Value in the checkout:
But I can not get it to appear in the purchase details or in the administration section (see the screenshots below).
Checkout details without the value:
Administration orders without the value:
In my code below, could someone tell what I am doing wrong?
The code that I use in my functions.php file:
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
global $product;
$id = $product->get_id();
// Get the field name of InputText1
$InputText1Name = get_post_meta($id, 'InputText1', true);
if ((!empty(get_post_meta($id, $InputText1, true)))){
echo '<div id="InputText1">';
echo '<label>'.__($InputText1Name).'</label> <input type="text" name="$InputText1V">';
echo '</div>';
}
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['$InputText1V'] ) ) {
$cart_item_data[ '$InputText1V' ] = $_REQUEST['$InputText1V'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ){
// Get the product id inside the cart
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
}
// Get the field name of InputText1
$InputText1Name = get_post_meta($product_id, 'InputText1', true);
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['$InputText1V'] ) ) {
$custom_items[] = array( "name" => $InputText1Name, "value" => $cart_item['$InputText1V'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['$InputText1V'] ) ) {
wc_add_order_item_meta( $product_id, "$InputText1V", $values['$InputText1V'] );
}
}
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['$InputText1V']) update_post_meta( $order_id, '$InputText1Name', esc_attr($_POST['$InputText1V']));
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['$InputText1V']) update_user_meta( $user_id, '$InputText1V', esc_attr($_POST['$InputText1V']) );
}
add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__($InputText1V).':</strong> ' . get_post_meta( $order_id, $InputText1V, true ) . '</p>';
}
function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}
add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
function order_meta_customized_display( $item_id, $item, $product ){
$order_product_id = $item['product_id'];
$field1name = get_post_meta($order_product_id, 'InputText1', true);
echo'<br>';
print_r($InputText1V);
echo'<br>';
echo $field1name;
echo ': ';
}
There are errors and missing things in your code. You don't need all that functions too and you should really avoid to use $ (and if you can capitals) in your custom field slugs (or in key slugs and even in function names) in your code.
I have tested and revisited your code. Here is what you need and can remove everything else:
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
global $product;
$product_id = $product->get_id();
// Get the field name of InputText1
$label = get_post_meta($product_id, 'InputText1', true);
if( ! empty( $label ) ){
echo '<div id="InputText1">
<label>'.$label.':</label> <input type="text" name="custom_slug" value="">
</div>';
}
}
// Store custom field label and value in cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['custom_slug'] ) ) {
$cart_item_data['custom_data']['label'] = get_post_meta($product_id, 'InputText1', true);
$cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['custom_slug'] );
$cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// Display items custom fields label and value in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ){
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['custom_data'] ) ) {
$custom_items[] = array(
'name' => $cart_item['custom_data']['label'],
'value' => $cart_item['custom_data']['value'],
);
}
return $custom_items;
}
// Save item custom fields label and value as order item meta data
add_action('woocommerce_add_order_item_meta','save_in_order_item_meta', 10, 3 );
function save_in_order_item_meta( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_data'] ) ) {
wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
This way you will get the display in Order received, Order view, Edit order (admin) and email notifications…
One of my favorite members helgatheviking, gave me a good solution of my previous question to remove the quantity field from cart page for specific product attribute . Below is the function given by her.
add_filter( 'woocommerce_cart_item_quantity', 'remove_cart_item_quantity', 10, 2 );
function remove_cart_item_quantity( $product_quantity, $cart_item_key ){
$cart_item = WC()->cart->cart_contents[ $cart_item_key ];
if( $cart_item['data']->is_type( 'variation' ) ){
$attributes = $cart_item['data']->get_attributes();
// var_dump( $attributes );
if( array_key_exists( 'color', $attributes ) ){
$product_quantity = '';
}
}
return $product_quantity;
}
Now the $product_quantity; return a blank string.
Is it possible to show the name instead of Blank. $product_quantity = ''; What should I replace ? If Selected product has color Green The $product_quantity; should return green.
EXAMPLE:
$product_quantity = '$color'; How could I get the color string from product attribute which in the cart.
I Solve this actually $_pname = WC()->cart->get_item_data( $cart_item ); So you can use $product_quantity = str_ireplace( 'Choose Quantity:', '',$_pname) ;
add_filter( 'woocommerce_cart_item_quantity', 'remove_cart_item_quantity', 10, 2 );
function remove_cart_item_quantity( $product_quantity, $cart_item_key ){
$cart_item = WC()->cart->cart_contents[ $cart_item_key ];
if( $cart_item['data']->is_type( 'variation' ) ){
$attributes = $cart_item['data']->get_attributes();
$_pname = WC()->cart->get_item_data( $cart_item );
if( array_key_exists( 'choose-quantity', $attributes ) ){
$product_quantity = str_ireplace( 'Choose Quantity:', '',$_pname) ;
}
}
return $product_quantity;
}
Output: