2014-02-10 21 views
2

以下代碼用於使用Woocommerce在我的Wordpress網站上獲取「書籍」類別產品的數據。從Wordpress獲取產品重量

<?php 
$args = array('post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'books'); 
     $loop = new WP_Query($args); 


    $send_array = array(); 
     while ($loop->have_posts()) : $loop->the_post(); 
     global $product; 

    $send_array[] = array(

     'id' => get_the_ID(), 
     'title' => get_the_title(), 
     'content' => get_the_content(), 
     'regular_price' => get_post_meta(get_the_ID(), '_regular_price', true), 
     'sale_price'=> get_post_meta(get_the_ID(), '_sale_price', true), 
     'weight' => woocommerce_get_product_terms($product->id, 'weight', 'names') 

    ); 

    endwhile; 

    wp_reset_query(); 
     ob_clean(); 
     echo json_encode($send_array); 
     exit(); 

    ?> 

這工作正常,並返回數據正確,除了weight屬性。

這是一個使用Woocommerce在我的網站中設置的自定義屬性。在我的數據庫中,重量屬性顯示在wp_woocommerce_attribute_taxonomies表中。此表具有字段attribute_id,attribute_name,attribute_label等。

我上面用於獲取產品的weight值的代碼不起作用。

我也試過'weight' => get_post_meta(get_the_ID(), '_weight', true),但它顯示爲一個空字符串(weight:「」),即使該產品在網站上有一個Weight值。

我如何獲得每種產品的重量並將其添加到陣列中,正如我上面所嘗試的weight

一直在做一些研究,這似乎是項目的工作。我究竟做錯了什麼?

我該如何解決這個問題?

回答

1

這對我來說是很重要的一件產品。

$item_weight=wp_get_post_terms($post->ID, 'pa_weight', array("fields" => "names")); 

$send_array[] = array(

    'id' => get_the_ID(), 
    'title' => get_the_title(), 
    'content' => get_the_content(), 
    'regular_price' => get_post_meta(get_the_ID(), '_regular_price', true), 
    'sale_price'=> get_post_meta(get_the_ID(), '_sale_price', true), 
    'weight' => $item_weight[0] 

); 

這看着在循環定義後,爲pa_weight返回的屬性值。

1

Woo Commerce函數'woocommerce_get_product_terms'返回一個數組。我對Woo Commerce的這個函數的結果並不熟悉,但假設它不返回一個多維數組,你應該將這些條件賦值給你的$ send_array數組之外的變量,然後嘗試返回第一個項陣列。

$weight = woocommerce_get_product_terms($product->id, 'pa_weight', 'names'); 

然後你陣列將使用$權重變量,像這樣:

$send_array[] = array(

    'id' => get_the_ID(), 
    'title' => get_the_title(), 
    'content' => get_the_content(), 
    'regular_price' => get_post_meta(get_the_ID(), '_regular_price', true), 
    'sale_price'=> get_post_meta(get_the_ID(), '_sale_price', true), 
    'weight' => $weight[0] 

); 

如果不工作,你應該做的var_dump($權重)的變量看佑如何商務部返回該函數的結果。從這個輸出中,你應該能夠確定正確的索引來獲得你之後的結果。

+0

我想這一點,但隨後爲重點'weight'顯示爲「空」的價值,當我使用'的var_dump($權重)',頁面顯示空白。沒有返回。 – Tester

+0

你必須用'pa_'開始你的屬性鍵,所以重量應該是'pa_weight'。我爲重量設置了一個屬性來測試它,併爲我返回一個重量數組。 – pIxelnate

+0

你的意思是在Wordpress後臺儀表板中添加前言「pa_」?或者我只需在上面的代碼中改爲「pa_weight」,即'$ weight = woocommerce_get_product_terms($ product-> id,'pa_weight','names');' – Tester

0

也檢查這一個。

$items = WC()->cart->get_cart(); 
$x = 0; 
foreach($items as $item => $values) { 
    $_product2 = $values['data']; 

    $getProductDetail = wc_get_product($values['product_id']); 

    echo $getProductDetail->get_image(); // accepts 2 arguments (size,  attr) 

    $weight = $getProductDetail->product_attributes; 
    echo 'Weight: '.$weight["weight"]["value"].'<br>'; 
} 
2
global $product;  
$product->get_weight(); 
+0

歡迎來到SO。請在答案中提供一點解釋,否則有人可能會將其標記爲「非常低質量的答案」。 – Syfer