1
我使用这显示WooCommerce产品名称:如何在WooCommerce中显示产品变体的标题?
<?php print $_product->get_title(); ?>
但仅显示父产品称号。如何显示变体产品的标题?
例如,我想显示“T恤衫尺寸较大”而不是“T恤衫”。
我使用这显示WooCommerce产品名称:如何在WooCommerce中显示产品变体的标题?
<?php print $_product->get_title(); ?>
但仅显示父产品称号。如何显示变体产品的标题?
例如,我想显示“T恤衫尺寸较大”而不是“T恤衫”。
所以,作为一个起点,你想要的是获得WC_Product_Variation
对象。这是非常简单的,一旦你有variation_id
:
$variation = $product->get_child($variation_id);
(如果你不直接拥有它,你可能想看看get_available_variations
)
然后,你可以写为简单的东西:
$attributes = $variation->get_variation_attributes();
echo $product->get_title() . ' Size ' . $attributes['attribute_size'];
如果您有多个属性,则可能需要循环访问$attributes
数组。
让我知道如果你遇到任何问题,
编辑 - 在$attributes
阵列前缀attribute_
的实际属性名称之前实现。它现在应该工作。