我希望能够列出变体选项的值。比如,我有一个灯笼,有12英寸、14英寸和16英寸三个尺寸可选。我想获取这些值。我一直在尝试使用foreach循环来获取这些值,但我需要一点帮助。以下是我的代码:
function test_func(){
global $woocommerce, $product, $post;
// test if product is variable
if( $product->is_type( 'variable' ) ){
$available_variations = $product->get_available_variations();
// var_dump($available_variations);
foreach( $available_variations as $key => $value ){
var_dump( $value['attributes'] ) ;
}
}
}
这是输出的结果:
array(1) { ["attribute_pa_size"]=> string(4) "12in" } array(1) { ["attribute_pa_size"]=> string(4) "14in" } array(1) { ["attribute_pa_size"]=> string(4) "16in" }
如您所见,我需要的值已经存在,但是我不知道如何将它们输出。
如果我使用 var_dump() 来输出 $available_variations,我会得到以下结果:
"["backorders_allowed"] => bool(false) ["dimensions"] => array(3) {["length"] => string(4) "11.8" ["width"] => string(4) "11.8" ["height"] => string(4) "11.8"} ["dimensions_html"] => string(21) "11.8 x 11.8 x 11.8 in" ["display_price"] => float(3.2) ["display_regular_price"] => float(3.2) ["image"] => array(18) {["title"] => string(11) "6712R-1.jpg" ["caption"] => string(0) "" ["url"] => string(59) "http://website/wp-content/uploads/2018/10/6712R-1.jpg" ["alt"] => string(0) "" ["src"] => string(67) "http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg" ["srcset"] => string(445) "http://website/wp-content/uploads/2018/10/6712R-1-600x600.jpg 600w, http://website/wp-content/uploads/2018/10/6712R-1-150x150.jpg 150w, http://website/wp-content/uploads/2018/10/6712R-1-300x300.jpg 300w, http://website/wp-content/uploads/2018/10/6712R-1-768x768.jpg 768w, http://website/wp-content/uploads/2018/10/6712R-1-1024x1024.jpg 1024w, http://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg 100w" ["sizes"] => string(31) "(max-width: 600px) 100vw, 600px" ["full_src"] => string(59) "http://website/wp-content/uploads/2018/10/6712R-1.jpg" ["full_src_w"] => int(2000) ["full_src_h"] => int(2000) ["gallery_thumbnail_src"] => string(67) "http://website/wp-content/uploads/2018/10/6712R-1-100x100.jpg" ["gallery_thumbnail_src_w"] => int(100) ["gallery_thumbnail_src_h"] => int(100) ["thumb_src"] => string(67) "http://website/wp-content/uploads/2018/10/6712R-1-300x300.jpg" ["thumb_src_w"] => int(300) ["thumb_src_h"] => int(300) ["src_w"] => int(600) ["src_h"] => int(600)} ["image_id"] => string(3) "164" ["is_downloadable"] => bool(false) ["is_in_stock"] => bool(true) ["is_purchasable"] => bool(true) ["is_sold_individually"] => string(
"["sku"] => string(5) "6712R" ["variation_description"] => string(0) "" ["variation_id"] => int(1462) ["variation_is_active"] => bool(true) ["variation_is_visible"] => bool(true) ["weight"] => string(0) "" ["weight_html"] => string(3) "N/A"} [1] => array(24) {["attributes"] => array(1) {["attribute_pa_size"] => string(4) "14in"} ["availability_html"] => string(51) "
这只是一个产品的示例,每个变化都有一个示例,但是这让您了解它的工作原理。如果您知道其他方法可以达到相同的效果,请告诉我。谢谢
$term_slug
。$term_name
为空。 - CubicInfinity