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

问题描述 投票:0回答:1

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

类似代码的参考,但仅适用于特定产品:
有条件地针对特定产品 ID 和数量自动应用优惠券

图像示例:

php wordpress woocommerce discount product-quantity
1个回答
0
投票

尝试为我的以下问题创建自定义解决方案。并以某种方式做到了,不确定这是否是正确且好的选择,但它至少按照我的需要对我有用。这将为商店中的每个产品创建一个单独的优惠券(如果添加新产品,它也会为其创建一个唯一的优惠券)。如果购物车中的产品数量为 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' );}
                }
            }
        }
    }
}
}
© www.soinside.com 2019 - 2024. All rights reserved.