You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中PHP全局变量跨页面传递及插件邮件触发问题

Integrate Your Custom $rushad Variable into WooCommerce Gift Card Plugin's Email Function

Hey there! Let’s get that custom text field value into your gift card plugin’s email. First, let’s address a key point upfront: relying solely on a global variable might not work long-term (since global variables only exist during the current HTTP request), so I’ll share both a quick global variable approach and a more reliable persistent method using order metadata.

Quick Fix: Using the Global Variable

If your plugin’s email function runs immediately during the same POST request where you set $rushad, you can directly access the global variable in the plugin’s email function:

Step 1: Declare the Global Variable in the Plugin’s Email Function

Modify your woocommerce_gift_card_send_email (or similar) function to pull in the global $rushad and inject it into the email content:

function woocommerce_gift_card_send_email($order_id, $gift_card_data) {
    // Existing email setup code
    $to = $gift_card_data['recipient_email'];
    $subject = __('Your Gift Card', 'your-plugin-text-domain');
    $message = __('Here’s your gift card code:', 'your-plugin-text-domain') . ' ' . $gift_card_data['code'];

    // Access the global $rushad variable
    global $rushad;
    // Only add it if it’s set and not empty to avoid errors
    if (isset($rushad) && !empty($rushad)) {
        $message .= "\n\n" . __('Personalized Note:', 'your-plugin-text-domain') . ' ' . esc_html($rushad);
    }

    // Send the email
    wp_mail($to, $subject, $message);
}

Important Note for This Approach

This only works if the email function fires during the same request where you set $rushad (right after the user submits the product form). If the plugin sends emails asynchronously (e.g., via a cron job or background queue), the global variable will no longer exist—so let’s look at the better, persistent solution.

Better Persistent Solution: Save to Order Metadata

Store the text field value as custom order metadata, so it’s permanently saved in the database and accessible whenever the email is sent:

Step 1: Update Your Product Page Code to Save to Order Meta

Replace your existing global variable code with this (it still sets the global variable but also saves the value to the order):

// Save the text field value to order metadata when the order is created
add_action('woocommerce_checkout_create_order', 'save_custom_rushad_to_order');
function save_custom_rushad_to_order($order) {
    if (isset($_POST['tmcp_textfield_0']) && !empty($_POST['tmcp_textfield_1'])) {
        // Sanitize the input for security
        $rushad_value = sanitize_text_field($_POST['tmcp_textfield_0']);
        // Save to order meta with a unique key
        $order->update_meta_data('_custom_rushad_message', $rushad_value);
        
        // Optional: Still set the global variable if you need it elsewhere in the same request
        global $rushad;
        $rushad = $rushad_value;
    }
}

Step 2: Fetch the Value from Order Meta in the Email Function

Modify the plugin’s email function to pull the saved value from the order instead of relying on the global variable:

function woocommerce_gift_card_send_email($order_id, $gift_card_data) {
    // Get the order object from the order ID
    $order = wc_get_order($order_id);
    // Retrieve the saved custom message
    $rushad_value = $order->get_meta('_custom_rushad_message');

    // Existing email setup code
    $to = $gift_card_data['recipient_email'];
    $subject = __('Your Gift Card', 'your-plugin-text-domain');
    $message = __('Here’s your gift card code:', 'your-plugin-text-domain') . ' ' . $gift_card_data['code'];

    // Add the custom message if it exists
    if (!empty($rushad_value)) {
        $message .= "\n\n" . __('Personalized Note:', 'your-plugin-text-domain') . ' ' . esc_html($rushad_value);
    }

    // Send the email
    wp_mail($to, $subject, $message);
}

Key Tips

  • Security: Always use sanitize_text_field() when saving user input and esc_html() when outputting it to prevent XSS attacks.
  • Text Domain: Replace your-plugin-text-domain with your actual plugin’s text domain (found in the plugin’s header) for proper translation support.
  • Troubleshooting: If the plugin’s email function doesn’t receive $order_id, check if $gift_card_data contains an order ID or use wc_get_order() with a different identifier (like the gift card’s associated order ID).

内容的提问来源于stack exchange,提问作者user3596056

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:51:39