Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce中设置折扣的签出输入文本字段_Php_Jquery_Ajax_Wordpress_Woocommerce - Fatal编程技术网

Php 在WooCommerce中设置折扣的签出输入文本字段

Php 在WooCommerce中设置折扣的签出输入文本字段,php,jquery,ajax,wordpress,woocommerce,Php,Jquery,Ajax,Wordpress,Woocommerce,我正在使用应答码设置折扣(负费用),这在我的商店中非常有效 但我想使用输入字段设置费用金额。我的目标是通过num/text输入字段中输入的值来减少购买总量。我不想使用复选框,我想使用输入或数字来确定费用金额 可能吗?如何执行此操作?要启用在WooCommerce中设置自定义折扣的签出输入文本字段,只需进行以下几项更改: // Display an input text field after billing fields add_action( 'woocommerce_after_checko

我正在使用应答码设置折扣(负费用),这在我的商店中非常有效

但我想使用输入字段设置费用金额。我的目标是通过num/text输入字段中输入的值来减少购买总量。我不想使用复选框,我想使用输入或数字来确定费用金额


可能吗?如何执行此操作?

要启用在WooCommerce中设置自定义折扣的签出输入文本字段,只需进行以下几项更改:

// Display an input text field after billing fields
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_checkout_text_field', 20 );
function add_custom_checkout_text_field(){

    woocommerce_form_field( 'custom_discount', array(
        'type'  => 'text',
        'label' => __('Add a discount amount'),
        'class' => array( 'form-row-wide' ),
    ), WC()->session->get('custom_discount') );
}

// Remove "(optional)" label on checkbox field
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // Only on checkout page for Order notes field
    if( 'custom_discount' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// Ajax / jQuery script
add_action( 'wp_footer', 'custom_discount_script' );
function custom_discount_script() {
    // On checkoutpage
    if( ( is_checkout() && ! is_wc_endpoint_url() ) ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof woocommerce_params === 'undefined')
            return false;

        $(document.body).on('input change', 'input[name=custom_discount]', function(){
            $.ajax({
                type: 'POST',
                url: woocommerce_params.ajax_url,
                data: {
                    'action': 'custom_discount',
                    'custom_discount': $(this).val(),
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    console.log(result);
                },
            });
        });
    });
    </script>
    <?php
    endif;
}

// Get the ajax request and set value to WC session
add_action( 'wp_ajax_custom_discount', 'get_ajax_custom_discount' );
add_action( 'wp_ajax_nopriv_custom_discount', 'get_ajax_custom_discount' );
function get_ajax_custom_discount() {
    if ( isset($_POST['custom_discount']) ) {
        $discount = $_POST['custom_discount'] ? floatval($_POST['custom_discount']) : '';
        WC()->session->set('custom_discount', $discount );
        // echo WC()->session->get('custom_discount');
    }
    die();
}

// Add / Remove a custom discount
add_action( 'woocommerce_cart_calculate_fees', 'add_remove_custom_discount', 10, 1 );
function add_remove_custom_discount( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || is_cart() )
        return;

    $discount = (float) WC()->session->get('custom_discount');

    if( $discount > 0 ) {
        $cart->add_fee( __( 'Custom Discount', 'woocommerce'), -$discount );
    }
}

// Reset WC Session custom discount variable
add_action( 'woocommerce_checkout_order_created', 'reset_wc_session_custom_discount_variable' );
function reset_wc_session_custom_discount_variable( $order ) {
    if ( WC()->session->__isset('custom_discount') ) {
        WC()->session->__unset('custom_discount');
    }
}
//在帐单字段之后显示输入文本字段
添加操作('woocommerce\u后加结帐单','add\u custom\u checkout\u text\u field',20);
函数添加\自定义\签出\文本\字段(){
woocommerce\u表单\u字段('自定义\u折扣',数组(
'类型'=>'文本',
“标签”=>“‘添加折扣金额’”,
'class'=>数组('form row wide'),
),WC()->session->get('custom_折扣');
}
//删除复选框字段上的“(可选)”标签
添加过滤器('woocommerce\u form\u field','remove\u order\u comments\u optional\u fields\u label',10,4);
函数删除\顺序\注释\可选\字段\标签($field,$key,$args,$value){
//仅在订单备注字段的签出页上
if('custom_discount'==$key&&is_checkout()){
$optional='('.esc_html_uuuuuuuu('optional','woocommerce'));
$field=str_replace($optional,,$field);
}
返回$field;
}
//Ajax/jQuery脚本
添加操作('wp_footer','custom_折扣脚本');
函数自定义\折扣\脚本(){
//论支票支付
如果((is_checkout()&&!is_wc_endpoint_url()):
?>
jQuery(函数($){
如果(参数的类型=='undefined')
返回false;
$(document.body).on('input change','input[name=custom_折扣]',function(){
$.ajax({
键入:“POST”,
url:woocommerce_params.ajax_url,
数据:{
“操作”:“自定义折扣”,
“自定义折扣”:$(this.val(),
},
成功:功能(结果){
$('body')。触发器('update_checkout');
控制台日志(结果);
},
});
});
});

要启用在WooCommerce中设置自定义折扣的签出输入文本字段,只需进行以下几项更改:

// Display an input text field after billing fields
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_checkout_text_field', 20 );
function add_custom_checkout_text_field(){

    woocommerce_form_field( 'custom_discount', array(
        'type'  => 'text',
        'label' => __('Add a discount amount'),
        'class' => array( 'form-row-wide' ),
    ), WC()->session->get('custom_discount') );
}

// Remove "(optional)" label on checkbox field
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // Only on checkout page for Order notes field
    if( 'custom_discount' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// Ajax / jQuery script
add_action( 'wp_footer', 'custom_discount_script' );
function custom_discount_script() {
    // On checkoutpage
    if( ( is_checkout() && ! is_wc_endpoint_url() ) ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof woocommerce_params === 'undefined')
            return false;

        $(document.body).on('input change', 'input[name=custom_discount]', function(){
            $.ajax({
                type: 'POST',
                url: woocommerce_params.ajax_url,
                data: {
                    'action': 'custom_discount',
                    'custom_discount': $(this).val(),
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    console.log(result);
                },
            });
        });
    });
    </script>
    <?php
    endif;
}

// Get the ajax request and set value to WC session
add_action( 'wp_ajax_custom_discount', 'get_ajax_custom_discount' );
add_action( 'wp_ajax_nopriv_custom_discount', 'get_ajax_custom_discount' );
function get_ajax_custom_discount() {
    if ( isset($_POST['custom_discount']) ) {
        $discount = $_POST['custom_discount'] ? floatval($_POST['custom_discount']) : '';
        WC()->session->set('custom_discount', $discount );
        // echo WC()->session->get('custom_discount');
    }
    die();
}

// Add / Remove a custom discount
add_action( 'woocommerce_cart_calculate_fees', 'add_remove_custom_discount', 10, 1 );
function add_remove_custom_discount( $cart ) {
    // Only on checkout
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || is_cart() )
        return;

    $discount = (float) WC()->session->get('custom_discount');

    if( $discount > 0 ) {
        $cart->add_fee( __( 'Custom Discount', 'woocommerce'), -$discount );
    }
}

// Reset WC Session custom discount variable
add_action( 'woocommerce_checkout_order_created', 'reset_wc_session_custom_discount_variable' );
function reset_wc_session_custom_discount_variable( $order ) {
    if ( WC()->session->__isset('custom_discount') ) {
        WC()->session->__unset('custom_discount');
    }
}
//在帐单字段之后显示输入文本字段
添加操作('woocommerce\u后加结帐单','add\u custom\u checkout\u text\u field',20);
函数添加\自定义\签出\文本\字段(){
woocommerce\u表单\u字段('自定义\u折扣',数组(
'类型'=>'文本',
“标签”=>“‘添加折扣金额’”,
'class'=>数组('form row wide'),
),WC()->session->get('custom_折扣');
}
//删除复选框字段上的“(可选)”标签
添加过滤器('woocommerce\u form\u field','remove\u order\u comments\u optional\u fields\u label',10,4);
函数删除\顺序\注释\可选\字段\标签($field,$key,$args,$value){
//仅在订单备注字段的签出页上
if('custom_discount'==$key&&is_checkout()){
$optional='('.esc_html_uuuuuuuu('optional','woocommerce'));
$field=str_replace($optional,,$field);
}
返回$field;
}
//Ajax/jQuery脚本
添加操作('wp_footer','custom_折扣脚本');
函数自定义\折扣\脚本(){
//论支票支付
如果((is_checkout()&&!is_wc_endpoint_url()):
?>
jQuery(函数($){
如果(参数的类型=='undefined')
返回false;
$(document.body).on('input change','input[name=custom_折扣]',function(){
$.ajax({
键入:“POST”,
url:woocommerce_params.ajax_url,
数据:{
“操作”:“自定义折扣”,
“自定义折扣”:$(this.val(),
},
成功:功能(结果){
$('body')。触发器('update_checkout');
控制台日志(结果);
},
});
});
});

很好,你对这项工作很在行。谢谢你的帮助。我很感谢你。是否可以在购物车页面中使用此功能?@MusaBaltacıNo仅在结帐时在购物车页面中不可能。在商店税务设置中,如果结帐页面上显示的价格似乎不含税,则在结帐页面上,当我键入100作为折扣金额时t、 虽然特别折扣栏上写着100,但它比税率增加了更多,折扣为117。我们可以不纳税或在优惠券应用程序中显示折扣金额吗?@LoicTheAztecPerfect,你是这项工作的高手。谢谢你的帮助。我很感谢你。是否可以在购物车页面中使用此功能?@MusaBaltacı在购物车页面中,仅在结帐时不可能。在商店税设置中,如果结帐页面上显示的价格似乎不含税,则在结帐页面上,当我键入100作为折扣金额时,虽然在特殊折扣行中显示100,但它会增加超过税率的折扣,即117。我们可以使用是否应纳税或与优惠券申请相同?@LoicTheAztec