3 回答

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 …

TA貢獻1871條經驗 獲得超13個贊
以下是有關如何從每個產品獲取元數據的Woocommerce 代碼參考。
另外,這是如何通過下面的鍵獲取元數據的示例:
function woo_custom_additional_information_tab_content() {
global $product;
$product_data = $product->get_meta('_specifications');
echo $product_data;
}
- 3 回答
- 0 關注
- 188 瀏覽
添加回答
舉報