Search WooCommerce price from an array of arrays. In this example we have multiple arrays of data. We search for the array that contain a certain value. Then, we obtain the price from that array.
Add this code snippet to your themes functions.php
file.
NOTICE: Requires WP Data Sync plugin version 2.1.1 or higher.
Sample Data
$post_meta = [
'_finish' => 'Foil',
'_api_prices' => [
[
'subTypeName' => 'Normal',
'highPrice' => 199.99,
'lowPrice' => 129.99
],
[
'subTypeName' => 'Foil',
'highPrice' => 299.99,
'lowPrice' => 229.99
]
]
];
How does the code work?
The code works by searching the arrays for the subTypeName
that equals the value of the _finish
field. Then, set the WooCommerce _regular_price
with the value of the highPrice
field from that array.
We run this on the wp_data_sync_set_property_post_meta filter hook so the _regular_price
field will be subject to all other relevant filters.
Code Example
/**
* DataSync set property post meta filter.
*
* @param array $post_meta
*
* @return array
*/
add_filter( 'wp_data_sync_set_property_post_meta', function( $post_meta ) {
if ( ! empty( $post_meta['_api_prices'] ) && ! empty( $post_meta['_finish'] ) ) {
$key = array_search( $post_meta['_finish'], array_column( $post_meta['_api_prices'], 'subTypeName' ) );
$post_meta['_regular_price'] = $post_meta['_api_prices'][ $key ]['highPrice'];
unset( $post_meta['_api_prices'] );
}
return $post_meta;
}, 20, 1 );
Get Started
-
Starter Plan
$39.00 / monthMore Info- 10,000 Requests/Month
- Unlimited Data Sources
- Unlimited Websites
- Unlimited Users
Basic Plan
$69.00 / monthMore Info- 40,000 Requests/Month
- Unlimited Data Sources
- Unlimited Websites
- Unlimited Users
Professional Plan
$129.00 / monthMore Info- 150,000 Requests/Month
- Unlimited Data Sources
- Unlimited Websites
- Unlimited Users
Data Source Mapping
$149.00 – $999.00More Info This product has multiple variants. The options may be chosen on the product pageLet us do the mapping for you!!We will map all your data sources for you, so you can move on to other projects that demand your time. One of our experts will map up to 50 fields for you in each data source.