很抱歉再次将此代码片段带到这里。尽管我正在努力从所获得的帮助中学习,所以我真的很感谢这些帮助。
我有一个代码片段来计算所购买的 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 = '<span class="price-amount">'. number_format($display_price, 2) . '</span>';
// Output product total price amount
$price_string = sprintf( __('Add %s for %s (%s)', 'woocommerce'),
'<span class="qty-units">1 unit</span>',
'<span class="qty-metres">' . $metres_per_qty . ' metre</span>',
str_replace('0.00', $formatted_price, wc_price(0)) );
// Output product total price amount
echo '<div class="total-price_qty" style="margin-bottom:20px;">'.$price_string.'</div>'
?>
<script>
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(<?php echo $metres_per_qty; ?> * qty)+lengthTxT );
$('.total-price_qty .price-amount').html( parseFloat(<?php echo $display_price; ?> * qty).toFixed(2) );
}
});
});
</script>
<?php
}
尝试以下修改后的代码来处理您的 2 个长度单位:
add_action( 'woocommerce_before_add_to_cart_quantity', 'display_product_total_amount_html' );
function display_product_total_amount_html() {
global $product;
$is_quarter = has_term('quarter-metre', 'product_cat', $product->get_id());
$is_half = has_term('half-metre', 'product_cat', $product->get_id());
if( ! ($is_quarter || $is_half) ) return;
$metres_per_qty = $is_half ? 0.5 : 0.25;
$display_price = wc_get_price_to_display($product);
$formatted_price = '<span class="price-amount">'. number_format($display_price, 2) . '</span>';
$price_string = sprintf( __('Add %s for %s (%s)', 'woocommerce'),
'<span class="qty-units">1 unit</span>',
'<span class="qty-metres">' . $metres_per_qty . ' metre</span>',
str_replace('0.00', $formatted_price, wc_price(0)) );
// Output HTML
printf('<div class="total-price_qty" style="margin-bottom:20px;">%s</div>', $price_string);
// Enqueue Javascript code
wc_enqueue_js( "$('input[name=quantity]').on('input change', function(){
const qty = $(this).val();
if ( qty != 0 ) {
const qtyTxT = qty > 1 ? ' units' : ' unit',
lengthTxT = qty >" . intval(1 / $metres_per_qty) . " ? ' metres' : ' metre';
$('.total-price_qty .qty-units').html( parseInt(qty)+qtyTxT );
$('.total-price_qty .qty-metres').html( parseFloat({$metres_per_qty} * qty)+lengthTxT );
$('.total-price_qty .price-amount').html( parseFloat({$metres_per_qty} * qty).toFixed(2) );
}
});" );
}
对于“四分之一米”类别,您将得到类似以下内容:
对于“半米”类别,您将得到类似以下内容: