Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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_Wordpress_Woocommerce_Cart_Discount - Fatal编程技术网

Php 根据Woocommerce中输入的邮政编码获取动态计算的费用

Php 根据Woocommerce中输入的邮政编码获取动态计算的费用,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,我试图在woocommerce\u cart\u calculate\u feeshook中传递一个变量,但此代码不起作用。当我尝试在变量中传递静态值时,它会起作用 这是我的密码: add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1); function add_custom_fees( WC_Cart $cart){ $final_discount; // storing ajax value

我试图在
woocommerce\u cart\u calculate\u fees
hook中传递一个变量,但此代码不起作用。当我尝试在变量中传递静态值时,它会起作用

这是我的密码:

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1);
function add_custom_fees( WC_Cart $cart){

    $final_discount; // storing ajax value
    $static_value = 5; // static value

    if ($final_discount) {
       /**
        * custom discount for the cart in woocommerce
        */
        // Hook before calculate fees
        add_action('woocommerce_cart_calculate_fees', 'add_custom_fees');

        /**
         * Add custom fee if more than three article
         * @param WC_Cart $cart
         */
        function add_custom_fees(WC_Cart $cart) {
            // Calculate the amount to reduce
            global $final_discount;
            $discount = $cart->subtotal * $final_discount / 100;
            $cart->add_fee('10% discount has been added.', -$discount);
        }
    }
}

编辑:

现在我要在
woocommerce\u cart\u calculate\u fees
hook中传递一个
WC\u Session
值,代码可以工作,但Session变量在页面刷新之前不会更新

会话变量正在存储woocommerce签出页面的
billing\u postcode
字段的onchange ajax值

我的jQuery代码(Ajax):

我的主题的functions.PHP文件中的PHP代码:

wp_enqueue_script('zip_code', get_template_directory_uri() . '/assets/js/zipcode.js', array('jquery'));
wp_localize_script('zip_code', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));

add_action('wp_ajax_woocommerce_apply_state', 'discount', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'discount', 10);

function discount() {
    if(isset($_POST['billing_postcode'])){
        $billing_postcode = isset($_POST['billing_postcode'])?$_POST['billing_postcode']:'not yet';
        global $wpdb;
        $zipcodes = $wpdb->get_results( 
            $wpdb->prepare( "
                SELECT * FROM wp_zip_codes_value 
                WHERE zip_code_value = %d", 
                $billing_postcode
            ) 
        );

        $zip_for_discount = array();
        foreach ( $zipcodes as $zipcode ){
            $zip_for_discount = $zipcode->zip_code_id;
        }

        $find_discount = $wpdb->get_results( 
            $wpdb->prepare( "
                SELECT * FROM wp_zip_codes 
                WHERE zip_code = %d", 
                $zip_for_discount
            ) 
        );

        $final_discount = array();
        if($find_discount){
            foreach ( $find_discount as $discount ){
                $final_discount[] = $discount->discount;
            }
        }
        $final_discount[0];
        WC()->session->set( 'final_discount', $final_discount[0] );
    }
}

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees', 20, 1);

function add_custom_fees( WC_Cart $cart){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $percent = WC()->session->get( 'final_discount' );
    if( $percent > 0 ){
        $discount = $cart->subtotal * $percent / 100;
        $cart->add_fee( __('Zip Code Discount', 'woocommerce' ) . " ($percent%)", -$discount);
    }
}

实现此功能的最佳方法是在之前的另一个函数中使用
WC\u Sessions
设置您的
$final\u折扣
变量,方法如下:

WC()->session->set( 'final_discount', $final_discount );
现在,您可以使用以下方法在前端挂钩或代码的任何位置获取此值:

$final_discount = WC()->session->get( 'final_discount' );
因此,您的最终折扣(负费用)代码如下:

add_action('woocommerce_cart_calculate_fees', 'add_custom_fee', 20, 1 );
function add_custom_fee( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Get the data from WC_Sessions
    $percent = WC()->session->get( 'final_discount' );
    
    if( $percent > 0 ){
        $discount = $cart->subtotal * $percent / 100;
        $cart->add_fee( __('Discount', 'woocommerce' ) . " ($percent%)" . $percent, -$discount);
    }
}
代码进入活动子主题(活动主题)的function.php文件

测试和工作


关于更新问题的更新:

这里是一个与您的类似但简化的Ajax示例代码(因为我没有类似的数据库自定义表来获取折扣百分比)

可变折扣百分比基于估算的邮政编码值

PHP代码:

add_action( 'wp_enqueue_scripts', 'wc_zipcode_enqueue_scripts' );
function wc_zipcode_enqueue_scripts() {

    // Only on front-end and checkout page
    if( is_admin() || ! is_checkout() ) return;

    // (For child themes use get_stylesheet_directory_uri() instead)
    wp_enqueue_script('zip_code', get_template_directory_uri() . '/assets/js/zipcode.js', array('jquery'));
    wp_localize_script('zip_code', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));
}

add_action('wp_ajax_woocommerce_apply_state', 'woocommerce_apply_state', 10 );
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'woocommerce_apply_state', 10 );
function woocommerce_apply_state() {
    global $wpdb;

    if( isset($_POST['billing_postcode']) ){
        $billing_postcode = $_POST['billing_postcode'];

        if( empty($billing_postcode) || $billing_postcode == 0 ) die();

        if( $billing_postcode < 30000 )
            $final_discount = 10;
        elseif( $billing_postcode >= 30000 && $billing_postcode < 60000 )
            $final_discount = 15;
        else
            $final_discount = 20;

        WC()->session->set( 'final_discount', $final_discount );
        echo json_encode( WC()->session->get('final_discount' ) );
    }
    die(); // Alway at the end (to avoid server error 500)
}

add_action('woocommerce_cart_calculate_fees' , 'add_custom_discount', 20, 1);
function add_custom_discount( WC_Cart $cart){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Get the data from WC_Sessions
    $percent = WC()->session->get( 'final_discount' );

    // If the billing postcode is not set we exit
    $billing_postcode = WC()->session->get('customer')['postcode'];
    if( empty($billing_postcode) ) return;

    if( $percent > 0 ){
        $discount = $cart->subtotal * $percent / 100;
        $cart->add_fee( __('Zip Code Discount', 'woocommerce' ) . " ($percent%)", -$discount);
    }
}
保存在主题文件夹
/assets/js/zipcode.js
中名为
zipcode.js的文件中


经过测试,工作正常(谢谢)您的代码运行良好,但有一点问题。页面刷新前折扣值不更新,页面刷新后显示折扣值。会取消会话吗?怎么去哪里?好的,谢谢。现在我用详细代码编辑了我的问题。请检查一下好吗?@rafiqislam在我的原始答案后更新…@LoicTheAztec这个答案非常有用+1.
add_action( 'wp_enqueue_scripts', 'wc_zipcode_enqueue_scripts' );
function wc_zipcode_enqueue_scripts() {

    // Only on front-end and checkout page
    if( is_admin() || ! is_checkout() ) return;

    // (For child themes use get_stylesheet_directory_uri() instead)
    wp_enqueue_script('zip_code', get_template_directory_uri() . '/assets/js/zipcode.js', array('jquery'));
    wp_localize_script('zip_code', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));
}

add_action('wp_ajax_woocommerce_apply_state', 'woocommerce_apply_state', 10 );
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'woocommerce_apply_state', 10 );
function woocommerce_apply_state() {
    global $wpdb;

    if( isset($_POST['billing_postcode']) ){
        $billing_postcode = $_POST['billing_postcode'];

        if( empty($billing_postcode) || $billing_postcode == 0 ) die();

        if( $billing_postcode < 30000 )
            $final_discount = 10;
        elseif( $billing_postcode >= 30000 && $billing_postcode < 60000 )
            $final_discount = 15;
        else
            $final_discount = 20;

        WC()->session->set( 'final_discount', $final_discount );
        echo json_encode( WC()->session->get('final_discount' ) );
    }
    die(); // Alway at the end (to avoid server error 500)
}

add_action('woocommerce_cart_calculate_fees' , 'add_custom_discount', 20, 1);
function add_custom_discount( WC_Cart $cart){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Get the data from WC_Sessions
    $percent = WC()->session->get( 'final_discount' );

    // If the billing postcode is not set we exit
    $billing_postcode = WC()->session->get('customer')['postcode'];
    if( empty($billing_postcode) ) return;

    if( $percent > 0 ){
        $discount = $cart->subtotal * $percent / 100;
        $cart->add_fee( __('Zip Code Discount', 'woocommerce' ) . " ($percent%)", -$discount);
    }
}