【问题标题】:Enable a calculated cart discount limit in Woocommerce在 Woocommerce 中启用计算的购物车折扣限制
【发布时间】:2018-10-11 10:36:58
【问题描述】:

我正在尝试在不使用优惠券的情况下组合 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>';
}

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案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 文件中。它现在应该可以按预期工作了……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多