hi some how i make it possible with this code:
function add_calculated_amount_meta_field() {
global $post;
if ('product' === $post->post_type) {
echo '<div class="options_group">';
echo '<p class="form-field">';
echo '<label for="_calculated_amount">' . __('Calculated Amount', 'your-text-domain') . '</label>';
$product_id = $post->ID;
// Make sure WooCommerce is active before proceeding
if (class_exists('WooCommerce')) {
$total_amount = calculate_total_amount_for_product($product_id);
echo '<input type="text" name="_calculated_amount" id="_calculated_amount" value="' . esc_attr($total_amount) . '" readonly />';
} else {
echo '<input type="text" name="_calculated_amount" id="_calculated_amount" value="WooCommerce not active" readonly />';
}
echo '</p>';
echo '</div>';
}
}
add_action(‘woocommerce_product_options_general_product_data’, ‘add_calculated_amount_meta_field’);
function calculate_total_amount_for_product($product_id) {
$total_amount = 0;
// Make sure WooCommerce is active before proceeding
if (class_exists('WooCommerce')) {
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
);
$orders = get_posts($args);
foreach ($orders as $order) {
$order_id = $order->ID;
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product = $item->get_product();
if ($product && $product->get_id() === $product_id) {
$total_amount += $item->get_total();
}
}
}
$fundraising_goal = get_post_meta($product_id, 'wcdp-settings[wcdp_fundraising_goal]', true);
if ($total_amount >= $fundraising_goal) {
$taxonomy = 'projects-statue';
$terms = wp_get_post_terms($product_id, $taxonomy);
foreach ($terms as $term) {
if ($term->slug === 'non-complete') {
wp_remove_object_terms($product_id, 'non-complete', $taxonomy);
wp_set_object_terms($product_id, 'complete', $taxonomy, true);
break;
}
}
} else {
$taxonomy = 'projects-statue';
$terms = wp_get_post_terms($product_id, $taxonomy);
foreach ($terms as $term) {
if ($term->slug === 'complete') {
wp_remove_object_terms($product_id, 'complete', $taxonomy);
wp_set_object_terms($product_id, 'non-complete', $taxonomy, true);
break;
}
}
}
return $total_amount;
} else {
return 'WooCommerce not active';
}
}
// … (rest of your code remains unchanged)