ARTICLE AD BOX
I have two options for checkout an order in the cart on my website:
1. The standard WooCommerce checkout.
2. Ordering through a manager using the Contact Form 7 order form.
I'm using code that creates a tag for the Contact Form 7 order form and adds products from the cart to it. To do this need to place the [cart_content] tag in the order form.
/* Get a cart details as a text */ function get_cart_content_as_text() { $cart_contents = WC()->cart->get_cart(); $cart_text = ''; $cart_total = 0; foreach ($cart_contents as $cart_item_key => $cart_item) { $product = $cart_item['data']; $quantity = $cart_item['quantity']; $sum = $cart_item['line_total']; $cart_total += $sum; $product_name = $product->get_name(); $cart_text .= "Product: $product_name x $quantity \n"; } $cart_text .= "Subtotal: $sum \n"; $cart_text .= "Total: $cart_total"; return $cart_text; } /* create a new tag for CF7 */ function cf7_add_cart_content_tag() { wpcf7_add_form_tag('cart_content', 'cf7_cart_content_handler'); } function cf7_cart_content_handler($tag) { $cart_content_text = get_cart_content_as_text(); return '<textarea name="cart_content" readonly>' . esc_textarea($cart_content_text) . '</textarea>'; } add_action('wpcf7_init', 'cf7_add_cart_content_tag'); <label> Name: [text* your-name autocomplete:name] </label> <label> E-mail: [email* your-email autocomplete:email] </label> <label> Products: [cart_content]</label> [submit "Submit"]Unfortunately, after filling out the form, the message submission freezes and the email doesn't arrive.
There was also an error:
Fatal error: Uncaught Error: Call to a member function get_cart() on null in $cart_contents = WC()->cart->get_cart();
How can I fix the code so everything works correctly?
