Don`t send email to customer after woocomerce order status changing - woocommerce

Please, help to solve the problem. I want to make WooComerce send email only once, about new order - both admin and customer. If I will change order status - NO email will be send.
I've tried to disable notifications at woocomerce-settings-emails and disable all notifications for clients, but in this case, clients don`t receive any email even for the first time.

Try this in funtion.php
add_action( 'woocommerce_email', 'unhook_those_pesky_emails' );
function unhook_those_pesky_emails( $email_class ) {
/**
* Hooks for sending emails during store events
**/
remove_action( 'woocommerce_low_stock_notification', array( $email_class, 'low_stock' ) );
remove_action( 'woocommerce_no_stock_notification', array( $email_class, 'no_stock' ) );
remove_action( 'woocommerce_product_on_backorder_notification', array( $email_class, 'backorder' ) );
// New order emails
remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_pending_to_completed_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_failed_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_failed_to_completed_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
// Processing order emails
remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
// Completed order emails
remove_action( 'woocommerce_order_status_completed_notification', array( $email_class->emails['WC_Email_Customer_Completed_Order'], 'trigger' ) );
// Note emails
remove_action( 'woocommerce_new_customer_note_notification', array( $email_class->emails['WC_Email_Customer_Note'], 'trigger' ) );
}

Related

Set user registration date while creating account during Woocommerce order

When user completes the order, I want to save the user selected value as user registration date. I know it can be achived with this code:
wp_update_user(
[
'ID' => $user_id,
'user_registered' => $user->user_registered,
]
);
But how I can make it work with the rest of my code? How I can save this data as the registration date? I know how to save the order meta etc. but I've never did something like this.
add_action( 'woocommerce_before_checkout_registration_form', 'custom_checkout_fields_before_billing_details', 20 );
function custom_checkout_fields_before_billing_details(){
$domain = 'woocommerce';
$checkout = WC()->checkout;
echo '<div id="custom_checkout_field">';
woocommerce_form_field( '_custom_field_name', array(
'type' => 'text',
'label' => __('SELECT DATE', $domain ),
'placeholder' => __('DATE"', $domain ),
'class' => array('custom-field-class form-row-wide'),
'required' => false, // or false
), $checkout->get_value( '_custom_field_name' ) );
echo '</div>';
echo '<script>jQuery(document).ready(function( $ ) {$( "#_custom_field_name").datepicker();});</script>';
}
// Save custom checkout fields the data to the order
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
function custom_checkout_field_update_meta( $order, $data ){
if( isset($_POST['_custom_field_name']) && ! empty($_POST['_custom_field_name']) )
$order->update_meta_data( '_custom_field_name', sanitize_text_field( $_POST['_custom_field_name'] ) );
}
add_action( 'wp_enqueue_scripts', 'enqueue_datepicker' );
function enqueue_datepicker() {
if ( is_checkout() ) {
// Load the datepicker script (pre-registered in WordPress).
wp_enqueue_script( 'jquery-ui-datepicker' );
// You need styling for the date picker. For simplicity, I've linked to Google's hosted jQuery UI CSS.
wp_register_style( 'jquery-ui', '//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' );
wp_enqueue_style( 'jquery-ui' );
}
}

Moving Express Checkout buttons on woocommerce (not Stripe)

I can find instructions on how to Move Stripe Payment Request Button, but not a single one to the new Woocommerce payments request button, which is a different instance :(
Any help is appreciated!
/** Move Stripe Payment Request Button on product page **/
remove_action( 'woocommerce_after_add_to_cart_quantity', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_html' ), 1 );
remove_action( 'woocommerce_after_add_to_cart_quantity', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_separator_html' ), 2 );
add_action( 'woocommerce_after_add_to_cart_button', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_html' ), 2 );
add_action( 'woocommerce_after_add_to_cart_button', array( WC_Stripe_Payment_Request::instance(), 'display_payment_request_button_separator_html' ), 1 );

Updating custom product meta box field - WooCommerce

I'm trying to update a custom product meta box field using the updating system from WooCommerce core. Here is my code:
The new field Shipping info
add_action( 'woocommerce_product_options_shipping', 'my_product_options_shipping' );
function my_product_options_shipping() {
global $post;
$shipping_info = get_post_meta( $post->ID, '_shipping_info', true );
woocommerce_wp_text_input(
array(
'id' => '_shipping_info',
'value' => $shipping_info,
'label' => __( 'Shipping info', 'woocommerce' ),
'placeholder' => __( 'Shipping in two days', 'woocommerce' ),
)
);
}
And this is the function that adds the new field as prop in WC_Meta_Box_Product_Data::save
add_action( 'woocommerce_admin_process_product_object', 'my_admin_process_product_object' );
function my_admin_process_product_object( $product ) {
$product->set_props(
array(
'shipping_info' => isset( $_POST['_shipping_info'] ) ? wc_clean( wp_unslash( $_POST['_shipping_info'] ) ) : null,
)
);
}
I think I'm missing a step. Shouldn't it be saved automatically from function WC_Meta_Box_Product_Data::save which is attached to woocommerce_process_product_meta?
EDIT
I found the missing step. I need to add my custom post meta in the protected array $extra_data from abstract class WC_Data.
I'm not too good at OOP, so how I can access that array to push my custom data?
I can see you're just doing $shipping_info = get_post_meta( $post->ID, '_shipping_info', true );
So why not save the trouble and just use update_post_meta
add_action( 'woocommerce_admin_process_product_object', 'my_admin_process_product_object' );
function my_admin_process_product_object( $product ) {
update_post_meta($product->get_id(), '_shipping_info', wc_clean( wp_unslash( $_POST['_shipping_info'] ) ) );
}

Woocommerce: redirect (keep) user in cart page after log-in in cart page

By default, Woocommerce displays the user login box in the checkout page; I wanted it to appear on the cart page as well, so I have added the following to the functions.php file in my theme's directory:
// display login form in cart page
add_action( 'woocommerce_after_cart', 'woocommerce_login_form' );
This works, but after the user log-in, he is redirected to his account page; I would like him to be kept at the cart page, which I think can be done by passing a redirect argument, as per the function's architecture:
function woocommerce_login_form( $args = array() ) {
$defaults = array(
'message' => '',
'redirect' => '',
'hidden' => false
);
$args = wp_parse_args( $args, $defaults );
wc_get_template( 'global/form-login.php', $args );
}
How can I pass an url as an argument to this function?
Thank you for your help.
I have managed to do it be replicating the function with some changes:
// display login form in cart page
add_action( 'woocommerce_after_cart', 'woocommerce_login_form_in_cart' );
function woocommerce_login_form_in_cart( $args = array() ) {
$defaults = array(
'message' => '',
'redirect' => '#',
'hidden' => false
);
$args = wp_parse_args( $args, $defaults );
wc_get_template( 'global/form-login.php', $args );
}

Wordpress restrict categories plugin with DWQA plugin

My environment:
using DWQA plugin and customized restrict categories plugin to work with DWQA plugin
Problem:
I have customized "restrict categories" plugin to work for DWQA instead of WP posts.
Now the problem is when I logged in as user with privileges set to category2 from restrict categories, it's not able to show anything for me.
To set category filter restrict categories is using the following code:
add_filter( 'pre_get_posts', array( &$this, 'posts_query' ) );
public function posts_query( $query ){
if ( $this->cat_list !== '' ) {
// Build an array for the categories
$cat_list_array = explode( ',', $this->cat_list );
// Make sure the posts are removed by default or if filter category is ran
if ( ! isset( $_REQUEST['cat'] ) )
$query->set( 'category__in', $cat_list_array );
elseif( isset( $_REQUEST['cat'] ) && $_REQUEST['cat'] == '0' )
$query->set( 'category__in', $cat_list_array );
}
return $query;
}
If I use category__not_in in place of category__in then all the posts are showing.
I have resolved this by changing code as below
public function posts_query( $query ){
// Build an array for the categories
$cat_list_array = explode( ',', $this->cat_list );
$taxquery = array(
array(
'taxonomy' => 'dwqa-question_category',
'field' => 'id',
'terms' => $cat_list_array,
'operator'=> 'NOT IN'
)
);
$query->set( 'tax_query', $taxquery );

Resources