У меня есть фрагмент кода для расчета количества ткани, приобретаемой в 1/ Длина 2 метра. Это помогает покупателю определить, сколько он покупает.
Приведенный ниже код работает идеально.
Мне нужно создать альтернативный расчет для 1 /4 метра, который будет отнесен к категории товаров «четверть метра».
Вот полуметровый товар с фрагментом:
https://thefabricboutique.co .uk/product/stonewashed-ramie-cotton-blend-indigo/
Вот ткань длиной четверть метра, для которой нужен альтернативный код:
https://thefabricboutique.co. uk/product/tilda-jubilee-collection-bird-tree-blue/
Пример: Добавьте 1 единицу за 0,25 метра (5,50 фунтов стерлингов)
Код: Выделить всё
add_action( 'woocommerce_before_add_to_cart_quantity', 'display_product_total_amount_html' );
function display_product_total_amount_html() {
global $product;
if( ! has_term( array('half-metre'), 'product_cat', $product->get_id() ) ) return;
$metres_per_qty = 0.5;
$display_price = wc_get_price_to_display($product);
$formatted_price = ''. number_format($display_price, 2) . '';
// Output product total price amount
$price_string = sprintf( __('Add %s for %s (%s)', 'woocommerce'),
'1 unit',
'' . $metres_per_qty . ' metre',
str_replace('0.00', $formatted_price, wc_price(0)) );
// Output product total price amount
echo ''.$price_string.''
?>
jQuery(function($){
$('input[name=quantity]').on( 'input change', function(){
const qty = $(this).val();
if ( qty != 0 ) {
const qtyTxT = qty > 1 ? ' units' : ' unit',
lengthTxT = qty > 2 ? ' metres' : ' metre';
$('.total-price_qty .qty-units').html( parseInt(qty)+qtyTxT );
$('.total-price_qty .qty-metres').html( parseFloat( * qty)+lengthTxT );
$('.total-price_qty .price-amount').html( parseFloat( * qty).toFixed(2) );
}
});
});
Подробнее здесь: [url]https://stackoverflow.com/questions/78523081/change-custom-snippet-product-calculation-from-half-metre-to-quarter-metre[/url]