将父产品名称添加到WooCommerce中的每个购物车项目名称中 [英] Add the parent product name to each cart item names in WooCommerce

查看:69
本文介绍了将父产品名称添加到WooCommerce中的每个购物车项目名称中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



模板 <$ c $中的代码c> 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)

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


父产品>子产品



解决方案

已于2018-02年更新


以经典方式不可能父分组产品名称或分组产品ID,因为它们在购物车对象中不存在!


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



然后显示带有当前购物车商品名称的父分组产品名称,我们使用另一个绑定在 woocommerce_cart_item_name 上的自定义函数code> 过滤器挂钩。



查看此更新:


注意:分组的产品名称具有其自己的永久链接



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

解决方案

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

这篇关于将父产品名称添加到WooCommerce中的每个购物车项目名称中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆