最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to change this simple code so that it sends the email notification after payment in WooCommerce?

programmeradmin1浏览0评论

How to change this code so that it sends the email notification after successfully payment in 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 );
    }
}

How to change this code so that it sends the email notification after successfully payment in 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 );
    }
}
Share Improve this question asked Oct 13, 2019 at 12:43 Vahid DamanafshanVahid Damanafshan 1133 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The hook you're looking for is "woocommerce payment complete" and it's triggered once an order has been marked as "complete". In the function called by that hook, you need to grab all coupons applied to the order, and check if any of them is the one you're looking for.

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 );

            }

        }
}

It's because you use double commas without escaping them. You replace this:

    $content = "
    The coupon code $coupon_code has been applied by a customer
    ";

with this:

    $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>
    ";

Notice that double comma from << style=" >> ? That basically closes the first double comma that you used to define the $content variable << $content = " >>. Basically, php sees the $content variable like this:

    $content ="
    The coupon code $coupon_code has been applied by a customer with 
    <p style="

Which means that php will try to interpret this: << text-align: right; direction: rtl; >> as being php code, which will return a fatal error because that's gibberish according to php ( and also, it doesn't see the $content variable line as being ended by ";", which probably means it will return something like "Unexpected 't' on line..."".

You will have to either escape all double commas inside your variable by preceding the escaped character by "\" like so:

    $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>
    ";

or choose single commas:

    $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>
    ";

I suggest you start getting more familiar with php, which is a pretty easy programming language and you will find a lot of tutorials and documentation. You might also want to read this thread to get a better understanding of the difference between single commas and double commas in php: https://stackoverflow/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论