如何在添加到购物车消息中用 product_sku 替换 product_name [英] How to replace product_name by product_sku on Add to cart message

查看:36
本文介绍了如何在添加到购物车消息中用 product_sku 替换 product_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,此消息行将是:

By default, this message line will be:

产品 product_name 已添加到购物车

Product product_name has been added to cart

我想将 product_name 更改为 product_sku.有什么办法吗?非常感谢!

I want to change product_name to product_sku. Is there any way to do it? Thank you very much!

注意:这是我用来编辑消息内容的代码,但它没有添加 product_sku

add_filter ( 'wc_add_to_cart_message_html', 'wc_add_to_cart_message_custom', 10, 2 );

function wc_add_to_cart_message_custom( $message, $products ) {
    if ( empty( $products ) ) {
        return $message;
    }

    foreach( $products as $product_id => $qty ) {
        $titles[] = get_the_title( $product_id );
    }

    $titles = array_filter( $titles );
    $added_text = sprintf( _n( 'product %s', 'product %s', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
    $message = sprintf( '%s <img src="images/Success.svg" style="width: 24px"> <a style="font-weight: 800">Added: </a>%s', esc_html__( '', 'woocommerce' ), esc_html( $added_text ));

    return $message;
}

推荐答案

而不是收集产品的标题"即名称,您可以改为收集他们的 SKU.

instead of collecting the product "titles" ie names, you can collect their SKUs instead.

add_filter ( 'wc_add_to_cart_message_html', 'wc_add_to_cart_message_custom', 10, 2 );

function wc_add_to_cart_message_custom( $message, $products ) {
    if ( empty( $products ) ) {
        return $message;
    }

    foreach( $products as $product_id => $qty ) {
        // get product object from ID
        $product = wc_get_product( $product_id );
        // add product SKU to array
        $skus[] = $product->get_sku();
         
    }

    $skus = array_filter( $skus );
    $added_text = sprintf( _n( 'product %s', 'product %s', sizeof( $skus ), 'woocommerce' ), wc_format_list_of_items( $skus ) );
    $message = sprintf( '%s <img src="images/Success.svg" style="width: 24px"> <a style="font-weight: 800">Added: </a>%s', esc_html__( '', 'woocommerce' ), esc_html( $added_text ));

    return $message;
}

这篇关于如何在添加到购物车消息中用 product_sku 替换 product_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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