我創造WooCommerce一個插件車創建自定義的折扣,並有一個小問題與添加自定義折讓車/結帳頁面。如何在woocommerce
如何我可以將自定義折讓車,而無需創建優惠券?假設我想在購物車頁面上打折5美元。我怎樣才能做到這一點?
下面是從那裏我已經使用優惠券申請折扣的插件文件我的代碼,但我想添加其他自定義的折扣,而無需使用優惠券。在插件文件
行動掛鉤:
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);
$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;
$coupon_code = 'wooreward_discount';
if($user_points>=$points_set){
// this following Code is optional and can be removed......as there is no need of if statement here
if ($woocommerce->cart->has_discount($coupon_code)) {
/*$woocommerce->add_error(__('Coupon Code Already Applied.!!','woocommerce'));*/
return false;
}else{
$woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
$woocommerce->add_message(__('Taxco925 Reward Discount Applied.!!','woocommerce'));
}
}else{
$woocommerce->add_error(__('Not Enough Taxco925 Points.!!','woocommerce'));
}
}else{
$woocommerce->add_error(__('You have have not earned any Taxco925 Points yet.!!','woocommerce'));
}
}
}
正如你可以看到這行$woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
增加了我的折讓車。但它在後臺使用優惠券來執行此操作。有沒有什麼辦法可以在不使用優惠券的情況下添加自定義折扣。
基於wooCommerce API,您不應該使用add_fee與負數。鏈接:http://woocommerce.wp-a2z.org/oik_api/wc_cartadd_fee/ –