2017-08-03 16 views
2

私はwoocommerceカートページで特定の属性値を取得しようとしています。 テーブルのカスタム列をのように作成しました。
Woocommerceはカートのページに特定の属性値を取得します

<td class="product-color" data-title="<?php esc_attr_e('Color', 'woocommerce'); ?>"> 
         <?php 
          echo apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key); 
         ?> 
        </td> 

        <td class="product-size" data-title="<?php esc_attr_e('Size', 'woocommerce'); ?>"> 
         <?php 
          echo apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key); 
         ?> 
        </td> 

        <td class="product-price" data-title="<?php esc_attr_e('Price', 'woocommerce'); ?>"> 
         <?php 
          echo apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key); 
         ?> 
        </td> 

あなたはそれらのすべてが製品価格を見るAS。私は色とサイズの属性を取得しようとしています。 私は、この検索し、見つかった:

<?php 

          echo apply_filters('woocommerce_cart_item_color', WC()->$product->get_attributes($_product), 
          $cart_item, $cart_item_key); 
         ?> 

しかし

私の最新の動作しませんでした。私はそのようにそれを試してみました

foreach(wc_get_product_terms($product->id, 'pa_size') as $attribute_value){ 
// Outputting the attibute values one by one 
echo $attribute_value . '<br>'; 

}

をtry:

$test = $_product->get_attributes(); 
foreach($test['pa_size']['options'] as $size){ 
          if ($size !== NULL) { 
         echo apply_filters('woocommerce_cart_item_price', $size , $cart_item, $cart_item_key); 
           var_dump($size); 

         } else { 
           echo "Not Specified"; 
          } 
         } 

と私は、この結果はintを得た(48)INT(47)

誰も私を助けてくださいすることができます。

+0

は、私はここで、この問題を解決: https://stackoverflow.com/questions/45520024/wordpress-woocommerce-product-attribute-return-key -not-value/45521680#45521680 –

答えて

2

カートページの属性と値を取得する方法は?

function.phpファイル内の関数の下に追加してください

<?php 
/** 
* WooCommerce: show all product attributes listed below each item on Cart page 
* ------ 
*/  

function wp_woo_cart_attributes($cart_item, $cart_item_key) { 

    $item_data = $cart_item_key['data']; 
    $attributes = $item_data->get_attributes(); 


    if (! $attributes) { 
     return $cart_item; 
    } 

    $out = $cart_item . '<br />'; 

    foreach ($attributes as $attribute) { 

     // skip variations 
     if ($attribute->get_variation()) { 
      continue; 
     } 
     $name = $attribute->get_name(); 
     if ($attribute->is_taxonomy()) { 

      $product_id = $item_data->get_id(); 
      $terms = wp_get_post_terms($product_id, $name, 'all'); 

      if (! empty($terms)) { 
       if (! is_wp_error($terms)) { 

        // get the taxonomy 
        $tax = $terms[0]->taxonomy; 

        // get the tax object 
        $tax_object = get_taxonomy($tax); 

        // get tax label 
        if (isset ($tax_object->labels->singular_name)) { 
         $tax_label = $tax_object->labels->singular_name; 
        } elseif (isset($tax_object->label)) { 
         $tax_label = $tax_object->label; 
         // Trim label prefix since WC 3.0 
         $label_prefix = 'Product '; 
         if (0 === strpos($tax_label, $label_prefix)) { 
          $tax_label = substr($tax_label, strlen($label_prefix)); 
         } 
        } 
        $out .= $tax_label . ': '; 
        $tax_terms = array(); 
        foreach ($terms as $term) { 
         $single_term = esc_html($term->name); 
         array_push($tax_terms, $single_term); 
        } 
        $out .= implode(', ', $tax_terms). '<br />'; 

       } 
      } 

     } else { 

      // not a taxonomy 

      $out .= $name . ': '; 
      $out .= esc_html(implode(', ', $attribute->get_options())) . '<br />'; 
     } 
    } 
    echo $out; 
} 

add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2); 
?> 
+0

再生いただきありがとうございます。それは私が必要とするものではありません。私は項目の下に属性を表示したくありません。私はテーブルの列としてそれらを表示したい。 –

関連する問題