[英]How to show product attribute name in cart page
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 . 我最喜欢的一位会员helgatheviking ,给了我一个很好的解决方案,解决了我先前提出的问题 ,即从购物车页面删除特定产品属性的数量字段。 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;
现在
$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;
如果所选产品的颜色为绿色,则
$product_quantity;
should return green. 应该返回绿色。 EXAMPLE:
$product_quantity = '$color';
示例:
$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 );
我实际上解决了这个
$_pname = WC()->cart->get_item_data( $cart_item );
So you can use $product_quantity = str_ireplace( 'Choose Quantity:', '',$_pname) ;
因此,您可以使用
$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: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.