本文介绍了Woocommerce:获取产品变体标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个插件,但无法获得正确的变体标题在 Woocommerce 中,我使用的变体称为展开层压".但是当我尝试获取变体的标题时,我得到:图表的变体 #781"
I'm writing a plugin and cannot get the correct variation titleIn Woocommerce the variation i use is called "unfolded laminated". but when i try to get the title of a variation i get: "Variation #781 of Chart"
这是使用的代码:
foreach ($order->get_items() as $item) {
$product_variation_id = $item['variation_id'];
if ($product_variation_id)
{
$product = new WC_Product($product_variation_id);
$productname = get_item_variations($product_variation_id);
}
else
{
$product = new WC_Product($item['product_id']);
$productname = get_the_title($item['product_id']);
}
}
如何获得正确的标题?
推荐答案
我实际上正在查找这个问题,试图弄清楚,这是我的解决方案,因为变体标题返回了一个 slug.
I was actually looking this up trying to figure it out and here's my solution because the variation title was returning a slug.
已在 WC 2.4.13 上测试
Tested on WC 2.4.13
$variation = new WC_Product_Variation($variation_id);
$title_slug = current($variation->get_variation_attributes());
$results = $wpdb->get_results("SELECT * FROM wp_terms WHERE slug = '{$title_slug}'", ARRAY_A);
$variation_title = $results[0]['name'];
这篇关于Woocommerce:获取产品变体标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!