php 添加自定义优惠券设置,在WooCommerce中启用自动应用功能

gev0vcfq  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(96)

我想添加2复选框到WooCommerce优惠券设置.选中第一个复选框后,将自动将优惠券应用于产品(产品设置在“常规优惠券”选项卡上配置)。第二个复选框,当选中时,将自动应用优惠券的第一个订单的新用户。所以我修改了代码,它是网络。请检查我的代码并给予我一些建议。任何帮助都是感激的。
下面是我的代码:

// Add a new tab
function custom_coupon_settings_tab($tabs) {
    $tabs['automatic_apply'] = array(
        'label' => __('Automatic Apply', 'mohsen'),
        'target' => 'automatic_apply_coupon_data',
        'class' => 'show_if_coupon',
    );
    return $tabs;
}
add_filter('woocommerce_coupon_data_tabs', 'custom_coupon_settings_tab');

// For content to the new tab
function custom_coupon_settings_tab_content() {
    global $post;

    $automatic_apply = get_post_meta($post->ID, 'automatic_apply', true);
    $automatic_apply_first_order = get_post_meta($post->ID, 'automatic_apply_first_order', true);

    ?>
    <div id="automatic_apply_coupon_data" class="panel woocommerce_options_panel">
        <?php
        woocommerce_wp_checkbox(
            array(
                'id' => 'automatic_apply',
                'label' => __('Automatically Apply this Coupon', 'mohsen'),
                'value' => $automatic_apply,
                'desc_tip' => true,
                'description' => __('When checked, this coupon will be automatically applied to eligible products without requiring the user to enter the code manually.', 'mohsen'),
            )
        );

        woocommerce_wp_checkbox(
            array(
                'id' => 'automatic_apply_first_order',
                'label' => __('Automatically Apply for First Order', 'mohsen'),
                'value' => $automatic_apply_first_order,
                'desc_tip' => true,
                'description' => __('When checked, this coupon will be automatically applied to the user\'s first order if they meet the coupon\'s eligibility requirements.', 'mohsen'),
            )
        );
        ?>
    </div>
    <?php
}
add_action('woocommerce_coupon_data_panels', 'custom_coupon_settings_tab_content');

function save_custom_coupon_settings($post_id) {
    $automatic_apply = isset($_POST['automatic_apply']) ? 'yes' : 'no';
    $automatic_apply_first_order = isset($_POST['automatic_apply_first_order']) ? 'yes' : 'no';
    update_post_meta($post_id, 'automatic_apply', $automatic_apply);
    update_post_meta($post_id, 'automatic_apply_first_order', $automatic_apply_first_order);
}
add_action('woocommerce_coupon_options_save', 'save_custom_coupon_settings');

// Automatically apply coupon
function apply_coupon_automatically($coupon_code) {
    if (isset(WC()->cart) && is_a(WC()->cart, 'WC_Cart')) {
        $coupon = new WC_Coupon($coupon_code);
        $automatic_apply = get_post_meta($coupon->get_id(), 'automatic_apply', true);
        $automatic_apply_first_order = get_post_meta($coupon->get_id(), 'automatic_apply_first_order', true);

        if ($automatic_apply === 'yes') {
            WC()->cart->apply_coupon($coupon_code);

            // For First Order
            if ($automatic_apply_first_order === 'yes') {
                $user_id = get_current_user_id();
                $user_orders = wc_get_orders(array(
                    'meta_key' => '_customer_user',
                    'meta_value' => $user_id,
                    'post_status' => array('wc-completed', 'wc-processing'),
                ));

                if (empty($user_orders)) {
                    WC()->cart->apply_coupon($coupon_code);
                }
            }
        }
    }
}
add_action('woocommerce_applied_coupon', 'apply_coupon_automatically');

字符串

rqcrx0a6

rqcrx0a61#

在您的代码中有一些错误,并且您没有使用正确的挂钩来自动应用优惠券。此外,所有付费客户都有一个特定的用户Meta字段,因此不需要查询客户的订单来检查它是否是新客户。
如果您正在使用这些自定义优惠券选项,使用一个优惠券为每个“自动应用”功能 (“正常”和“第一个客户订单”),它应该是轻得多保存数据使用update_option()避免重复繁重的优惠券查询 (见底部,我已经添加了一个附加).
代码:

// Utility function: Get coupon codes with "auto apply" feature enabled
function get_auto_apply_coupons() {
    $coupon_posts = (array) get_posts( array(
        'post_type'         => 'shop_coupon',
        'post_status'       => 'publish',
        'numberposts'       => -1,
        'meta_query'        => array(
            'relation'      => 'OR',
            array(
                'key'       => 'auto_apply',
                'value'     => 'yes',
            ),
            array(
                'key'       => 'auto_apply_1st_order',
                'value'     => 'yes',
            ),
        ),
    ));
    return array_column( $coupon_posts, 'post_name' );
}

// Conditional function: Check if is a paying customer
function is_paying_customer(){
    return WC()->customer->get_is_paying_customer();
}

// Admin counpon pages: Add a new tab
add_filter('woocommerce_coupon_data_tabs', 'custom_coupon_settings_tab');
function custom_coupon_settings_tab($tabs) {
    $tabs['automatic_apply'] = array(
        'label' => __('Automatic Apply', 'woocommerce'),
        'target' => 'auto_apply_coupon_data',
        'class' => 'show_if_coupon',
    );
    return $tabs;
}

// Admin counpon pages: Add fields to the new tab
add_action('woocommerce_coupon_data_panels', 'custom_coupon_settings_tab_content', 10, 2 );
function custom_coupon_settings_tab_content( $coupon_id, $coupon ) 
{
    echo '<div id="auto_apply_coupon_data" class="panel woocommerce_options_panel">';

    woocommerce_wp_checkbox( array(
        'id'            => 'auto_apply',
        'label'         => __('Automatically Apply this Coupon', 'woocommerce'),
        'desc_tip'      => true,
        'description'   => __('When checked, this coupon will be automatically applied to eligible products without requiring the user to enter the code manually.', 'woocommerce'),
    ) );

    woocommerce_wp_checkbox( array(
        'id'            => 'auto_apply_1st_order',
        'label'         => __('Automatically Apply for First Order', 'woocommerce'),
        'desc_tip'      => true,
        'description'   => __('When checked, this coupon will be automatically applied to the user\'s first order if they meet the coupon\'s eligibility requirements.', 'woocommerce'),
    ) );

    echo '</div>';
}

// Admin counpon pages: Save the field values
add_action( 'woocommerce_coupon_options_save', 'save_custom_coupon_settings', 10, 2 );
function save_custom_coupon_settings( $post_id, $coupon ) {
    $coupon->update_meta_data( 'auto_apply', isset($_POST['auto_apply']) ? 'yes' : 'no' );
    $coupon->update_meta_data( 'auto_apply_1st_order', isset($_POST['auto_apply_1st_order']) ? 'yes' : 'no' );
    $coupon->save();
}

// Automatically apply coupon that has "auto apply" feature enabled
add_action('woocommerce_before_calculate_totals', 'apply_coupon_automatically'); 
function apply_coupon_automatically( $cart ) {
    if ((is_admin() && !defined('DOING_AJAX')))
        return;

    if (did_action('woocommerce_before_calculate_totals') >= 2)
        return;

    // Get coupons with auto apply feature
    $auto_coupons = get_auto_apply_coupons(); 

    // Check if an "auto apply" coupon is not already applied to cart
    if ( count( array_intersect( $cart->get_applied_coupons(), $auto_coupons ) ) === 0 ) {
        // loop through "auto apply coupon codes
        foreach( $auto_coupons as $coupon_code ) {
            $coupon = new WC_Coupon($coupon_code); //  Get the coupon object

            $auto_apply = $coupon->get_meta('auto_apply');
            $auto_apply_1st_order = $coupon->get_meta('auto_apply_1st_order');

            // Check if it's a new customer and for coupon(s) with "auto apply" feature
            if ( ! is_paying_customer() && $auto_apply_1st_order === 'yes' ) {
                $cart->apply_coupon( $coupon_code );
                break; // stop the loop
            } elseif ( $auto_apply === 'yes' && is_paying_customer() ) {
                $cart->apply_coupon( $coupon_code );
                break; // stop the loop
            }
        }
    }
}

字符串
代码放在子主题的functions.php文件或插件文件中。经过测试,工作正常。
补充说明:

避免繁重、重复的查询。

在这里,我们将使用update_option()get_option()来避免重复、繁重的查询。“此代码将只处理一个优惠券代码为每个“自动应用”功能.
代码:

// Conditional function: Check if is a paying customer
function is_paying_customer(){
    return WC()->customer->get_is_paying_customer();
}

// Admin counpon pages: Add a new tab
add_filter('woocommerce_coupon_data_tabs', 'custom_coupon_settings_tab');
function custom_coupon_settings_tab($tabs) {
    $tabs['automatic_apply'] = array(
        'label' => __('Automatic Apply', 'woocommerce'),
        'target' => 'auto_apply_coupon_data',
        'class' => 'show_if_coupon',
    );
    return $tabs;
}

// Admin counpon pages: Add fields to the new tab
add_action('woocommerce_coupon_data_panels', 'custom_coupon_settings_tab_content', 10, 2 );
function custom_coupon_settings_tab_content( $coupon_id, $coupon ) 
{
    echo '<div id="auto_apply_coupon_data" class="panel woocommerce_options_panel">';

    woocommerce_wp_checkbox( array(
        'id'            => 'auto_apply',
        'label'         => __('Automatically Apply this Coupon', 'woocommerce'),
        'desc_tip'      => true,
        'value'         => $coupon->get_code() === get_option('auto_apply_coupon') ? 'yes' : 'no',
        'description'   => __('When checked, this coupon will be automatically applied to eligible products without requiring the user to enter the code manually.', 'woocommerce'),
    ) );

    woocommerce_wp_checkbox( array(
        'id'            => 'auto_apply_1st_order',
        'label'         => __('Automatically Apply for First Order', 'woocommerce'),
        'value'         => $coupon->get_code() === get_option('auto_apply_coupon_1st_order') ? 'yes' : 'no',
        'desc_tip'      => true,
        'description'   => __('When checked, this coupon will be automatically applied to the user\'s first order if they meet the coupon\'s eligibility requirements.', 'woocommerce'),
    ) );

    echo '</div>';
}

// Admin counpon pages: Save the field values
add_action( 'woocommerce_coupon_options_save', 'save_custom_coupon_settings', 10, 2 );
function save_custom_coupon_settings( $post_id, $coupon ) {
    $coucon_code = $coupon->get_code();
    if ( isset($_POST['auto_apply']) ) {
        update_option('auto_apply_coupon', $coucon_code );
    } elseif( get_option('auto_apply_coupon') === $coucon_code ) {
        update_option('auto_apply_coupon', '' );
    }
    if ( isset($_POST['auto_apply_1st_order']) ) {
        update_option('auto_apply_coupon_1st_order', $coucon_code );
    } elseif( get_option('auto_apply_coupon_1st_order') === $coucon_code ) {
        update_option('auto_apply_coupon_1st_order', '' );
    }
}

// Automatically apply coupon that has "auto apply" feature enabled
add_action('woocommerce_before_calculate_totals', 'apply_coupon_automatically'); 
function apply_coupon_automatically( $cart ) {
    if ((is_admin() && !defined('DOING_AJAX')))
        return;

    if (did_action('woocommerce_before_calculate_totals') >= 2)
        return;

    // Get coupons with auto apply feature
    $auto_apply = get_option('auto_apply_coupon'); 
    $auto_apply_1st_order = get_option('auto_apply_coupon_1st_order');
    $auto_coupons = array( $auto_apply, $auto_apply_1st_order );
    $applied_coupons = $cart->get_applied_coupons();

    // Check if an "auto apply" coupon is not already applied to cart
    if ( count( array_intersect( $applied_coupons, $auto_coupons ) ) === 0 ) {
        // Check if it's a new customer and for coupon(s) with "auto apply" feature
        if ( ( ! is_paying_customer() && ! empty($auto_apply_1st_order) ) ) {
            $cart->apply_coupon( $auto_apply_1st_order );
        } elseif ( ! empty($auto_apply) && is_paying_customer() ) {
            $cart->apply_coupon( $auto_apply );
        }
    }
}


代码放在子主题的functions.php文件或插件文件中。经过测试,工作正常。

相关问题