Добавить пользовательское поле продукта в электронных письмах WooCommerce «Заказ выполнен» - PullRequest
2 голосов
/ 26 апреля 2020

Можно ли как-нибудь добавить содержимое из настраиваемого поля продукта в электронные письма WooCommerce для «заказа завершено»?

Я добавил этот код в свои функции. php, чтобы сохранить свое настраиваемое поле в метаинформация о продукте.

//1.1 Display Activation Instructions field in admin
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_act_fields');
function woocommerce_product_act_fields(){
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    woocommerce_wp_text_input(
        array(
            'id' => '_act',
            'label' => __('Activation Instructions', 'woocommerce'),
            'desc_tip' => 'true',
            'description' => 'Enter the Activation Instructions Link.'
        )
    );    
    echo '</div>';
}

//1.2 Save Activation Instructions field in product meta
add_action('woocommerce_process_product_meta', 'woocommerce_product_act_field_save');
function woocommerce_product_act_field_save($post_id){
    $woocommerce_act_product_text_field = $_POST['_act'];
    if (!empty($woocommerce_act_product_text_field))
        update_post_meta($post_id, '_act', esc_attr($woocommerce_act_product_text_field));
}

Я также добавил этот код для отображения пользовательского текста в моих электронных письмах woocomerce «заказ выполнен».

add_action( 'woocommerce_email_customer_details', 'bbloomer_add_content_specific_email', 20, 4 );
function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
   if ( $email->id == 'customer_completed_order' ) {
      echo '<h2>Activation & Download Instructions</h2><p style="margin-bottom:35px;">Please refer to 
      our <a href="https://example.com/activation-guides/">Activation Guides</a> for detailed 
      activation and download instructions.</p>';
   }
}

Приведенный выше код работает хорошо, и он отображает пользовательский текст для всех электронных писем с заказом.

Но я хочу заменить часть текста на текстовое поле с данными о продукте. Данные различны для каждого продукта.

Спасибо.

Ответы [ 2 ]

2 голосов
/ 26 апреля 2020

Использование: get_post_meta для извлечения мета-поля сообщения для данного идентификатора сообщения.

//1.1 Display Activation Instructions field in admin
function woocommerce_product_act_fields() {
    echo '<div class="product_custom_field">';

    woocommerce_wp_text_input(
        array(
            'id' => '_act',
            'label' => __('Activation Instructions', 'woocommerce'),
            'desc_tip' => 'true',
            'description' => 'Enter the Activation Instructions Link.'
        )
    );

    echo '</div>';
}
add_action('woocommerce_product_options_inventory_product_data', 'woocommerce_product_act_fields', 10, 0 );

//1.2 Save Activation Instructions field in product meta
function woocommerce_product_act_field_save( $product ) {
    if( isset($_POST['_act']) ) {
        $product->update_meta_data( '_act', sanitize_text_field( $_POST['_act'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'woocommerce_product_act_field_save', 10, 1 );

//2 Add url to email
function woocommerce_product_act_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'customer_completed_order' ) {
        $items = $order->get_items();

        echo '<h2>Activation & Download Instructions</h2>';

        foreach ( $items as $item ) {
            // Get product ID
            $product_id = $item->get_product_id();

            // Get product name
            $product_name = $item->get_name();

            // Get post meta
            $url = get_post_meta( $product_id, '_act', true);

            if ( $url ) {
                echo '<p style="margin-bottom:35px;">For ' . $product_name . ' follow <a href="' . $url. '"> these instructions</a></p>';
            }
        }
    }
}
add_action( 'woocommerce_email_customer_details', 'woocommerce_product_act_email', 20, 4 );
0 голосов
/ 26 апреля 2020
add_action( 'woocommerce_email_customer_details', 'bbloomer_add_content_specific_email', 20, 4 );

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

        $items = $order->get_items();

        foreach ( $items as $item ) {
            // Get product ID
            $product_id = $item->get_product_id();

            // Get post meta
            $url = get_post_meta( $product_id, '_act', true );

            if ( $url ) {
                echo '<h2>'.$item->get_name().'Activation & Download Instructions</h2><p style="margin-bottom:35px;">Please refer to our <a href="' . $url . '">Activation Guides</a> for detailed activation and download instructions.</p>';
            }
        }
    }
}

Попробуйте этот код

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