Add the parent product name to each cart item names in WooCommerce

家住魔仙堡 提交于 2019-12-11 03:02:09

问题


I Would like to display the parent product name with child product name (cart item) in cart page, for my grouped products. I am selecting Parent product data as grouped product under linked product -> Grouped products adding child product.

The code from template cart.php:

echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key )

This is what I would like for each cart item:

Parent Product > Child Product


回答1:


UPDATED on 2018-02

Is not possible in a classic way to get the parent grouped product name or the grouped product ID as they don't exist in cart object!

The way is to include the grouped product id in cart object when a product is added to cart using a custom function hooked in woocommerce_add_cart_item_data action hook (only grouped product id will be added as custom data in cart object).

Then to display the parent grouped product name with the current cart item name we use another custom function hooked in woocommerce_cart_item_name filter hook.

See this update: Display parent grouped product name in Woocommerce orders details

So the final code is:

// Adding the grouped product ID custom hidden field data in Cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {

    if( ! empty( $_REQUEST['add-to-cart'] ) && $product_id != $_REQUEST['add-to-cart'] ) {
        $cart_item_data['custom_data']['grouped_product_id'] = $_REQUEST['add-to-cart'];
        $data['grouped_product_id'] = $_REQUEST['add-to-cart'];

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $data );
    }
    return $cart_item_data;
}


// Add the parent grouped product name to cart items names
add_filter( 'woocommerce_cart_item_name', 'custom_product_title_name', 10, 3 );
function custom_product_title_name( $cart_item_name, $cart_item, $cart_item_key ){
    // Only in cart and checkout pages
    if ( is_cart() || is_checkout() )
    {
        // The product object from cart item
        $product = $cart_item['data'];
        $product_permalink = $product->is_visible() ? $product->get_permalink( $cart_item ) : '';

        // The parent product name and data
        if( ! empty( $cart_item['custom_data']['grouped_product_id'] ) ){
            $group_prod_id = $cart_item['custom_data']['grouped_product_id'];
            $group_prod = wc_get_product($group_prod_id);
            if ( ! $group_prod->is_type( 'grouped' ) ) return $cart_item_name;
            $parent_product_name = $group_prod->get_name();
            $group_prod_permalink = $group_prod->is_visible() ? $group_prod->get_permalink() : '';

            if ( ! $product_permalink )
                return $parent_product_name . ' > ' . $product->get_name();
            else
                return sprintf( '<a href="%s">%s</a> > <a href="%s">%s</a>', esc_url( $group_prod_permalink ), $parent_product_name, esc_url( $product_permalink ), $product->get_name() );
        }
        else
            return $cart_item_name;
    }
    else
        return $cart_item_name;
}

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

This code is tested and works for WooCommerce version 3+

You will get this:

NOTE: The grouped product name has it's own permalink



来源:https://stackoverflow.com/questions/45522014/add-the-parent-product-name-to-each-cart-item-names-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!