WooCommerce - 在购物车中显示多个变体名称作为产品名称
WooCommerce - Display more than one variation name as product name in cart
我想用两个选定的变体替换购物车中的产品名称。理想情况下采用这种格式:
变体 1 - 变体 2
使用以下代码它可以工作,但只显示第一个变体的名称。
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
$new_name = wc_gzd_get_product( $product )->get_attribute('pa_attribute3';
if( method_exists( $product, 'set_name' ) )
$product->set_name( $new_name );
else
$product->post->post_title = $new_name;
}
}
有没有人找到这个错误或知道它是如何工作的?
非常感谢!
我已经解决了
我采用上面的代码使用属性 3 作为项目名称并使用此代码添加属性 1 和 2:
function show_things_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$att1 = $product->get_attribute('pa_attribute1');
$att2 = $product->get_attribute('pa_attribute2');
{
$item_name = '<class="product-model">' . $att1 . __( " ", "woocommerce") . $att2 . __( " - ", "woocommerce") . $item_name;
}
return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'show_things_in_cart_items', 99, 3 );
感谢用户 LoicTheAztec 提供此代码的基础,我只是根据需要对其进行了修改。
我在这里找到它:https://wordpress.stackexchange.com/questions/348631/adding-product-sku-before-cart-item-name-in-woocommerce
我想用两个选定的变体替换购物车中的产品名称。理想情况下采用这种格式:
变体 1 - 变体 2
使用以下代码它可以工作,但只显示第一个变体的名称。
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
$original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
$new_name = wc_gzd_get_product( $product )->get_attribute('pa_attribute3';
if( method_exists( $product, 'set_name' ) )
$product->set_name( $new_name );
else
$product->post->post_title = $new_name;
}
}
有没有人找到这个错误或知道它是如何工作的?
非常感谢!
我已经解决了
我采用上面的代码使用属性 3 作为项目名称并使用此代码添加属性 1 和 2:
function show_things_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$att1 = $product->get_attribute('pa_attribute1');
$att2 = $product->get_attribute('pa_attribute2');
{
$item_name = '<class="product-model">' . $att1 . __( " ", "woocommerce") . $att2 . __( " - ", "woocommerce") . $item_name;
}
return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'show_things_in_cart_items', 99, 3 );
感谢用户 LoicTheAztec 提供此代码的基础,我只是根据需要对其进行了修改。 我在这里找到它:https://wordpress.stackexchange.com/questions/348631/adding-product-sku-before-cart-item-name-in-woocommerce