如何在 WooCommerce 中获取产品特定元数据

kod*_*ire 4 php wordpress product metadata woocommerce

我正在使用 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];
}
Run Code Online (Sandbox Code Playgroud)

上面代码的结果是:

[attributes] => Array
(
    [_edit_last] => 1
    [_edit_lock] => 1594817821:1
    .
    .
    .
)
Run Code Online (Sandbox Code Playgroud)

但我想要:

[attributes] => Array
(
    [price] => 1000
    [color] => 'red'
    .
    .
    .
)
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 10

有多种方法,具体取决于您想要的:

\n

1)。您将需要使用WC_Product 对象上的WC_Product方法来获取产品属性:

\n
$product_id = 37; // Defined product Id for testing\n\n// Get the WC_Product Object from the product ID (optional)\n$product = wc_get_product($product_id); // If needed\n\n// Get product active price\n$product_data = $product->get_price();\n\n// \xe2\x80\xa6\xc2\xa0and so on \xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n

2)。对于特定或自定义产品元数据,您将使用使用所需元键的WC_Data方法,例如:get_meta()

\n
$product_id = 37; // Defined product Id for testing\n\n// Get the WC_Product Object from the product ID (optional)\n$product = wc_get_product($product_id); // If needed\n\n// Get custom meta data froma specific meta key\n$product_data = $product->get_meta(\'my_meta_key\');\n\n// \xe2\x80\xa6\xc2\xa0and so on \xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n

3)。您可以使用对象上的WC_Data方法get_data()WC_Product来获取数组中不受保护的元数据:

\n
$product_id = 37; // Defined product Id for testing\n\n// Get the WC_Product Object from the product ID (optional)\n$product = wc_get_product($product_id); // If needed\n\n// Get product unprotected meta data in an array\n$product_data = $product->get_data();\n\n// Output row data\necho \'<pre>\'. print_r( $obj, true ) . \'</pre>\';\n
Run Code Online (Sandbox Code Playgroud)\n

你会得到类似的东西(摘录):

\n
Array\n(\n    [id] => 37\n    [name] => Happy Ninja Dish Hip\n    [slug] => happy-ninja-dish\n    [date_created] => WC_DateTime Object\n        (\n            [utc_offset:protected] => 0\n            [date] => 2015-12-26 11:53:15.000000\n            [timezone_type] => 3\n            [timezone] => Europe/Paris\n        )\n\n    [date_modified] => WC_DateTime Object\n        (\n            [utc_offset:protected] => 0\n            [date] => 2020-07-09 19:25:32.000000\n            [timezone_type] => 3\n            [timezone] => Europe/Paris\n        )\n\n    [status] => publish\n    [featured] => \n    [catalog_visibility] => visible\n    [description] => Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet\xe2\x80\xa6\n\n[add_to_cart id="53"]\n    [short_description] => Pellentesque habitant morbi tristique senectus...\n    [sku] => Gh2563\n    [price] => 90.50\n    [regular_price] => 100\n    [sale_price] => 90.50\n\n// \xe2\x80\xa6 and so on \xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n