Php 在Woocommerce中启用计算的购物车折扣限额

Php 在Woocommerce中启用计算的购物车折扣限额,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,我正在尝试在不使用优惠券的情况下建立一个WooCommerce折扣,除了“限制”功能外,其他功能都在工作 每位新客户在购买时将获得20%的折扣(首次购物者),但这20%不能也不应超过15美元 这是一个插件代码,我需要帮助做的是如何“告诉它”,如果购物车折扣金额高于15,那么将其删除并放入15 这是不起作用的: if($discount=$discountValue/100>15)返回$discountLimit 完整代码: function loc_first_order_add_adm

我正在尝试在不使用优惠券的情况下建立一个WooCommerce折扣,除了“限制”功能外,其他功能都在工作

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

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

这是不起作用的:
if($discount=$discountValue/100>15)返回$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>';
}
函数loc\u first\u order\u add\u admin\u menu(){
添加子菜单页面(“woocommerce”、“第一订单折扣”、“第一订单折扣”、“管理选项”、“woocommerce\u第一订单折扣”、“第一订单添加选项页面”);
}
函数loc\u first\u order\u add\u settings\u init(){
注册设置('pluginPage','first\u order\u add\u settings');
添加设置部分(“第一次订单添加插件页面部分”)、“(这将为所有首次客户提供您选择的折扣,无需使用优惠券。”、“woo first折扣”)、“(插件页面”);
添加设置字段('first_order_choose'、'Type'、'woo first折扣')、'loc_first order_options'、'pluginPage'、'first_order_add_pluginPage_section');
添加设置字段('first_order_add_value'、'value'、'woo first折扣')、'loc_first order_number'、'pluginPage'、'first_order_add_pluginPage_section');
添加设置字段('first_order_add_limit'、'limit'、'woo first折扣')、'loc_first order_limit'、'pluginPage'、'first_order_add_pluginPage_section');
}
函数loc_first_order_options(){
$options=获取选项(“第一顺序添加设置”);
?>



我重新查看了您的所有函数代码,因为它有点过时。请尝试以下操作:

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文件中。现在应该可以正常工作了