WooCommerce: Переопределение цены корзины с помощью текста
У нас есть множество продуктов с любым из них:
- Без цены
- Нулевая цена
Мы сделали их доступными для покупки с помощью встроенного крючка, но в корзине они по-прежнему отображаются как имеющие 0 price
во время оформления заказа.
Мы хотели бы, чтобы в сводке по корзине и оформлению заказа отображался "Специальный заказ" или любой другой текст, но, похоже, WooCommerce аннулирует текстовые цены.
Попробовал это : WooCommerce: Добавить товар в корзину с ценой переопределение?
Вышеописанное отлично работает с переопределением числа, но при попытке переопределения текста по умолчанию возвращается отображение 0 price
.
Почему? Эти продукты изготавливаются на заказ, и администратор магазина обновит цену после разговора с клиентом/поставщиком.
2 answers
Вам нужно отфильтровать строки, отображающие цену и промежуточный итог в корзине. Ссылка, которую вы упомянули, обсуждала изменение фактической цены. В вашем случае цена равна $0, пока вы не установите фактическую цену позже. Вероятно, есть фильтры и для итогов корзины, но это должно быть началом:
add_filter( 'woocommerce_cart_item_price', 'so_38057349_cart_item_price', 10, 3 );
function so_38057349_cart_item_price( $price, $cart_item, $cart_item_key ) {
if ( $cart_item[ 'data' ]->price == 0 ) {
$price = __( 'Special Order', 'yourtheme' );
}
return $price;
}
add_filter( 'woocommerce_cart_item_subtotal', 'so_38057349_cart_item_subtotal', 10, 3 );
function so_38057349_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
if ( $cart_item[ 'data' ]->price == 0 ) {
$subtotal = __( 'To be determined', 'yourtheme' );
}
return $subtotal;
}
add_filter( 'woocommerce_order_formatted_line_subtotal', 'so_38057349_order_item_subtotal', 10, 3 );
function so_38057349_order_item_subtotal( $subtotal, $item, $order ) {
if ( isset( $item[ 'line_subtotal' ] ) && $item[ 'line_subtotal' ] == 0 ) {
$subtotal = __( 'To be determined', 'yourtheme' );
}
return $subtotal;
}
Конечно, это также относится к любому продукту с ценой 0 и, возможно, не только к тем, которые вы настроили на заказ, поэтому вам может потребоваться более условная логика чем я здесь снабдил.
Чтобы продолжить ваш комментарий.... woocommerce_order_amount_total
- это числовое значение, а не отображаемый html. Вы можете видеть функции, вызываемые в cart-totals.php
шаблон.
function so_38057349_woocommerce_cart_subtotal( $cart_subtotal, $compound, $cart ) {
if( $cart->subtotal == 0 ){
$cart_subtotal = __( 'Order subtotal to be determined', 'yourtheme' );
}
return $cart_subtotal;
};
add_filter( 'woocommerce_cart_subtotal', 'so_38057349_woocommerce_cart_subtotal', 10, 3 );
// define the woocommerce_order_amount_total callback
function so_38057349_woocommerce_order_amount_total( $order_total ) {
if( WC()->cart->get_total() == 0 ){
$order_total = __( 'Order total to be determined', 'yourtheme' );
}
return $order_total;
};
add_filter( 'woocommerce_cart_totals_order_total_html', 'so_38057349_woocommerce_order_amount_total' );
Обновленный снимок экрана:
Развивая ответ Хельги, вот полный код, который изменяет цены на "подлежащие определению" на страницах корзины, страницах оформления заказа и электронных письмах с подтверждением заказа. Текст сохранен разным, чтобы люди могли определить, куда идет каждый фильтр.
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price, $cart_item, $cart_item_key ) {
if ( $cart_item[ 'data' ]->price == 0 ) {
$price = __( 'Special Order', 'yourtheme' );
}
return $price;
}
add_filter( 'woocommerce_cart_item_subtotal', 'filter_cart_item_subtotal', 10, 3 );
function filter_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
if ( $cart_item[ 'data' ]->price == 0 ) {
$subtotal = __( 'To be determined', 'yourtheme' );
}
return $subtotal;
}
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $instance ) {
$cart_subtotal = __( 'To be determined 4', 'yourtheme' );
return $cart_subtotal;
};
add_filter( 'woocommerce_order_formatted_line_subtotal', 'filter_order_item_subtotal', 10, 3 );
function filter_order_item_subtotal( $subtotal, $item, $order ) {
if ( isset( $item[ 'line_subtotal' ] ) && $item[ 'line_subtotal' ] == 0 ) {
$subtotal = __( 'To be determined 2', 'yourtheme' );
}
return $subtotal;
}
add_filter( 'woocommerce_order_subtotal_to_display', 'filter_woocommerce_order_subtotal_to_display', 10, 3 );
function filter_woocommerce_order_subtotal_to_display( $subtotal, $compound, $instance ) {
$subtotal = __( 'To be determined 6', 'yourtheme' );
return $subtotal;
};
add_filter( 'woocommerce_get_formatted_order_total', 'filter_woocommerce_get_formatted_order_total', 10, 2 );
function filter_woocommerce_get_formatted_order_total( $formatted_total, $instance ) {
$formatted_total = __( 'To be determined 8', 'yourtheme' );
return $formatted_total;
};