我想用两个选定的变体替换购物车中产品的名称。理想的格式是:
变体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;
}
}
有人找到该错误或有其工作方式吗?
非常感谢!
答案 0 :(得分:0)
我已经解决了。
我使用上面的代码将属性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