将父产品名称添加到 WooCommerce 中的每个购物车项目名称

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

我想在我的分组产品的购物车页面中显示父产品名称和子产品名称(购物车项目)。 我在链接产品下选择父产品数据作为分组产品 -> 分组产品添加子产品。

来自模板的代码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 )

这是我想要的每个购物车商品:

Parent Product > Child Product

2018 年 2 月更新

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!

方法是 在使用 woocommerce_add_cart_item_data 中的自定义函数将产品添加到购物车时,将分组产品 ID 包含在购物车对象中 操作挂钩(只有分组的产品 ID 将作为自定义数据添加到购物车对象中)。

然后,为了显示包含当前购物车商品名称的父分组产品名称,我们使用另一个挂钩在 woocommerce_cart_item_name 过滤器挂钩中的自定义函数。

查看此更新:

所以最后的代码是:

// 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;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已经过测试,适用于 WooCommerce 版本 3+

你会得到这个:

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