WooCommerce — Пользовательское уведомление на страницах заказа с благодарностью и просмотром моей учетной записи

В WooCommerce у меня есть настраиваемое поле days_manufacture для каждого продукта с разными (целочисленными) значениями.

Также у меня есть этот код, который отображает сообщение на странице корзины с максимальным значением "дни изготовления":

add_action('woocommerce_before_cart', 'days_of_manufacture');
function days_of_manufacture() {
    $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
    $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
    $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
    $max_days = 0;

    foreach( WC()->cart->get_cart() as $cart_item )
        if($cart_item['days_manufacture'] > $max_days)
            $max_days = $cart_item['days_manufacture'];

    if($max_days != 0) {
        if ($max_days == 1)
            $days_txt = $day_txt;

        $output = $text . $max_days . $days_txt;
        echo "<div class='woocommerce-info'>$output</div>";
    }
}

Теперь я хотел бы отобразить это сообщение на странице порядка просмотра thankyou и на страницах My account > Orders > View order.

Является ли это возможным?

Спасибо!


person Felipe Honorato    schedule 12.09.2016    source источник


Ответы (1)


Да, это так… но поскольку в соответствующих шаблонах для этого нет доступных хуков, вам придется переопределить 2 шаблона (для этого прочитайте как переопределить шаблоны woocommerce через тему).

Шаг 1. Функция

function days_of_manufacture_order_view($order) {
        $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
        $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
        $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
        $max_days = 0;

        foreach( $order->get_items() as $item )
            if(get_post_meta($item['product_id'], 'days_manufacture', true) > $max_days )
                $max_days = get_post_meta($item['product_id'], 'days_manufacture', true);

        if($max_days != 0) {
            if ($max_days == 1)
                $days_txt = $day_txt;

            $output = $text . $max_days . $days_txt;
            echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // forcing display for some themes
        }
}

Этот код находится в файле function.php вашей активной дочерней темы (или темы), а также в любом файле плагина.

Шаг 2. Вставка функции в шаблоны

1) Шаблон заказа просмотра Личного кабинета:

Этот шаблон находится в your_theme/woocommerce/myaccount/view-order.php

Вот выдержка из этого шаблона (с функцией внутри него):

<?php
/**
 * View Order
 *
 * Shows the details of a particular order on the account page.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/view-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.6.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

?>
<?php days_of_manufacture_order_view($order); // <== inserted Here ?>
<p><?php

// … … …

2) Шаблон заказа на просмотр благодарности:

Этот шаблон находится в your_theme/woocommerce/checkout/thankyou.php

Вот выдержка из этого шаблона (с функцией внутри него):

<?php
/**
 * Thankyou page
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/checkout/thankyou.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
if ( $order ) : ?>

<?php days_of_manufacture_order_view($order); // inserted Here ?>

    <?php if ( $order->has_status( 'failed' ) ) : ?>

Этот код протестирован и работает


Рекомендации:

person LoicTheAztec    schedule 12.09.2016