【发布时间】:2014-03-22 13:11:43
【问题描述】:
我正在创建一个新的 woocommerce 插件,它将在购物车页面顶部提供应用折扣的选项。
现在,当购物车中有产品时,用户将单击“应用折扣”按钮,当单击时,我的自定义插件会触发操作,该插件会将购物车折扣添加到该特定购物车订单。
到目前为止,它运行良好,并且还显示应用了购物车折扣。下面是截图
正如您从屏幕截图中看到的那样,Order Total 显示的数字计算错误。 以下是我的自定义 woocommerce 插件文件中的代码。
提交按钮时会调用以下操作
if(!empty($_POST['apply_discount_woo'])){
add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action'));
}
及功能码:
public function cart_order_total_action(){
if ( is_user_logged_in() ){
global $woocommerce;
global $current_user;
global $wpdb;
$u_id = $current_user->ID;
$table_name = $wpdb->prefix."woocommerce_customer_reward_ms";
$thetable2 = $wpdb->prefix . "woocommerce_customer_reward_cart_ms";
$table_name3 = $wpdb->prefix."woocommerce_customer_reward_points_log_ms";
$data = $wpdb->get_row("SELECT * from $table_name where id=$u_id");
$data2 = $wpdb->get_row("SELECT * from $thetable2");
/* Order Id goes here */
$orders=array();//order ids
$args = array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => 'shop_order',
'post_status' => 'publish',
'tax_query'=>array(
array(
'taxonomy' =>'shop_order_status',
'field' => 'slug',
'terms' =>'on-hold'
)
)
);
$posts=get_posts($args);
//get the post ids as order ids
$orders=wp_list_pluck( $posts, 'ID' );
$order = $orders[0];
/* Order Id ends here */
if($data){
$user_points = $data->points;
$points_set = $data2->woo_pts_set;
print_r($woocommerce->cart->applied_coupons);
echo $woocommerce->cart->discount_cart;
echo $woocommerce->cart->get_cart_total();
/* the line below adds the cart discount to the Cart */
$woocommerce->cart->discount_cart = $data2->woo_discount_set;
}else{
echo 'You dont have any woo points yet.!!';
}
}
}
如何在添加购物车折扣时更新购物车总数?
【问题讨论】:
-
或者还有其他方式,可以添加购物车的Discount吗???
标签: php wordpress woocommerce