Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

php - Remove attribute values from product variation title and show them on separate rows

I have a checkout page where I want to:

  • Remove selected product attribute values from product variation title item.
  • Remove the quantity from item title too.
  • Display the different product attribute value such as size, color and the quantity on different rows, for this product variation item.

I want to display on my checkout page:

Aria Sport Shorts (the product title)

Color: Dusty Pink

Size: Small

QTY: 1

Instead of this:

How it looks now

Is it possible? Where I should start to make this real?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATED

1) Since WooCommerce 3+, to remove attribute values from Product variation title and to display them in a separate row will need to use this 2 dedicated simple hooks (in checkout page).

  • Removing attribute values from Product variation title:
add_filter( 'woocommerce_product_variation_title_include_attributes', 'variation_title_not_include_attributes' );
function variation_title_not_include_attributes( $boolean ){
    if ( ! is_cart() )
        $boolean = false;
    return $boolean;
}
  • Display Product variation attributes label and values in separate rows:
add_filter( 'woocommerce_is_attribute_in_product_name', 'remove_attribute_in_product_name' );
function remove_attribute_in_product_name( $boolean){
    if ( ! is_cart() )
        $boolean = false;
    return $boolean;
}

2) Checkout page - Remove the quantity from the product title and add it back in a separate row.

  • Remove the quantity from the product title:
add_filter( 'woocommerce_checkout_cart_item_quantity', 'remove_product_variation_qty_from_title', 10, 3 );
function remove_product_variation_qty_from_title( $quantity_html, $cart_item, $cart_item_key ){
    if ( $cart_item['data']->is_type('variation') && is_checkout() )
        $quantity_html = '';

    return $quantity_html;
}
  • Add back the cart item quantity in a separate row:
add_filter( 'woocommerce_get_item_data', 'filter_get_item_data', 10, 2 );
function filter_get_item_data( $item_data, $cart_item ) {

    if ( $cart_item['data']->is_type('variation') && is_checkout() )
        $item_data[] = array(
            'key'      => __('QTY'),
            'display'  => $cart_item['quantity']
        );

    return $item_data;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested in Woocommerce version 3+ and works. You should maybe need to make some styling changes…


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...