dsgft1486 2017-02-04 19:01
浏览 103
已采纳

触发订阅付款的woocommerce订单电子邮件钩子

I'd like to hook into the subscription plugin when a new order has been placed which I have done successfully using this action: woocommerce_checkout_subscription_created. Inside the function for that action I want to modify the email that goes out to the customer which I have tried to do like so:

<?php 
    function subscription_created($subscription){   

    add_action('woocommerce_email_before_order_table','my_offer',20);
    $order = $subscription->order;

    function my_offer($order){

        echo "<h2>Your Trial Offer</h2>";
        echo "<p>Your subscription to this product entitles you to a free blah blah blah...</p>";

    }

    return $var;

}
add_action('woocommerce_checkout_subscription_created','subscription_created'); 
?>

Like I said, the action for the created subscription fires (I logged the output for $subscription successfully). The email action isn't working.

I'm guessing this has something to do with scope, but I'm not sure. Any thoughts would be appreciated.

  • 写回答

1条回答 默认 最新

  • doulu4976 2017-02-07 18:38
    关注

    Running an action within an action isn't possible, I believe.

    If you check the code for this action, you have access to the following:

    do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email );
    

    Therefore, you could perhaps NOT hook into woocommerce_checkout_subscription_created and only use woocommerce_email_before_order_table.

    You could then query whether or not the $order is a subscription and then modify the output accordingly.

    add_action( 'woocommerce_email_before_order_table', function($order, $sent_to_admin, $plain_text, $email) {
        if ( function_exists( 'wcs_order_contains_subscription' ) ) {
            if ( wcs_order_contains_subscription( $order->ID ) ) {
                // Do what you need to do
            }
        } 
    }, 10, 4 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?