2015-06-01 50 views
0

我試圖在我的單個產品頁上的表上顯示我的自定義產品屬性的詳細信息。我可以得到產品差異SKU,但我似乎無法得到「標籤」(我能做的最好的就是獲得slu))。WooCommerce獲取可變產品屬性標籤

這是我目前正在使用的代碼:

<!-- ******* START PRODUCT VARIABLE TABLE SECTION ******* --> 
<?php 
global $woocommerce, $product, $post; 

if($product->is_type('variable')) : 

    $available_variations = $product->get_available_variations(); 

    // START CHECK IF VARIATIONS ARE PRESENT 
    if ($available_variations) : 

    $attributes = $product->get_attributes(); 
?> 

<div class="row"> 
    <div class="small-12 column"> 
     <table class="varations-table"> 
      <thead> 
       <tr> 
        <th class="small-4">ORDER #</th> 
        <th class="small-5">DESCRIPTION</th> 
        <th class="small-3">UOM</th>   
       </tr> 
      </thead> 

      <tbody> 

      <?php foreach ($available_variations as $prod_variation) : ?> 

       <?php 
        // get some vars to work with 
        $post_id = $prod_variation['variation_id']; 
        $post_object = get_post($post_id); 
       ?> 
       <tr> 
        <td><?php echo $prod_variation['sku']; ?></td> 

       <?php foreach ($prod_variation['attributes'] as $attr_name => $attr_value) : ?> 
        <td><?php echo $attr_value; ?></td> 
       <?php endforeach;?> 

        <td class="uom"><?php echo the_field('uom'); ?></td> 

       </tr> 
      <?php endforeach;?> 
      </tbody> 
     </table> 
    </div> 
</div> 

<?php 
    // END CHECK IF VARIATIONS ARE PRESENT 
    endif; 
endif; 
?> 
<!-- ******* END PRODUCT VARIABLE TABLE SECTION ******* --> 

有誰知道我怎麼能拉變量標籤(不只是塞,用str_replace函數用空格替換破折號())?

回答

3

如果你有slu,,你可以用get_taxonomy() function得到一個WordPress分類對象。因此,要獲得WooCommerce自定義屬性的標籤,您只需要執行get_taxonomy($attribute_slug)->labels->name

+0

這簡直太尷尬了。實際上我很慚愧,我沒有想到這一點。哈哈感謝您的幫助 – Ryan

-1
<?php if ($product->is_type('variable' )) : ?> 
       <div class="size"> 
        <span class="name-size">SIZE</span> 
        <span class="value-size"> 
         <?php 
         $attr = get_post_meta($post->ID, '_product_attributes', true); 
         echo $attr['size']['value']; 
         ?> 
        </span> 
       </div> 
      <?php endif; ?>