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: