WooCommerce заменяет «Доступно по предзаказу» в корзине / оформлении заказа в зависимости от категории продукта - PullRequest
1 голос
/ 17 апреля 2020

Я написал код для отображения настраиваемого сообщения о задержке заказа на странице сведений о продукте, основанной на категории продукта.

function custom_backorder_message( $text, $product ){
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {

        if( has_term( 'bridal-line', 'product_cat' ) ) {
            $text = __( 'Your piece will be handcrafted for you. Upon order we will manufacture your piece of eternity. Sadly, we can not give you a timeline, due to Covid 19, but are expecting 5-7 weeks', 'text-domain' );
        }else {
            $text = __( 'This product is currently out of stock, but upon order we will handcraft your piece. Sadly, we can not give you a timeline, due to Covid 19, but are expecting 6-8 week.', 'text-domain' );
        }
    }
    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_backorder_message', 10, 2 );

Прямо сейчас, на странице корзины отображается «Доступно по заказу». Как я могу показать правильное сообщение о задержке заказа?

Любая помощь приветствуется!

1 Ответ

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

Использование: woocommerce_cart_item_backorder_notification

Обратите внимание, что третий параметр ($product_id) указан как has_term, это потому, что по умолчанию текущий пост (ID) используется. Однако, если в корзине несколько товаров, есть несколько идентификаторов ...

// Change backorder notification - Single product page
function custom_availability_text( $text, $product ) {
    // Returns whether or not the product is stock managed.
    if ( $product->managing_stock() && $product->is_on_backorder( 1 ) ) {
        // Check if the current post has any of given terms.
        if( has_term( 'bridal-line', 'product_cat' ) ) {
            $text = __( 'My first text', 'woocommerce' );
        } else {
            $text = __( 'My second text', 'woocommerce' );
        }
    }
    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'custom_availability_text', 10, 2 );

// Change backorder notification - Shop page
function custom_cart_item_backorder_notification( $html, $product_id ){
    // Check if the current post has any of given terms.
    if ( has_term( 'bridal-line', 'product_cat', $product_id ) ) {
        $html = '<p class="backorder_notification">' . esc_html__( 'My first text', 'woocommerce' ) . '</p>';
    } else {
        $html = '<p class="backorder_notification">' . esc_html__( 'My second text', 'woocommerce' ) . '</p>';
    }

    return $html;
}
add_filter( 'woocommerce_cart_item_backorder_notification', 'custom_cart_item_backorder_notification', 10, 2 );
...