【问题标题】:Limit to one applied coupon, removing other previous applied coupons in Woocommerce限制为一张应用的优惠券,在 Woocommerce 中删除其他以前应用的优惠券
【发布时间】:2019-04-13 18:31:54
【问题描述】:

我正在动态制作优惠券以使用用户电子邮件作为优惠券,但我如何限制用户每个购物车仅使用一张优惠券。如果使用多个自动从购物车中删除前一个。

    add_filter ( 'woocommerce_get_shop_coupon_data', 'generate_coupons', 10, 2  );
    function generate_coupons( $data, $code) {
        global $wpdb, $woocommerce;
        $vpm_options        = get_option( 'vpm_email_coupon_option_name' ); // Array of All Options
        $amount             = $vpm_options['coupon_value_3']; // coupon_discount
        $offer_type         = $vpm_options['offer_type_2'];  // Type: fixed_cart, percent, fixed_product, percent_product
        //getting the coupon input value 
        if (filter_var($code, FILTER_VALIDATE_EMAIL) && email_exists( $code )) {
            $code = $code ;
        }else{
            $code = '';
        }  
        // Check if the coupon has already been created in the database
        $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $code );
        $coupon_id = $wpdb->get_var( $sql );

        if ( empty( $coupon_id ) ) {
            // Create a coupon with the properties you need
            $data = array(
                'discount_type'              => $offer_type,
                'coupon_amount'              => $amount, // value
                'product_ids'                => array(),
                'exclude_product_ids'        => array(),
                'usage_limit'                => '',
                'usage_limit_per_user'       => '',//Limit
                'limit_usage_to_x_items'     => '',
                'usage_count'                => '',
                'expiry_date'                => '2020-12-31', // YYYY-MM-DD
            );
            // Save the coupon in the database
            $coupon = array(
                'post_title'    => $code,
                'post_content'  => '',
                'post_status'   => 'publish',
                'post_author'   => 1,
                'post_type'     => 'shop_coupon'
            );
            $new_coupon_id = wp_insert_post( $coupon );

            // Write the $data values into postmeta table
            foreach ($data as $key => $value) {
                update_post_meta( $new_coupon_id, $key, $value );
            }
            //apply the coupon 
            $woocommerce->cart->add_discount( $code );
            return $data;
        }
    }

【问题讨论】:

    标签: php wordpress woocommerce cart coupon


    【解决方案1】:

    以下代码将删除第一个应用的优惠券,如果客户应用另一个优惠券代码,那么购物车中将只有一个应用的优惠券(最后应用的优惠券)

    add_action( 'woocommerce_before_calculate_totals', 'one_applied_coupon_only', 10, 1 );
    function one_applied_coupon_only( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // For more than 1 applied coupons only
        if (  sizeof($cart->get_applied_coupons()) > 1 && $coupons = $cart->get_applied_coupons() ){
            // Remove the first applied coupon keeping only the last appield coupon
            $cart->remove_coupon( reset($coupons) );
        }
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多