问题
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:
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 );
来源:https://stackoverflow.com/questions/42044353/trigger-woocommerce-order-email-hook-from-subscription-payment