Created
February 6, 2021 20:18
-
-
Save mrpsiho/ca00404b2db1a740a3a709e658bd6151 to your computer and use it in GitHub Desktop.
Pairing custom meta field with NOV in Uni CPO
This file contains 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
add_action( 'woocommerce_product_options_pricing', 'uni_display_fields' ); | |
add_action( 'woocommerce_process_product_meta', 'uni_save_fields' ); | |
function uni_display_fields() { | |
woocommerce_wp_text_input( | |
array( | |
'id' => 'uni_paired_meta', | |
'label' => 'Meta field paired with NOV', | |
'description' => 'This value will be used instead of {uni_nov_cpo_paired_meta}', | |
) | |
); | |
} | |
function uni_save_fields( $post_id ) { | |
$product = wc_get_product( $post_id ); | |
$product->update_meta_data( 'uni_paired_meta', sanitize_text_field( $_POST['uni_paired_meta'] ) ); | |
$product->save(); | |
} | |
add_filter('uni_cpo_nov_variable_value', 'uni_paired_nov_value', 10, 4); | |
function uni_paired_nov_value($value, $product, $variables, $slug) { | |
if ('paired_meta' === $slug && function_exists('uni_cpo_calculate_formula')) { | |
$val = get_post_meta($product['id'], 'uni_paired_meta', true); | |
return uni_cpo_calculate_formula($val); | |
} | |
return $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks