I have a code in a custom plugin which gives the warning:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in public_html/wp-includes/class-wp-hook.php on line 289
The code is as follows:
<?php
// Backend: Additional pricing option custom field
add_action('woocommerce_product_options_pricing', 'wc_cost_product_field');
function wc_cost_product_field()
{
woocommerce_wp_text_input(array('id' => '_sale_perc_price', 'class' => 'wc_input_price short', 'label' => __('Sconto (%)', 'woocommerce'), 'desc_tip' => 'true', 'description' => 'Se non vuota, il prezzo in offerta viene calcolato al salvataggio. Se la percentuale di sonto è vuota e il prezzo in offerta è valorizzato, viene calcolato lo sconto in percentuale al salvataggio. Se sia il prezzo in offerta sia lo sconto in percentuale sono vuoto rimarranno vuoti entrambi'));
}
// Backend: Saving product pricing option custom field value
add_action('woocommerce_admin_process_product_object', 'save_product_custom_meta_data', 100, 1);
function save_product_custom_meta_data($product)
{
$sale_perc = $_POST('_sale_perc_price');
if (isset($sale_perc)) {
$sale_perc = absint(sanitize_text_field($_POST('_sale_perc_price')));
}
$price = $product->get_regular_price();
if (!empty($sale_perc)) {
$product->update_meta_data('_sale_perc_price', $sale_perc);
$sale_price = $price - ($price * $sale_perc) / 100;
$sale_price = round($sale_price * 100) / 100;
$product->set_sale_price($sale_price);
} else {
$sale_price = $product->get_sale_price();
if (!empty($sale_price)) {
$sale_perc = ($price - $sale_price) * 100 / $price;
$sale_perc = round($sale_perc * 100) / 100;
$product->update_meta_data('_sale_perc_price', $sale_perc);
} else {
$product->update_meta_data('_sale_perc_price', null);
}
}
}
add_filter('woobe_before_update_product_field', 'woobe_before_update_product_field_func', 10, 3);
function woobe_before_update_product_field_func($value, $product_id, $field_key)
{
if ($field_key === '_sale_perc_price') {
$price = get_post_meta($product_id, '_regular_price', true);
$sale_price = $price - ($price * $value) / 100;
$sale_price = round($sale_price * 100) / 100;
update_post_meta($product_id, '_sale_price', $sale_price);
}
if ($field_key === 'sale_price') {
$price = get_post_meta($product_id, '_regular_price', true);
if (!empty($value)) {
$sale_perc = ($price - $value) * 100 / $price;
$sale_perc = round($sale_perc * 100) / 100;
update_post_meta($product_id, '_sale_perc_price', $sale_perc);
} else {
update_post_meta($product_id, '_sale_perc_price', 0);
}
}
if ($field_key === 'regular_price') {
$price = $value;
$sale_perc = get_post_meta($product_id, '_sale_perc_price', true);
$sale_price = $price - ($price * $sale_perc) / 100;
$sale_price = round($sale_price * 100) / 100;
update_post_meta($product_id, '_sale_price', $sale_price);
}
return $value;
}
function add_custom_field_in_bulk_edit_quick_edit()
{
echo '<div class="inline-edit-group">';
woocommerce_wp_text_input(array('id' => '_sale_perc_price', 'class' => 'input-text-wrap', 'label' => 'Sconto (%)'));
echo '</div>';
}
add_action('woocommerce_product_quick_edit_end', 'add_custom_field_in_bulk_edit_quick_edit', 99);
add_action('woocommerce_product_bulk_edit_end', 'add_custom_field_in_bulk_edit_quick_edit', 99);
function save_custom_field_in_bulk_edit_quick_edit($post_id, $post)
{
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if ('product' !== $post->post_type) {
return $post_id;
}
$sale_perc = $_POST('_sale_perc_price');
$price = $_POST('_regular_price');
$sale_price = $_POST('_sale_price');
if (!empty($sale_perc)) {
$sale_perc = absint(sanitize_text_field($_POST('_sale_perc_price')));
update_post_meta($post_id, '_sale_perc_price', $sale_perc);
$sale_price = $price - ($price * $sale_perc) / 100;
$sale_price = round($sale_price * 100) / 100;
update_post_meta($post_id, '_sale_price', $sale_price);
} else if (!empty($sale_price)) {
$sale_perc = ($price - $sale_price) * 100 / $price;
$sale_perc = round($sale_perc * 100) / 100;
update_post_meta($post_id, '_sale_perc_price', $sale_perc);
}
$_POST('_sale_perc_price') = '';
}
add_action('woocommerce_product_bulk_and_quick_edit', 'save_custom_field_in_bulk_edit_quick_edit', 99, 2);
I think something’s wrong with the call back functions in the hooks, but cannot figure it out. Any suggestions?