Woocommerce - 自动更改订单状态并将电子邮件发送到选定的地址

问题描述 投票:0回答:1

我正在定制 woocommerce 以创建一个食品预订系统。我已成功删除所有不必要的结账字段,并禁用了支付网关,因为我只需要注册订单。我现在使用 elementor 和我正在编写的自定义插件向结帐页面添加一些自定义字段。我现在有这个代码,我需要自定义结帐,当客户进行预订(订单)时,我需要向一些自定义电子邮件地址发送电子邮件,这些地址可以使用自定义结帐字段从用户中选择已添加。是否可以自动将订单状态更改为已完成并发送电子邮件,以及使用什么钩子?

class CustomCheckout {        
    
    #
    public function __construct()
    {
        # 
        add_action( 'woocommerce_checkout_fields', array( $this, 'remove_default_checkout_fields') );
        # 
        add_action( 'woocommerce_after_checkout_billing_form', array( $this, 'add_custom_checkout_fields') );
        # 
        add_action( 'phpmailer_init', array( $this, 'mailtrap') );
        # 
        add_filter( 'woocommerce_cart_needs_payment', '__return_false' );
    }

    # debug email woocommerce
    public function mailtrap( $phpmailer ) 
    {
        $phpmailer->isSMTP();
        $phpmailer->Host = 'sandbox.smtp.mailtrap.io';
        $phpmailer->SMTPAuth = true;
        $phpmailer->Port = 2525;
        $phpmailer->Username = '****';
        $phpmailer->Password = '***';
    }
        
    #
    public function remove_default_checkout_fields( $fields )
    {
        //unset( $fields['billing']['first_name'] );
        //unset( $fields['billing']['last_name'] );
        unset( $fields['billing']['billing_company'] );
        unset( $fields['billing']['billing_country'] );
        unset( $fields['billing']['billing_address_1'] );
        unset( $fields['billing']['billing_address_2'] );
        unset( $fields['billing']['billing_city'] );
        unset( $fields['billing']['billing_state'] );
        unset( $fields['billing']['billing_postcode'] );
        # Rimuovo campi spedizione
        unset( $fields['shipping']['shipping_first_name'] );
        unset( $fields['shipping']['shipping_last_name'] );
        unset( $fields['shipping']['shipping_company'] );
        unset( $fields['shipping']['shipping_country'] );
        unset( $fields['shipping']['shipping_address_1'] );
        unset( $fields['shipping']['shipping_addredd_2'] );
        unset( $fields['shipping']['shipping_city'] );
        unset( $fields['shipping']['shipping_state'] );
        unset( $fields['shipping']['shipping_postcode'] );

        #
        return $fields;
    }
    
    #
    public function add_custom_checkout_fields( $checkout )
    {
        # 
        woocommerce_form_field( 
            'pdvselector', 
            array(
                'type' => 'select',
                'required' => true,
                'class' => array('form-row-first'),
                'label' => 'Punto di ritiro',
                //'label_class'
                'options' => array(
                    '' => 'Select a store',
                    '[email protected]' => 'Value 1',
                    '[email protected]' => 'Value 2',
                    '[email protected]' => 'Value 3'
                )
            ),
            $checkout->get_value( 'pdvselector' )
        );

        #
        woocommerce_form_field( 
            'dateselector', 
            array(
                'type' => 'date',
                'required' => true,
                'class' => array('form-row-last'),
                'label' => 'Reservation day'
            ), 
            $checkout->get_value( 'dateselector' ) 
        );
    
        #
        woocommerce_form_field(
            'hourselector',
            array(
                'type' => 'time',
                'required' => true,
                'class' => array('form-row-last'),
                'label' => 'Reservation hour'
            ),
            $checkout->get_value( 'hourselector' )
        );
    }
  
    #
    public function send_order_status_change_email( $order_id )
    {
        #
        $order = wc_get_order( $order_id );
        //$order->update_status( '' );
        #
    }
}

$checkout = new CustomCheckout();

php wordpress woocommerce hook-woocommerce
1个回答
0
投票

1. 挂钩

woocommerce_thankyou
动作:

此操作在成功下订单时触发,非常适合处理状态更改和电子邮件通知等订单后任务。

2.修改你的

send_order_status_change_email
方法:

public function send_order_status_change_email( $order_id ) {
    $order = wc_get_order( $order_id );

    // Get the selected email address from the custom field
    $selected_email = $order->get_meta( 'pdvselector' );

    // Change the order status to completed
    $order->update_status( 'completed' );

    // Send the email notification to the selected address
    WC()->mailer()->send( 'customer_completed_order', $order_id, $selected_email );
}

3.

CustomCheckout
构造函数中添加操作钩子:

public function __construct() {

    //add this line
    add_action( 'woocommerce_thankyou', array( $this, 'send_order_status_change_email' ), 10, 1 );
}

当客户下订单时,将触发 send_order_status_change_email 方法。它将从 pdvselector 自定义字段检索选定的电子邮件地址。订单状态将更新为“已完成”。 “customer_completed_order”电子邮件模板将发送到所选的电子邮件地址,通知他们预订。

© www.soinside.com 2019 - 2024. All rights reserved.