在Woocommerce中启用计算购物车折扣限制

时间:2018-05-01 11:25:04

标签: php wordpress woocommerce cart discount

我试图在不使用优惠券的情况下整理WooCommerce折扣,除了"限制"功能

每位新客户在购买时将获得20%(首次购物者),但20%不能也不应超过15美元。

这是一个插件代码,我需要帮助的是如何"告诉它"如果购物车折扣金额高于15,则将其删除并放入15。

这不起作用: if ( $discount = $discountValue/100 >15) return $discountLimit;

完整的代码:

    function loc_first_order_add_admin_menu(  ) { 
    add_submenu_page( 'woocommerce', 'First Order Discount', 'First Order Discount', 'manage_options', 'woocomerce_first_order_discount', 'first_order_add_options_page' );
}

function loc_first_order_add_settings_init(  ) { 
    register_setting( 'pluginPage', 'first_order_add_settings' );
    add_settings_section( 'first_order_add_pluginPage_section', __( 'This will give all first time customers a discount of your choice without using a coupon.', 'woo-first-discount' ), '', 'pluginPage' );
    add_settings_field( 'first_order_choose', __( 'Type', 'woo-first-discount' ), 'loc_first_order_options', 'pluginPage', 'first_order_add_pluginPage_section' );
    add_settings_field( 'first_order_add_value',  __( 'Value', 'woo-first-discount' ), 'loc_first_order_number', 'pluginPage',  'first_order_add_pluginPage_section' );
    add_settings_field( 'first_order_add_limit',  __( 'Limit', 'woo-first-discount' ), 'loc_first_order_limit', 'pluginPage',   'first_order_add_pluginPage_section' );
}

function loc_first_order_options(  ) { 
    $options = get_option( 'first_order_add_settings' );
    ?>
    <input id="off" type='radio' name='first_order_add_settings[first_order_choose]' <?php checked( $options['first_order_choose'], 'off' ); ?> value='off'>
    <label for="off"><?php echo __( 'Disable', 'woo-first-discount' ); ?></label>
    <br>
    <input id="fixed" type='radio' name='first_order_add_settings[first_order_choose]' <?php checked( $options['first_order_choose'], 'fixed' ); ?> value='fixed'>
    <label for="fixed"><?php echo __( 'Fixed', 'woo-first-discount' ); ?></label>
    <br>
    <input id="percent" type='radio' name='first_order_add_settings[first_order_choose]' <?php checked( $options['first_order_choose'], 'percent' ); ?> value='percent'>
    <label for="percent"><?php echo __( 'Percentage', 'woo-first-discount' ); ?></label>
    <?php
}


function loc_first_order_limit(  ) { 
    $options = get_option( 'first_order_add_settings' );
    ?>
    <input type='number' min="0" name='first_order_add_settings[first_order_add_limit]' value='<?php echo $options['first_order_add_limit']; ?>'>
    <?php
}

function loc_first_order_number(  ) { 
    $options = get_option( 'first_order_add_settings' );
    ?>
    <input type='number' min="0" name='first_order_add_settings[first_order_add_value]' value='<?php echo $options['first_order_add_value']; ?>'>
    <?php
}

function first_order_add_options_page(  ) { 
    ?>
    <form action='options.php' method='post'>
        <h2><?php echo __( 'First Order Discount', 'woo-first-discount' ); ?></h2>
        <?php
        settings_fields( 'pluginPage' );
        do_settings_sections( 'pluginPage' );
        submit_button();
        ?>
    </form>
    <?php
}

function loc_first_order_discount() {
    global $wpdb, $woocommerce;
    if ( is_user_logged_in() ) {
        $customer_id = get_current_user_id();
        $orderNumCheck = wc_get_customer_order_count( $customer_id );
        $options = get_option( 'first_order_add_settings' );
        $discountType = $options['first_order_choose'];
        $discountValue = $options['first_order_add_value'];
        $discountLimit = $options['first_order_add_limit'];

        if ($orderNumCheck == 0 and $discountType != 'off') {
            $subtotal = WC()->cart->cart_contents_total;
            if ($discountType == 'fixed') {
                WC()->cart->add_fee( 'First Time Order Discount ', -$discountValue );
            } else {
                if ( $discount = $discountValue/100 >15) return $discountLimit;
                WC()->cart->add_fee( 'First Time Order Discount ', -$subtotal*$discount );
            }
        } else {
            WC()->cart->add_fee( 'First Time Order Discount ', 0 );
        }
    }
}

add_action( 'woocommerce_cart_calculate_fees', 'loc_first_order_discount' );

add_filter( 'woocommerce_checkout_login_message', 'loc_return_customer_message' );
function loc_return_customer_message() {
return '<b>Have you shopped with us before?</b><br>Did you know that all first time orders automatically receives a 20% discount with a maximum value of 15 dollars?<br>';
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我重新访问了所有功能代码,因为它有点过时了。试试这个:

add_action( 'woocommerce_cart_calculate_fees', 'loc_first_order_discount', 20, 1 );
function loc_first_order_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( ! is_user_logged_in() )
        return; // Only for logged in users

    $customer_id   = get_current_user_id();
    $orderNumCheck = wc_get_customer_order_count( $customer_id );
    $options       = get_option( 'first_order_add_settings' );
    $discountType  = $options['first_order_choose'];

    if ( $orderNumCheck > 0 || $discountType == 'off' )
        return; // Exit

    $discountValue = $options['first_order_add_value'];
    $discountLimit = $options['first_order_add_limit'];
    $subtotal      = $cart->cart_contents_total;
    $discount_text = __('First Time Order Discount ', 'woocommerce');
    $discount      = 0;


    if ($discountType == 'fixed') {
        $discount = $discountValue;
    } else {
        $discount = $subtotal / 0.20; // 20%

        if( $discount > $discountLimit )
            $discount = $discountLimit;
    }

    if( $discount > 0 )
        $cart->add_fee( $discount_text, -$discount );
}

代码放在活动子主题(或活动主题)的function.php文件中。它应该按预期工作......