• Resolved maliktanveer

    (@maliktanveer)


    The plugin is excellent, but I have a question about the meta key for the Fundraising Goal field. We are actually interested in an automatic category change when the fundraising goal amount is reached. For instance, if we set the goal amount to 100, we would like the category to change automatically once that amount is achieved. I attempted to use the ‘wcdp-settingswcdp_fundraising_goal’ meta key, but I encountered issues with array data. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jonas

    (@flinnn)

    Hi maliktanveer,

    thanks for reaching out. I think the easiest way is to use the filter wcdp_update_product_revenue:

    function update_category_based_on_revenue( $revenue, $productid ) {
        if ( $revenue > 5000 && $productid === 123 ) {
            $product = wc_get_product( $productid );
    
            $new_category_id = 42; // Replace with the desired category ID
            $product->set_category_ids( array( $new_category_id ) );
            $product->save();
        }
    
        return $revenue;
    }
    
    add_filter( 'wcdp_update_product_revenue', 'update_category_based_on_revenue', 10, 2 );

    Hope this helps.

    Best
    Jonas

    PS: If you like Donation Platform for WooCommerce and want to support the further growth and development of the plugin, please consider a 5-star rating on www.ads-software.com.

    Thread Starter maliktanveer

    (@maliktanveer)

    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)

    Thread Starter maliktanveer

    (@maliktanveer)

    but we are facing another problem with progress bar on front end. sometimes after ordering it works sometimes did not. can you please check the bug? amount did not update on front end after order and if make another order then amount update with previous as well?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘the meta key for the Fundraising Goal field’ is closed to new replies.