WooCommerce add_to_cart

The question: Is there a newer way to add product(s) to the cart? Well, WC_Cart::add_to_cart() is still the way to do it. Except (on the front-end), there’s no need to reinstantiate the cart class: $cart = new WC_Cart(); because the main WooCommerce class already instantiates WC_Cart, and you can easily access the class instance like … Read more

Filter default_content only for products

default_content is a filter used in the backend. You don’t necessarily have anything in the loop so standard functions will probably fail. However, you’re given a second argument of type WP_Post. You can check its post_type easily and work from there. add_filter(‘default_content’, ‘WPSE_product_default_content’, 10, 2); function WPSE_product_default_content($post_content, $post) { if ($post->post_type !== ‘product’) return $post_content; … Read more

Woocommerce Canceled Order Notification email to admin

Was the order moved from Pending to Cancelled? WooCommerce no longer sends an email to the admin for that (I think the logic is that a lot of bot-created Pending orders were automatically cancelled and giving admins too many notifications). You can restore the old behavior by hooking woocommerce_order_status_pending_to_cancelled_notification and calling WC()->mailer()->get_emails()[‘WC_Email_Cancelled_Order’]->trigger with the order … Read more