当前位置: 首页 > 知识库问答 >
问题:

将WooCommerce产品名称中的自定义字段值附加到购物车和结账处

南宫书
2023-03-14

我正在尝试更改购物车和结帐页面中的产品名称。

我有以下代码添加一些购物车元数据:

function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }

    if( isset( $cart_item['sample_name'] ) ) {
        $custom_items[] = array( "name" => $cart_item['sample_name'], "value" => $cart_item['sample_value'] );
    }
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

但是我也想改变产品的名称。

例如,如果产品名称是Apple自定义字段'sample\u value'值是加糖,我想得到苹果(加糖)

我该如何实现这一点?

共有1个答案

司马腾
2023-03-14

在计算总计之前使用woocommerce\u钩住的自定义函数action钩子:

// Changing the cart item name
add_action( 'woocommerce_before_calculate_totals', 'customizing_cart_items_name', 20, 1 );
function customizing_cart_items_name( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through each cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Continue if we get the custom 'sample_name' for the current cart item
        if( empty( $cart_item['sample_name'] ) ){
            // Get an instance of the WC_Product Object
            $product = $cart_item['data'];
            // Get the product name (Added compatibility with Woocommerce 3+)
            $product_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
            // The new string composite name
            $product_name .= ' (' . $cart_item['sample_name'] . ')';

            // Set the new composite name (WooCommerce versions 2.5.x to 3+)
            if( method_exists( $product, 'set_name' ) ) 
                $product->set_name( $product_name );
            else
                $product->post->post_title = $product_name;
        }
    }
}

代码function.php活动子主题(或主题)的文件中,或者也在任何插件文件中。

此代码经过测试并正常工作。

 类似资料:
  • 我正在尝试获取WooCommerce产品的自定义字段值,以显示在商店系统的不同页面上。我成功地对单个产品页面执行了此操作,但相同的代码不适用于购物车。 对于单个产品,我在函数中使用了此代码。php,它可以很好地工作: 我为购物车尝试了相同的代码,但这次值没有显示在页面上。我还提到了这个线程(获取自定义属性),将$post更改为$product,但仍然没有输出。。。 有什么建议吗?

  • WooCommerce中,非Ajax情况下,点击加入购物车按钮后跳转到何处可以通过filter:add_to_cart_redirect修改,下面代码可以实现产品加入购物车后直接结账的功能,跳过购物车页面。 如果你希望Shop首页或分类页中的加入购物车按钮也具备这种效果,不要勾选Enable AJAX add to cart buttons on archives功能。否则该代码只在单个产品页面有

  • 我想知道是否有人对以下场景有经验。 我正在使用WooCommerce将我的产品上市,准备出售。一些产品(大约20个)有大量的变化,因此我决定将产品数据放在单独的表中,并通过SKU(获取数据的简单查询)将其加载到woocommerce中。到目前为止,一切都很顺利。这些产品很好用。它们的变体正在显示,用户可以选择它们,将它们添加到购物车中。 问题是,我已经将这20种产品设置为单一产品,并给它们一个$1

  • 我已经在woocommerce单一产品页面上添加了一些自定义选项,使用了下面关于我的主题函数的代码。php: 现在我想在购物车页面上显示所选的选项值。请帮我做这件事。谢啦

  • 我的职责是编写以下代码: 有没有人能把我推向正确的方向,让我在购物车、结帐、邮件和订单中获得正确的值? 最初取自这里在WooCommerce 3中到处显示ACF产品自定义字段

  • 我在我的产品页面中创建了一个自定义保修字段,通过function.php. 我想“复制”这个自定义字段的键和值,在一个自我生成的自定义字段的管理订单页面上,根据产品在购物车/订单(无插件)。 因此,通过订单页面上的这个自定义字段,我将最终能够使用WooCommerce pdf发票插件在我的pdf发票中显示“保修”。 另一种解释是: 作为管理员,我在“产品1”页面填写保修值 当订单中有“produc