在 Woocommerce 订单详细信息中显示父分组产品名称 [英] Display parent grouped product name in Woocommerce orders details

查看:55
本文介绍了在 Woocommerce 订单详细信息中显示父分组产品名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Woocommerce 分组产品,我使用以下代码在购物车和结帐页面中显示父产品名称:

//在 Cart 对象中添加分组的产品 ID 自定义隐藏字段数据add_action('woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2);函数 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'];//下面的语句确保每个添加到购物车的操作都是唯一的订单项$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );WC()->session->set('custom_data', $data);}返回 $cart_item_data;}//将父分组产品名称添加到购物车项目名称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 ){//仅在购物车和结帐页面if ( is_cart() || is_checkout() ){//购物车项目中的产品对象$product = $cart_item['data'];$product_permalink = $product->is_visible() ?$product->get_permalink( $cart_item ) : '';//父产品名称和数据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' ) ) 返回 $cart_item_name;$parent_product_name = $group_prod->get_name();$group_prod_permalink = $group_prod->is_visible() ?$group_prod->get_permalink() : '';如果(!$product_permalink)返回 $parent_product_name .' >' .$product->get_name();别的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() );}别的返回 $cart_item_name;}别的返回 $cart_item_name;}

代码来自这里:

For Woocommerce grouped products I am displaying parent product name in cart and checkout pages using the code bellow:

// Adding the grouped product ID custom hidden field data in Cart object
add_action( '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 comes from here: Add the parent product name to each cart item names in WooCommerce

Now I would like to display also this parent product name in orders details and in back-end too.

I will be grateful for any help on this.

解决方案

I have revisited the original code answer a bit and I have enabled the display of those parent products linked names on orders and email notifications:

// 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', 20, 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']
    && is_numeric($_REQUEST['add-to-cart']) ){
        $group_prod = wc_get_product($_REQUEST['add-to-cart']);
        if ( ! $group_prod->is_type( 'grouped' ) )
            return $cart_item_data; // Exit

        $cart_item_data['grouped_product'] = array(
            'id' => $_REQUEST['add-to-cart'],
            'name' => $group_prod->get_name(),
            'link' => $group_prod->get_permalink(),
            'visible' => $group_prod->is_visible(),
        );

        // Below statement make sure every add to cart action as unique line item
        $cart_item_data['grouped_product']['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}


// Add the parent grouped product name to cart items names
add_filter( 'woocommerce_cart_item_name', 'custom_product_title_name', 20, 3 );
function custom_product_title_name( $cart_item_name, $cart_item, $cart_item_key ){
    // 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( isset( $cart_item['grouped_product'] ) ){
        $group_product = $cart_item['grouped_product'];
        $group_prod_link = $product->is_visible() && is_cart() ? $group_product['link'] : '';

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

// Save grouped product data in order item meta
add_action( 'woocommerce_checkout_create_order_line_item', 'added_grouped_order_item_meta', 20, 4 );
function added_grouped_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if( isset($values['grouped_product']) ){
        $item_id = $item->get_id();
        $grouped_data = $values['grouped_product'];
        unset($grouped_data['unique_key']);
        $item->update_meta_data( '_grouped_product', $grouped_data );
    }
}

// Display grouped product linked names in order items (+ email notifications)
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 20, 3 );
function custom_order_item_name( $item_name, $item, $is_visible ) {
    $product = $item->get_product();
    $product_id = $item->get_product_id();
    $product_permalink = $is_visible ? $product->get_permalink( $item ) : '';
    $grouped_data = wc_get_order_item_meta( $item->get_id(), '_grouped_product', true );
    if( empty($grouped_data) ){
        $item_name = $product_permalink ? sprintf(
            '<a href="%s">%s</a>',
            esc_url( $product_permalink),
            $item->get_name()
        ) : $item->get_name();
    } else {
        $item_name = $product_permalink ? sprintf(
            '<a href="%s">%s</a> > <a href="%s">%s</a>',
            $grouped_data['link'],
            $grouped_data['name'],
            esc_url( $product_permalink) ,
            $item->get_name()
        ) : $grouped_data['name'] . ' > ' . $item->get_name();
    }
    return $item_name;
}

// Display on backend order edit pages
add_action( 'woocommerce_before_order_itemmeta', 'backend_order_item_name_grouped', 20, 3 );
function backend_order_item_name_grouped( $item_id, $item, $product ){
    if( ! ( is_admin() && $item->is_type('line_item') ) ) return;

    $grouped_data = wc_get_order_item_meta( $item_id, '_grouped_product', true );
    if( empty($grouped_data) ) return;
    $product_link = admin_url( 'post.php?post=' . $grouped_data['id'] . '&action=edit' );
    $grouped_name_html = '<a href="' . esc_url( $grouped_data['link'] ) . '" class="wc-order-item-name">' . esc_html( $grouped_data['name'] ) . '</a>';
    echo '<br><br><div class="wc-order-item-name">
        <small><strong>'.__('Grouped parent').':</strong></small><br>
        ' . $grouped_name_html . '
    </div>';
}

Code goes in function.php file of your active child theme (or theme);

Tested and works.

这篇关于在 Woocommerce 订单详细信息中显示父分组产品名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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