gpt4 book ai didi

php - 如何在 Woocommerce 项目中显示变体名称

转载 作者:行者123 更新时间:2023-12-03 08:53:34 25 4
gpt4 key购买 nike

我正在尝试制作一个 Ajax 弹出式购物车,其中产品将动态添加。除了产品变化之外,一切都运行良好。当可变产品添加到购物车时,它不显示变体名称:

<?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;
}
?>

最佳答案

首先,您只需要使用 WC_Product method get_name() (请参阅模板 cart/minicart.php on line 36 ) 在代码中替换以下行:

$title          = $_product->get_title();

与:

$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).

这次它将显示变体名称。

关于php - 如何在 Woocommerce 项目中显示变体名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57384634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com