我正在创建一个新的 woocommerce 插件,它将在购物车页面顶部提供应用折扣的选项。
现在,当购物车中有产品时,用户将点击 Apply Discount 按钮,当点击并从我的自定义插件触发操作时,将为该特定购物车订单添加购物车折扣。
到目前为止,它运行良好,还显示应用了购物车折扣。下面是截图
正如您从屏幕截图中看到的那样,订单总计显示计算出了错误的数字。 以下是我的自定义 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.!!';
}
}
}
如何在添加的购物车折扣上更新购物车总数?
最佳答案
也许这不是您想要 100% 实现目标的方式,但您可以即时生成优惠券:
$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
关于php - 应用于购物车的购物车折扣在 wordpress 中使用 Woocommerce 时未在订单总计中显示正确的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21927352/