如何获得woocommerce产品属性

时间:2015-01-02 09:23:02

标签: wordpress woocommerce

我尝试了许多代码和功能,以便在购物车页面上获取woocommerce产品属性...

function mycustom()
{
  $cart = WC()->cart->get_cart();
    foreach($cart as $key => $value)
    {
                $price = $value['data']->price;
                echo $price;
    }

}

这段代码给了我一个价格,但我需要产品属性。<​​/ p>

var_dump($cart)返回

array(1) { ["71abc9824e5d1f7ec18d323a87e8d4a9"]=> array(10) { ["product_id"]=> string(2) "87" ["variation_id"]=> string(2) "89" ["variation"]=> array(5) { ["Design"]=> string(21) "Custom_54a65f2320a149" ["type"]=> string(6) "custom" ["wbfd_type"]=> string(11) "wbfd_custom" ["wbfd_custom_price"]=> string(2) "12" **["Number of Cards"]=> string(2) "50"** } ["quantity"]=> int(1) ["data"]=> object(WC_Product_Variation)#121 (18) { ["variation_id"]=> int(89) ["parent"]=> object(WC_Product_Simple)#117 (3) { ["id"]=> int(87) ["post"]=> object(WP_Post)#120 (24) { ["ID"]=> int(87) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2015-01-02 05:31:53" ["post_date_gmt"]=> string(19) "2015-01-02 05:31:53" ["post_content"]=> string(0) "" ["post_title"]=> string(26) "Baby Shower Invitations B1" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(28) "baby-shower-invitations-b1-2" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2015-01-02 06:15:51" ["post_modified_gmt"]=> string(19) "2015-01-02 06:15:51" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(58) "http://example.com" ["menu_order"]=> int(0) ["post_type"]=> string(7) "product" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } ["product_type"]=> string(6) "simple" } ["variation_shipping_class"]=> bool(false) ["variation_shipping_class_id"]=> bool(false) ["variation_has_sku"]=> bool(true) ["variation_has_length"]=> bool(true) ["variation_has_width"]=> bool(true) ["variation_has_height"]=> bool(true) ["variation_has_weight"]=> bool(true) ["variation_has_tax_class"]=> bool(true) ["variation_has_downloadable_files"]=> bool(true) ["variation_level_meta_data:protected"]=> array(11) { ["downloadable"]=> string(2) "no" ["virtual"]=> string(2) "no" ["manage_stock"]=> string(2) "no" ["sale_price_dates_from"]=> string(0) "" ["sale_price_dates_to"]=> string(0) "" ["price"]=> string(0) "" ["regular_price"]=> string(0) "" ["sale_price"]=> string(0) "" ["stock"]=> int(0) ["stock_status"]=> string(7) "instock" ["downloadable_files"]=> array(0) { } } ["variation_inherited_meta_data:protected"]=> array(7) { ["tax_class"]=> string(0) "" ["backorders"]=> string(2) "no" ["sku"]=> string(0) "" ["weight"]=> string(0) "" ["length"]=> string(0) "" ["width"]=> string(0) "" ["height"]=> string(0) "" } ["id"]=> int(87) ["post"]=> object(WP_Post)#120 (24) { ["ID"]=> int(87) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2015-01-02 05:31:53" ["post_date_gmt"]=> string(19) "2015-01-02 05:31:53" ["post_content"]=> string(0) "" ["post_title"]=> string(26) "Baby Shower Invitations B1" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(28) "baby-shower-invitations-b1-2" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2015-01-02 06:15:51" ["post_modified_gmt"]=> string(19) "2015-01-02 06:15:51" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(58) "http://example.com" ["menu_order"]=> int(0) ["post_type"]=> string(7) "product" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } ["product_type"]=> string(9) "variation" ["price"]=> string(2) "12" ["variation_data"]=> array(1) { ["attribute_wtd-variation"]=> string(13) "wtd-variation" } } ["line_total"]=> float(12) ["line_tax"]=> int(0) ["line_subtotal"]=> int(12) ["line_subtotal_tax"]=> int(0) ["line_tax_data"]=> array(2) { ["total"]=> array(0) { } ["subtotal"]=> array(0) { } } } }

我只需要50个粗体文字。 这是我的产品属性。<​​/ p>

2 个答案:

答案 0 :(得分:1)

function mycustom()
{
  global $woocommerce;
  $cart = $woocommerce->cart->get_cart();
    foreach($cart as $key => $value)
    {               
      echo $woocommerce->cart->get_item_data( $value);
    }

}

答案 1 :(得分:0)

默认情况下,购物车页面会显示变体的属性。大概你的主题是覆盖这个模板。您可以删除它或重命名cart.php模板,以便WooCommerce不会使用它。或者您可以使用wc_get_formatted_variation()显示格式化的属性列表:

function mycustom()
{
  $cart = WC()->cart->get_cart();
    foreach($cart as $key => $value)
    {
        $price = $value['data']->price;
        echo $price;
        if( $value['data']->is_type('variation') ){
            echo wc_get_formatted_variation( $value['data']->get_variation_attributes() );
        }

    }

}