基于 WooCommerce 产品自定义输入字段的折扣

Discount based on WooCommerce product custom input fields

我正在尝试根据单个产品页面中的数量字段为用户提供折扣。

基本上,该网站出售门票,成人和 children 的价格不同。所以我在单个产品页面中创建了输入字段,这样用户就可以输入有多少成年人以及他要购买多少children。

在产品管理员中,我有一个 "children discount" 的 ACF(高级自定义字段),所以在购物车中,我想根据 children 的数量给予折扣。例如,假设用户要为该产品购买 5 张票,成人票 3 张,children 票 2 张,我想计算这 2 张票的折扣 children。

到目前为止我尝试过的:

add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );

function custom_product_price_field(){

    echo '<div class="custom-text text">
              <p>Quantity of adults:</p>
              <input type="text" name="qtty_adults" value="" title="Quantity Adults" class="qtty-field">
          </div>
          <div class="custom-text text">
              <p>Quantity of children:</p>
              <input type="text" name="qtty_kids" value="" title="Quantity Kids" class="qtty-field">
          </div>';

}


add_action('woocommerce_cart_calculate_fees' , 'add_user_discounts');

function add_user_discounts( WC_Cart $cart ){

    global $product;

    $qtty_kids = (float) sanitize_text_field( $_POST['qtty_kids'] );
    $discount_per_kid = (float) get_field('children_discount', $product->id);


    $discount = $qtty_kids * $discount_per_kid;

    $cart->add_fee( 'Discount for children', -$discount);
}

这样做折扣总是 $0

任何人都可以帮助我实现这一目标吗?

您的代码有点过时,有一些错误,并且缺少很多东西才能达到您的期望……

尝试以下方法 (更新)

add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );
function custom_product_price_field(){

    echo '<div class="custom-text text">
        <p>'.__("Quantity of adults:").'</p>
        <input type="text" name="qtty_adults" value="" title="'.__("Quantity Adults").'" class="qtty-field">
    </div>
    <div class="custom-text text">
        <p>'.__("Quantity of children:").'</p>
        <input type="text" name="qtty_kids" value="" title="'.__("Quantity Kids").'" class="qtty-field">
    </div>';
}

// Add selected add-on option as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data_callback', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id ) {
    if ( isset( $_POST['qtty_kids'] )  && $children_discount = get_field( 'children_discount', $product_id ) ) {
        $cart_item_data['children_discount'] = (float) $children_discount - (float) sanitize_text_field( $_POST['qtty_kids'] );
        $cart_item_data['unique_key']        = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

// Set a discount based a product custom field(s)
add_action('woocommerce_cart_calculate_fees' , 'add_children_discount', 10, 1 );
function add_children_discount( $cart ){
    if ( is_admin() && ! defined('DOING_AJAX') )
        return;

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

    $discount = 0; // Initialising

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset( $cart_item['children_discount'] ) ) {
            $discount += $cart_item['children_discount'];
        }
    }

    if ( $discount > 0 )
        $cart->add_fee( __("Discount for children", "woocommerce"), -$discount );
}

已测试并有效。


相关: