I am trying to retrieve field values submitted from contact form 7 in wordpress and then do some calculations. After that i am checking if the product is already in cart and if not it is being added via code. This what i aim to approach but yet i can not figure out what is wrong in my code. Need help please.

function action_wpcf7_before_send_mail( $contact_form ) {

        $submission = WPCF7_Submission::get_instance();

        if ( $submission ) {
        $posted_data = $submission->get_posted_data();
        }

        global $tour_id;
        global $final_price;

        $cm_mail = $posted_data['cm-email']; 
        $tour_id = $posted_data['tour-id'];
        $tour_price = $posted_data['tour-price'];
        $tour_discount_price = $posted_data['tour-discount-price'];
        if (empty($tour_discount_price)) {
            $tour_final_price = $tour_price;
        } else {
            $tour_final_price = $tour_discount_price;
        }
        $tour_date = $posted_data['tour-date'];
        $adult_no = $posted_data['no-adult'];
        $child_no = $posted_data['no-child'];

        $total_pax = array($adult_no,$child_no);

        if (sum($total_pax) >= 1)
        { $final_price = $tour_final_price * 1; }
        if (sum($total_pax) >= 3)
        { $final_price = $tour_final_price * 0.8; }
        else if (sum($total_pax) >= 7)
        { $final_price = $tour_final_price * 0.6; }
        else if (sum($total_pax) >= 16)
        { $final_price = $tour_final_price * 0.4; }
        else
        { $final_price = 0; }


        global $woocommerce;
        if( 0 < matched_cart_items($tour_id) ){
        return;
        } else {
        $woocommerce->cart->add_to_cart($tour_id);
        }

}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail');
/*==============================================================================================*/

/*==============================================================================================*/
function matched_cart_items($product_id) {

    if(!WC()->cart->is_empty()):

        // Initialise the count
        $count = 0;

        foreach(WC()->cart->get_cart() as $cart_item ):

            $items_id = $cart_item['product_id'];

            // For an array of product IDS
            if(is_array($product_id) && in_array($items_id, $product_id))
                $count++; // incrementing the counted items

            // for a unique product ID (integer or string value)
            if($product_id == $items_id)
                $count++; // incrementing the counted items

        endforeach;

        // returning counted items 
        return $count;

    endif;
}

Read more here: Integrate Contact Form 7 with Woocommerce


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: