亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 WooCommerce 中獲取產品特定元數據

如何在 WooCommerce 中獲取產品特定元數據

PHP
楊魅力 2023-08-06 15:42:17
我正在使用 WooCommerce,并且想要獲取專門針對該產品的元密鑰,例如價格、顏色等...但問題是我不想獲取所有元密鑰,而只想獲取所需的元密鑰。例如,我不需要_edit_last, _edit_lock, _wc_review_count... 而只需要priceand color(作為示例)。如果它是一個不重要的網頁,但我不想在網頁上顯示它們,但我想將它們作為 json 發送到其他地方。所以應該對數據進行過濾。順便說一句,它在插件中,我想與其他人分享,所以我不能/不應該訪問他們的 WooCommerce api。我的代碼獲取所有元鍵(但我只想要其中的一些):$attributes = get_post_meta($product->get_id());foreach($attributes as $key => $value) {    $result['products'][$p]['attributes'][$key] = $value[0];}上面代碼的結果是:[attributes] => Array(    [_edit_last] => 1    [_edit_lock] => 1594817821:1    .    .    .)但我想要:[attributes] => Array(    [price] => 1000    [color] => 'red'    .    .    .)
查看完整描述

3 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

有多種方法,具體取決于您想要的:

1)。您將需要使用WC_Product 對象上的WC_Product方法來獲取產品屬性:

$product_id = 37; // Defined product Id for testing


// Get the WC_Product Object from the product ID (optional)

$product = wc_get_product($product_id); // If needed


// Get product active price

$product_data = $product->get_price();


// … and so on …

2)。對于特定或自定義產品元數據,您將使用使用所需元鍵的WC_Data方法,例如:get_meta()

$product_id = 37; // Defined product Id for testing


// Get the WC_Product Object from the product ID (optional)

$product = wc_get_product($product_id); // If needed


// Get custom meta data froma specific meta key

$product_data = $product->get_meta('my_meta_key');


// … and so on …

3)。您可以使用對象上的WC_Data方法get_data()WC_Product來獲取數組中不受保護的元數據:

$product_id = 37; // Defined product Id for testing


// Get the WC_Product Object from the product ID (optional)

$product = wc_get_product($product_id); // If needed


// Get product unprotected meta data in an array

$product_data = $product->get_data();


// Output row data

echo '<pre>'. print_r( $obj, true ) . '</pre>';

你會得到類似的東西(摘錄):


Array

(

? ? [id] => 37

? ? [name] => Happy Ninja Dish Hip

? ? [slug] => happy-ninja-dish

? ? [date_created] => WC_DateTime Object

? ? ? ? (

? ? ? ? ? ? [utc_offset:protected] => 0

? ? ? ? ? ? [date] => 2015-12-26 11:53:15.000000

? ? ? ? ? ? [timezone_type] => 3

? ? ? ? ? ? [timezone] => Europe/Paris

? ? ? ? )


? ? [date_modified] => WC_DateTime Object

? ? ? ? (

? ? ? ? ? ? [utc_offset:protected] => 0

? ? ? ? ? ? [date] => 2020-07-09 19:25:32.000000

? ? ? ? ? ? [timezone_type] => 3

? ? ? ? ? ? [timezone] => Europe/Paris

? ? ? ? )


? ? [status] => publish

? ? [featured] =>?

? ? [catalog_visibility] => visible

? ? [description] => Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet…


[add_to_cart id="53"]

? ? [short_description] => Pellentesque habitant morbi tristique senectus...

? ? [sku] => Gh2563

? ? [price] => 90.50

? ? [regular_price] => 100

? ? [sale_price] => 90.50


// … and so on …


查看完整回答
反對 回復 2023-08-06
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

有兩種方法可以做到這一點,一種是在 foreach 循環中手動過濾元數據數組,另一種是對 wp_postmeta 表進行自定義查詢以獲取特定鍵。



查看完整回答
反對 回復 2023-08-06
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

以下是有關如何從每個產品獲取元數據的Woocommerce 代碼參考。

另外,這是如何通過下面的鍵獲取元數據的示例:

function woo_custom_additional_information_tab_content() {


global $product;


$product_data = $product->get_meta('_specifications');


echo $product_data;

}


查看完整回答
反對 回復 2023-08-06
  • 3 回答
  • 0 關注
  • 188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號