猿问

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

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


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


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


到目前为止我已经尝试过:


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


谁能给我一些关于如何实现这一点的帮助?


拉丁的传说
浏览 59回答 1
1回答

呼如林

你的代码有点过时了,有一些错误,还有很多遗漏的东西来得到你所期望的……请尝试以下操作(更新):add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );function custom_product_price_field(){&nbsp; &nbsp; echo '<div class="custom-text text">&nbsp; &nbsp; &nbsp; &nbsp; <p>'.__("Quantity of adults:").'</p>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="qtty_adults" value="" title="'.__("Quantity Adults").'" class="qtty-field">&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="custom-text text">&nbsp; &nbsp; &nbsp; &nbsp; <p>'.__("Quantity of children:").'</p>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="qtty_kids" value="" title="'.__("Quantity Kids").'" class="qtty-field">&nbsp; &nbsp; </div>';}// Add selected add-on option as custom cart item dataadd_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 ) {&nbsp; &nbsp; if ( isset( $_POST['qtty_kids'] )&nbsp; && $children_discount = get_field( 'children_discount', $product_id ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart_item_data['children_discount'] = (float) $children_discount - (float) sanitize_text_field( $_POST['qtty_kids'] );&nbsp; &nbsp; &nbsp; &nbsp; $cart_item_data['unique_key']&nbsp; &nbsp; &nbsp; &nbsp; = md5( microtime().rand() ); // Make each item unique&nbsp; &nbsp; }&nbsp; &nbsp; 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 ){&nbsp; &nbsp; if ( is_admin() && ! defined('DOING_AJAX') )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; if ( did_action('woocommerce_cart_calculate_fees') >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $discount = 0; // Initialising&nbsp; &nbsp; // Loop through cart items&nbsp; &nbsp; foreach ( $cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; if( isset( $cart_item['children_discount'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $discount += $cart_item['children_discount'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if ( $discount > 0 )&nbsp; &nbsp; &nbsp; &nbsp; $cart->add_fee( __("Discount for children", "woocommerce"), -$discount );}测试和工作。
随时随地看视频慕课网APP
我要回答