【问题标题】:Send a custom email when WooCommerce checkout button is pressed按下 WooCommerce 结帐按钮时发送自定义电子邮件
【发布时间】:2017-12-29 06:48:11
【问题描述】:

每当使用 PHP 为 Woocommerce 按下结帐按钮时,我都会尝试发送自定义电子邮件。

此电子邮件将与 wooCommerce 的电子邮件通知一起发送。 我使用了这个answer,并编辑了如下代码:

//execute some php on successfull checkout
add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );
function so_32512552_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item ) {

        if ( $item['product_id'] > 0 ) {
            $_product = $order->get_product_from_item( $item );

            // the message
            $msg = "First line of text\nSecond line of text";

            // use wordwrap() if lines are longer than 70 characters
            $msg = wordwrap($msg,70);

            // send email
            mail("info@example.com","My subject",$msg);


        }
    }
}

但似乎什么也没发生。有什么想法吗?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce checkout email-notifications


    【解决方案1】:

    这不起作用,因为此钩子仅在订单状态完成时触发 ...
    使用 wp_mail() 也比使用 mail() 函数更好。

    您可以尝试使用挂在 woocommerce_thankyou 动作挂钩中的自定义函数:

    add_action( 'woocommerce_thankyou', 'custom_email_notification', 10, 1 );
    function custom_email_notification( $order_id ) {
    
        if ( ! $order_id ) return;
    
        ## THE ORDER DATA ##
    
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );
    
        // Iterating through each order items
        foreach ( $order->get_items() as $item_id => $order_item ) {
    
            // Accessing to the protected data of the WC_Order_Item_Product object
            $order_item_data = $order_item->get_data();
    
            // Get the associated WC_Product object
            $product = $order_item->get_product();
    
            // Accessing to the WC_Product object protected data
            $product_data = $product->get_data();
        }
    
    
        ## SENDING AN EMAIL (outside the loop is better to send it once) ##
    
        $to = "test@mail.com";
        $subject = "the subject here";
        $content = "Here goes your message";
    
        // Sending your custom email notification
        wp_mail( $to, $subject, $content );
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码在 WooCommerce 3+ 上经过测试并且可以正常工作。

    woocommerce_thankyou钩子在订单接收页面被触发……

    【讨论】:

    • 非常感谢提供非常详细和有用的代码。我刚刚尝试用我的电子邮件地址替换 test@mail.com,但不幸的是我没有收到任何电子邮件。已到达感谢页面。
    • @xbass540 你好,这个代码已经过测试并且完全可以工作......所以你托管与你的 Wordpress 安装相关的外发电子邮件有问题......
    • 我想过,但目前默认的 woocommerce 电子邮件发送没有问题。我应该在我的服务器设置中寻找哪些不足的设置?
    • @xbass540 我不知道……你也应该看看垃圾邮件文件夹……
    猜你喜欢
    • 2013-08-06
    • 2019-09-25
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多