php - 是否可以使用 wpdb 查询或 woocommerce wordpress 中的 ajax 调用来更新购物车总数?
问题描述
根据我的客户 HTML 设计,没有提供购物车页面设计,而不是他们希望product name, image , price , sub-total and total
在结帐页面中显示并计算结帐页面中的所有内容的购物车。
他们提供了结帐页面设计,例如
在此页面中,一旦用户填写表格并单击继续,它将转到下一个
第二页
在这个页面中,会显示第一个表格填写数据并要求选择运输方式,选择方式后右侧总计将被更新并单击继续它会转到下一步
第三页……
在此页面中,显示上一页详细信息,单击立即付款按钮后,将创建订单。
我不知道如何管理这种情况,但我尝试使用 jQuery ajax 来管理这些东西,但是一旦我来到第二页,我想根据更改运输方式更新总价。
我尝试过使用不同钩子的方法,但它不起作用。
add_action( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function custom_calculated_total( $total, $cart ){
echo "dsfsd";exit();
return round( $total - ($total * 0.15), $cart->dp );
}
add_filter( 'woocommerce_calculated_total', 'woocommerce_header_add_to_cart_fragment','doAjax' );
function woocommerce_header_add_to_cart_fragment( $wc_price ) {
$wc_price = 10.5;
return $wc_price;
}
一旦我使用了这个钩子,就会自动更新而不选择任何运输方式。
此外,我还尝试在 ajax 调用更改运输方式以根据运输方式价格更新购物车总数。
add_action('wp_footer', 'checkout_billing_email_js_ajax' );
function checkout_billing_email_js_ajax() {
// Only on Checkout
if( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery(function($){
$(document).ready(function(){
$(document).on("click", ".shipping_method" ,function(e) {
//alert($(this).val())
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
// url: cart_ajax.ajax_url,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
enctype: 'multipart/form-data',
data: {
'action': 'ajax_order',
'fields': $('form.checkout').serializeArray(),
'user_id': <?php echo get_current_user_id(); ?>,
},
success: function (result) {
console.log(result); // For testing (to be removed)
},
error: function(error) {
console.log(error); // For testing (to be removed)
}
});
});
});
});
</script>
<?php
endif;
}
回调函数方法一
add_action('wp_ajax_ajax_order', 'submited_ajax_order_data');
add_action( 'wp_ajax_nopriv_ajax_order', 'submited_ajax_order_data');
function submited_ajax_order_data()
{
global $woocommerce;
$valorTotal = 10.5;
WC()->cart->total = $valorTotal;
WC()->cart->calculate_totals();
wc_price($valorTotal);*/
$woocommerce->cart->set_total($woocommerce->cart->total + $valorTotal);
}
回调函数方法2:当我在回调函数中调用动作挂钩时不起作用
add_action('wp_ajax_ajax_order', 'submited_ajax_order_data');
add_action( 'wp_ajax_nopriv_ajax_order', 'submited_ajax_order_data' );
function submited_ajax_order_data()
{
add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
}
function custom_cart_total() {
WC()->cart->total *= 10.25;
var_dump( WC()->cart->total);
}
我知道这是个大问题,但有人帮我怎么做。(我想根据更改运输方式更新购物车总数)
解决方案
您可以使用woocommerce_cart_calculate_fees
挂钩添加折扣并自动更改订单总额。
也可以使用woocommerce_calculated_total
挂钩,但您将无法跟踪应用的折扣(如果您必须在一天内重新计算订单总额,除非您添加自定义订单元数据)。
使用WC()->session->get('chosen_shipping_methods');
您将获得一个数组,其中包含客户选择的运输方式的 ID。
我创建了两个变量:
$chosen_method_id
:返回所选运输方式的 id,如果您想检查特定运输区域的特定方式(例如local_pickup:6
) ,可以使用它$chosen_method
:如果您只想检查是否是,则返回要使用的运输方式的类型flat_rate
,free_shipping
或者local_pickup
您选择要使用的那个。
然后:
// adds a custom discount to the cart based on the shipping method chosen
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_discount_based_on_shipping_method', 10, 1 );
function add_custom_discount_based_on_shipping_method( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// returns an array with the ids of the selected shipping methods
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( empty( $chosen_methods ) ) {
return $total;
}
// get the shipping method id (for example: "flat_rate:1")
$chosen_method_id = $chosen_methods[0];
// get the shipping method (for example: "flat_rate", "free_shipping" or "local_pickup")
$chosen_method = explode( ':', $chosen_method_id )[0];
switch ( $chosen_method ) {
case 'flat_rate':
// 15% of discount
$percentage = 15;
break;
case 'free_shipping':
// 10% of discount
$percentage = 10;
break;
case 'local_pickup':
// 20% of discount
$percentage = 20;
break;
}
// gets cart contents total
$cart_total = $cart->cart_contents_total;
$discount = $cart_total * $percentage / 100;
if ( isset( $discount ) && $discount > 0 ) {
$cart->add_fee( __( "Discount", 'woocommerce' ), -$discount );
}
}
该代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。
推荐阅读
- caching - web.config 缓存设置不起作用
- export - 将部分页面渲染到文件
- node.js - 多容器 Docker 应用程序部署失败
- python - python 正则表达式读取部分文本文件
- javascript - Firefox 停止调用 AJAX 函数
- python - 根据列标题添加新行
- f# - Xamarin Android:资源未拾取 F# 图标
- r - dimnames(s$v) <- list(colnames(x), paste0("PC", j)) 中的错误:dimnames 2 的错误长度不等于数组范围
- asp.net-core - asp.net核心部署持久错误代码='0x80004005:8000808c。在赢 2012r2
- azure - 逻辑应用性能是否取决于连接器?