Автоматически сгенерированный купон не отображается в электронном письме заказа - PullRequest
0 голосов
/ 27 февраля 2019

Я сделал две функции, каждая из которых привязана к действию.Они оба работают хорошо, за исключением одной вещи;Мне нужно, чтобы он работал вместе.

Когда заказ помечен как завершенный, будет сгенерирован купон с использованием wp_rand.Затем я добавил собственный текст в электронное письмо с заказом и использовал $coupon_code в теге span.

Как мне объединить эти два, чтобы в электронном письме отображался сгенерированный код скидки?Есть идеи?

Вот код:

add_action( 'woocommerce_email_before_order_table', 'add_coupon_code_to_order_email', 20, 4 );
function add_coupon_code_to_order_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' ) {
echo '<h2 class="discount-coupon-title">THIS IS YOUR NEXT DISCOUNT</h2><p class="discount-coupon-text">When you\'re ready, welcome back to a 10% discount!<br/> This is your discount code: <code><strong>'.$coupon_code.'</strong></code></p>';
}
?>
<style>
.discount-coupon-title { color: red; }
.discount-coupon-text { color: black; }
</style>
<?php
}


add_action( 'woocommerce_order_status_completed', 'create_coupon_on_order_completed', 10, 1 );
function create_coupon_on_order_completed( $order_id ) {
$order = wc_get_order( $order_id );
$coupon_code = wp_rand();
$amount = '10';
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon' );

$new_coupon_id = wp_insert_post( $coupon );
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', $product_ids );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
unset($product_ids);
}

1 Ответ

0 голосов
/ 27 февраля 2019
add_action('woocommerce_email_before_order_table', 'add_coupon_code_to_order_email', 20, 4);

function add_coupon_code_to_order_email($order, $sent_to_admin, $plain_text, $email) {
    if ($email->id == 'customer_completed_order') {

        $product_ids = '';
        foreach ($order->get_items() as $item) {
            $product_ids .= $item->get_product_id() . ',';
        }



        $coupon_code = wp_rand();
        $amount = '10';
        $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
        $coupon = array(
            'post_title' => $coupon_code,
            'post_content' => '',
            'post_status' => 'publish',
            'post_author' => 1,
            'post_type' => 'shop_coupon');

        $new_coupon_id = wp_insert_post($coupon);
        update_post_meta($new_coupon_id, 'discount_type', $discount_type);
        update_post_meta($new_coupon_id, 'coupon_amount', $amount);
        update_post_meta($new_coupon_id, 'individual_use', 'no');
        update_post_meta($new_coupon_id, 'product_ids', $product_ids);
        update_post_meta($new_coupon_id, 'exclude_product_ids', '');
        update_post_meta($new_coupon_id, 'usage_limit', '1');
        update_post_meta($new_coupon_id, 'expiry_date', '');
        update_post_meta($new_coupon_id, 'apply_before_tax', 'yes');
        update_post_meta($new_coupon_id, 'free_shipping', 'no');
        unset($product_ids);


        echo '<h2 class="discount-coupon-title">THIS IS YOUR NEXT DISCOUNT</h2><p class="discount-coupon-text">When you\'re ready, welcome back to a 10% discount!<br/> This is your discount code: <code><strong>' . $coupon_code . '</strong></code></p>';
    }
?>
    <style>
    .discount-coupon-title { color: red; }
    .discount-coupon-text { color: black; }
    </style>
    <?php

}

Проверено OK с WC 3.5.5 и WP 5.1

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...