Display the total price on the WooCommerce single product page with the original product price * minimum quantity(在WooCommerce单一产品页面上显示总价格和原始产品价格*最小数量)
本文介绍了在WooCommerce单一产品页面上显示总价格和原始产品价格*最小数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望创建一个PHP代码片段,在WooCommerce单一产品页面上显示原始产品价格*最小数量的总价格
根据WooCommerce - auto update total price when quantity changed答案代码,我目前掌握的情况如下:
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Prezzo Totale:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
}
});
});
</script>
<?php
}
问题是,当查看单一产品页面时,它会显示一个产品数量的价格。更改事件发生在数量已更改时,因此在此事件发生之前不执行任何计算。
我有一个B2B网站,所以当客户查看单个产品页面时,他们会立即看到最低数量,那么我如何才能立即获得每种产品最低数量的总价?
推荐答案
以下代码在加载页面时考虑产品数量,然后在更改产品数量时更新价格。
通过代码中添加的注释标签进行解释
function action_woocommerce_single_product_summary() {
global $product;
// Getters
$price = $product->get_price();
$currency_symbol = get_woocommerce_currency_symbol();
// let's setup our div
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Product Total:','woocommerce'), '<span class="price">' . $currency_symbol . $price . '</span>' );
?>
<script>
jQuery(function($) {
// jQuery variables
var price = <?php echo $price; ?>,
currency = '<?php echo $currency_symbol; ?>',
quantity = $( '[name=quantity]' ).val();
// Code to run when the document is ready
var product_total = parseFloat( price * quantity );
$( '#product_total_price .price' ).html( currency + product_total.toFixed( 2 ) );
// On change
$( '[name=quantity]' ).change( function() {
if ( ! ( this.value < 1 ) ) {
product_total = parseFloat( price * this.value );
$( '#product_total_price .price' ).html( currency + product_total.toFixed( 2 ) );
}
});
});
</script>
<?php
}
// We are going to hook this on priority 31, so that it would display below add to cart button.
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0 );
这篇关于在WooCommerce单一产品页面上显示总价格和原始产品价格*最小数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在WooCommerce单一产品页面上显示总价格和原始产品价格*最小数量


猜你喜欢
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01