Created
April 29, 2024 20:05
-
-
Save mattsherman/8b3e67068d247f9825a4b1ca5caf2602 to your computer and use it in GitHub Desktop.
Example how to handle "packed" meta values in WooCommerce's new product editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\GroupInterface; | |
add_filter( | |
'woocommerce_rest_prepare_product_object', | |
function ( $response, $post ) { | |
$custom_regular_prices_encoded = get_post_meta( $post->get_id(), '_custom_regular_prices', true ); | |
$custom_regular_prices = json_decode( | |
'' === $custom_regular_prices_encoded ? '{}' : $custom_regular_prices_encoded, | |
true, | |
); | |
$response->data['custom_regular_price_1'] = isset( $custom_regular_prices['price_1'] ) ? $custom_regular_prices['price_1'] : ''; | |
$response->data['custom_regular_price_2'] = isset( $custom_regular_prices['price_2'] ) ? $custom_regular_prices['price_2'] : ''; | |
return $response; | |
}, | |
10, | |
2, | |
); | |
add_filter( | |
'woocommerce_rest_insert_product_object', | |
function ( $product, $request ) { | |
$product_id = $product->get_id(); | |
$params = $request->get_params(); | |
$existing_custom_regular_prices_encoded = get_post_meta( $product_id, '_custom_regular_prices', true ); | |
$existing_custom_regular_prices = json_decode( | |
'' === $existing_custom_regular_prices_encoded ? '{}' : $existing_custom_regular_prices_encoded, | |
true, | |
); | |
$existing_price_1 = isset( $existing_custom_regular_prices['price_1'] ) ? $existing_custom_regular_prices['price_1'] : ''; | |
$existing_price_2 = isset( $existing_custom_regular_prices['price_2'] ) ? $existing_custom_regular_prices['price_2'] : ''; | |
$custom_regular_prices = [ | |
'price_1' => isset( $params['custom_regular_price_1'] ) ? $params['custom_regular_price_1'] : $existing_price_1, | |
'price_2' => isset( $params['custom_regular_price_2'] ) ? $params['custom_regular_price_2'] : $existing_price_2, | |
]; | |
update_post_meta( $product_id, '_custom_regular_prices', wp_json_encode( $custom_regular_prices ) ); | |
}, | |
10, | |
2, | |
); | |
add_action( | |
'rest_api_init', | |
function () { | |
add_action( | |
'woocommerce_block_template_area_product-form_after_add_block_pricing', | |
function ( GroupInterface $pricing_group ) { | |
$section = $pricing_group->add_section( | |
array( | |
'id' => 'example-complex-product-meta-fields-pricing-section', | |
'attributes' => array( | |
'title' => __( 'My Custom Pricing Fields', 'YOUR_TEXT_DOMAIN' ), | |
), | |
) | |
); | |
$section->add_block( | |
array( | |
'id' => 'example-complex-product-meta-fields-custom-regular-price-1', | |
'blockName' => 'woocommerce/product-pricing-field', | |
'attributes' => array( | |
'label' => __( 'Custom Regular Price 1', 'YOUR_TEXT_DOMAIN' ), | |
'property' => 'custom_regular_price_1', | |
), | |
) | |
); | |
$section->add_block( | |
array( | |
'id' => 'example-complex-product-meta-fields-custom-regular-price-2', | |
'blockName' => 'woocommerce/product-pricing-field', | |
'attributes' => array( | |
'label' => __( 'Custom Regular Price 2', 'YOUR_TEXT_DOMAIN' ), | |
'property' => 'custom_regular_price_2', | |
), | |
) | |
); | |
} | |
); | |
}, | |
9, | |
0 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment