WooCommerce 应用优惠券取决于购物车订单项数量

WooCommerce apply coupon depends of cart line item quantity

经过长时间的查找,我无法找到任何正确的代码如何为购物车订单项应用优惠券。假设客户添加了一些数量为 10 的产品,我选择的优惠券应该适用于该产品。如果他添加了另一个数量超过 10 件的产品,则应为该产品再次应用相同的优惠券。 这里有什么帮助吗? 我能够找到类似的东西,但这只适用于特定的产品 ID,如何更新此代码以浏览每个购物车产品,检查它们的数量并为数量为 10 或更多的产品应用优惠券的任何帮助?

类似代码的参考,但仅针对特定产品:

图片示例:

尝试为我的以下问题创建自定义解决方案。并以某种方式做到了,不确定这是否是正确和好的选择,但它至少对我有用,正是我需要的。这会为商店中的每个产品创建一个单独的优惠券(如果添加了新产品,它也会为其创建一个唯一的优惠券)。如果购物车中的产品数量为 10 件或更多,优惠券将自动应用于每个购物车订单项。它为该产品提供 10% 的折扣。按照下面的代码,也许对某人有用,因为我在任何地方都找不到任何插件或代码可以像这样工作...

$args = array(
    'posts_per_page'   => -1,
    'orderby'          => 'title',
    'order'            => 'asc',
    'post_type'        => 'shop_coupon',
    'post_status'      => 'publish',
);
    
$all_coupons = get_posts( $args );

// Loop through the available coupons
foreach ( $all_coupons as $coupon ) {
    // Get the name for each coupon and add to the previously created array
    $coupon_name = $coupon->post_title;
}

foreach ($all_coupons as $coupon) {
    $coupons_array[] = $coupon->post_title;
}

$all_ids = get_posts( array(
        'post_type' => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'fields' => 'ids',
   ) );

   foreach ( $all_ids as $id ) {
       $product_id_array[] = $id;
    }

// Get values from arr2 and arr1 unique
 $output = array_merge(array_diff($coupons_array, $product_id_array), array_diff($product_id_array, $coupons_array));
    
function coupon_exists($coupon_code) {
    global $wpdb;
        $sql = $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_type = 'shop_coupon' AND post_name = '%s'", $coupon_code );
        $coupon_codes = $wpdb->get_results($sql);
        if (count($coupon_codes)> 0) {
            return true;
        }
    else {
            return false;
        }
    }

    foreach ($output as $o) {
        if (is_numeric($o)) {
            if (!coupon_exists($o)) {
                generate_coupon($o);
            }
        }
}

function generate_coupon($coupon_code){
                $coupon = new WC_Coupon();
                $coupon->set_code($coupon_code);
                //the coupon discount type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart'
                $coupon->set_discount_type('percent_product');
                //the discount amount, defaults to zero
                $coupon->set_amount(10);
                $coupon->set_individual_use(false);
                 $coupon->set_product_ids(array($coupon_code));
                //save the coupon
                $coupon->save();
                return $coupon_code;
}


add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {

       $all_ids = get_posts( array(
        'post_type' => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'fields' => 'ids',
       ) );

    if ( !WC()->cart->is_empty() ){


        // First cart loop: Counting number of subactegory items in cart
        foreach ( $all_ids as $id ){
        foreach ( WC()->cart->get_cart() as $cart_item ){
                if( $id == $cart_item['data']->id ){
                    if( 10 <= $cart_item['quantity']  ){
                        WC()->cart->add_discount( $id );
                        //wc_add_notice( __( 'Discount of <strong>10%</strong> for quantity.', 'theme_domain' ), 'success' );
                    }else{
                         WC()->cart->remove_coupon( $id );
                         //wc_add_notice( __( 'Discount of <strong>10%</strong> due to low quantity removed.', 'theme_domain' ), 'success' );}
                }
            }
        }
    }
}
}