N
N
Nikolai2021-01-21 10:09:48
WordPress
Nikolai, 2021-01-21 10:09:48

How to submit Woocommerce cart content via contact form 7?

Hello. There is a site - a flower shop on WP + WC.
We make the "Collect a bouquet from a photo" functionality, set up the form, but it is necessary to be able to choose from a constantly updated catalog of additional items (postcards, toys, sweets, wrappers, etc.).

So far, there is only one option in my head - In the pop-up window, we show a grid of products from the desired category, and ask you to add the items you need to the basket. After that, we send the form + the contents of the basket to the manager, and we clear the basket itself.

I tried using the Contact Form 7 Dynamic Text Extension , but the result was not very good:
60092859ccfb2807411962.png

Maybe someone knows how to implement this more competently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai, 2021-01-23
@nipopadyuk

I don't claim to be original, but it works. We add the names of the goods that are in the basket to the end of the letter of a specific form.

add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_start_function' );
function wpcf7_before_send_mail_start_function($cf7){
 	$mail=$cf7->prop('mail');
  if($mail){
    $contact_form = $cf7->get_current();
    $contact_form_id = $contact_form -> id;
    if ($contact_form_id == 8261){
      if ( !is_admin()){ 
      $product_name = "";
      wc()->frontend_includes();
      WC()->session = new WC_Session_Handler();
      WC()->session->init();
      WC()->customer = new WC_Customer( get_current_user_id(), true );
      WC()->cart = new WC_Cart();
      foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
          $product_name = $product_name.apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key )."\n";
        }
        }
        $my = $product_name;
      }else{
        $my = "";
      } 

     $mail['body'].="\n\r".$my; // допишем в конец тела писма содержимое корзины
     
     
     WC()->cart->empty_cart();
         WC()->session->set('cart', array());
    }
   $cf7->set_properties(array('mail'=>$mail));
  } 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question