微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在woocommerce中为购物车创建自定义折扣

我在WooCommerce中创建了一个插件,并且在向CART / CHECKOUT页面添加自定义折扣时遇到了一个小问题.

如何在不创建优惠券的情况下将自定义折扣应用于购物车?
假设我想在购物车页面上给予5美元的折扣.我怎样才能做到这一点?

以下是我使用优惠券应用折扣的插件文件中的代码,但我想在不使用优惠券的情况下添加一个自定义折扣.

插件文件中的Action Hook:

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));
将我的折扣添加到购物车.但它在后台使用优惠券来做到这一点.有没有办法可以在不使用优惠券的情况下添加自定义折扣.

add_action('woocommerce_checkout_order_processed','custom_disount',10,1);
function custom_disount($order_id){
    $order = wc_get_order($order_id);
    $order_items = $order->get_items();
    foreach ($order_items as $order_item_key => $order_item) {
        $product = new WC_Product((int) $order_item['product_id']);
        $quantity = (int) $order_item['qty'];
        $discount=($product->regular_price*30)/100;  //30% disount.
        wc_update_order_item_Meta($order_item_key,'_line_total',($product->regular_price*$quantity)-($discount*$quantity));
    }
}

原文地址:https://www.jb51.cc/php/133096.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐