如何更改这个简单的代码,以便在WooCommerce中付款后发送电子邮件通知?

时间:2019-10-13 作者:Vahid Damanafshan

如何更改此代码以发送电子邮件通知after successfully payment 在WooCommerce?

add_action( \'woocommerce_applied_coupon\', \'custom_email_on_applied_coupon\', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
    if( $coupon_code == \'mycoupon\' ){

        $to = "[email protected]";
        $subject = "Coupon $coupon_code has been applied";
        $content = "
        The coupon code $coupon_code has been applied by a customer
        ";

        wp_mail( $to, $subject, $content );
    }
}

2 个回复
最合适的回答,由SO网友:alexandrucarjan 整理而成

您正在寻找的挂钩是“woocommerce payment complete”,一旦订单标记为“complete”,就会触发该挂钩。在该钩子调用的函数中,您需要获取应用于订单的所有优惠券,并检查其中是否有您正在寻找的优惠券。

add_action( \'woocommerce_payment_complete\', \'so_payment_complete\' );
function so_payment_complete( $order_id ){

        // Get an instance of WC_Order object
        $order = wc_get_order( $order_id );

        // Coupons used in the order LOOP (as they can be multiple)
        foreach( $order->get_used_coupons() as $coupon_code ){

            // Retrieving the coupon ID
            $coupon_post_obj = get_page_by_title($coupon_code, OBJECT, \'shop_coupon\');
            $coupon_id       = $coupon_post_obj->ID;

            // Get an instance of WC_Coupon object in an array(necessary to use WC_Coupon methods)
            $coupon = new WC_Coupon($coupon_id);
            $coupon_code = $coupon->get_code();

            // Check if it\'s the right coupon
            if ( $coupon_code == \'mycoupon\' ){

                $to = "[email protected]";
                $subject = "Coupon $coupon_code has been applied";
                $content = "
                The coupon code $coupon_code has been applied by a customer
                ";

                wp_mail( $to, $subject, $content );

            }

        }
}

SO网友:alexandrucarjan

这是因为您使用了双逗号而没有转义它们。替换此:

    $content = "
    The coupon code $coupon_code has been applied by a customer
    ";
使用此选项:

    $content = "
    The coupon code $coupon_code has been applied by a customer with 
    <p style="text-align: right; direction: rtl;">The coupon code 
    $coupon_code has been applied by a customer</p>
    ";
请注意,来自<;的双逗号<;style=“>>?这基本上关闭了用于定义$content变量的第一个双逗号<;$content=“>>”。基本上,php看到的$内容变量如下所示:

    $content ="
    The coupon code $coupon_code has been applied by a customer with 
    <p style="
这意味着php将尝试解释这一点:<<;文本对齐:右对齐;方向:rtl;>>作为php代码,它将返回一个致命错误,因为根据php,这是胡言乱语(而且,它不认为$content变量行以“;”结尾,这可能意味着它将返回类似“意外的‘t’在线…”“。

您必须通过在转义字符前面加“\\”来转义变量中的所有双逗号,如下所示:

    $content = "
    The coupon code $coupon_code has been applied by a customer with 
    <p style=\\"text-align: right; direction: rtl;\\">The coupon code 
    $coupon_code has been applied by a customer</p>
    ";
或选择单逗号:

    $content = "
    The coupon code $coupon_code has been applied by a customer with 
    <p style=\'text-align: right; direction: rtl;\'>The coupon code 
    $coupon_code has been applied by a customer</p>
    ";
我建议您开始更加熟悉php,这是一种非常简单的编程语言,您会发现许多教程和文档。您可能还想阅读此线程,以便更好地理解php中单逗号和双逗号之间的区别:https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php

相关推荐

如何将外部变量传递给wp_new_User_Notify_Email过滤器?

我们正在尝试自定义新用户在我们的网站上注册时收到的默认电子邮件。为此,我们使用WP4.9.0中添加的wp\\u new\\u user\\u notification\\u email筛选器对于我们的功能。php文件不要凌乱,我们在“templates/email\\u welcome.php”中有电子邮件模板。我们需要加载此文件来构建$message变量,但返回的内容始终为空。下面是我们在子主题函数中添加的内容。php// ========================================