如何在 Woocommerce Items 中显示变体名称 [英] How to display the variation name in Woocommerce Items

查看:29
本文介绍了如何在 Woocommerce Items 中显示变体名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 Ajax 弹出式购物车,其中将动态添加产品.除了产品变化外,一切正常.当一个可变产品添加到购物车时,它没有显示变体名称:

cart->get_cart();foreach($items as $item => $values) {$_product = wc_get_product( $values['data']->get_id());$product_link = get_permalink( $values['data']->get_id());$title = $_product->get_title();$variations = wc_get_formatted_cart_item_data($values,true);echo ''.$title.'</a>';回声 $variations;}?>

首先,您只需要使用 WC_Product 方法 get_name() (参见模板 cart/minicart.php 第 36 行) 在您的代码中替换该行:

$title = $_product->get_title();

与:

$title = $_product->get_name();

<块引用>

重要说明:在某些情况下,您需要添加以下几行(取决于您要显示的内容和位置):

//强制在产品名称中显示变体属性(在cart/minicart/checkout 中)add_filter('woocommerce_product_variation_title_include_attributes', '__return_true');//(可选)强制将产品变体属性显示为单独的格式化元数据(在购物车/迷你车/结帐中)add_filter('woocommerce_is_attribute_in_product_name', '__return_false');

代码位于活动子主题(或活动主题)的 functions.php 文件中.

为了测试,一旦将此代码添加到您主题的functions.php 文件中,请先清空购物车,因为购物车片段缓存在迷你购物车 (Ajax) 中.

这次将显示变体名称.

I am trying to make a Ajax popup cart where product will be add dynamically. Everything is working fine except the product variation. when a variable product added to cart its not showing the variation name:

<?php 
$items = WC()->cart->get_cart();
    foreach($items as $item => $values) {
        $_product       =  wc_get_product( $values['data']->get_id() );
        $product_link   = get_permalink( $values['data']->get_id() );
        $title          = $_product->get_title();
        $variations     = wc_get_formatted_cart_item_data($values,true);
        echo '<a href="'.$product_link.'">'. $title.'</a>';
        echo $variations;
    }
?>

解决方案

First, you just need to use WC_Product method get_name() (see in the template cart/minicart.php on line 36) replacing in your code the line:

$title          = $_product->get_title();

with:

$title          = $_product->get_name();

Important Note: In some cases you will need to add the following lines (depending on what you want to display and where):

// Force displaying variation attributes in the product name (in cart/minicart/checkout)
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_true' );
// (Optional) Force displaying product variation attributes as separated formatted metadata (in cart/minicart/checkout)
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

Code goes in functions.php file of the active child theme (or active theme).

To test it, once added this code to your theme's functions.php file, empty the cart first, as cart fragments are cached in mini cart (Ajax).

This time it will show the variation name.

这篇关于如何在 Woocommerce Items 中显示变体名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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