首页 > 解决方案 > 如何使用 ajax 更改结帐端点 /order-price/woocommerce 中的价格?

问题描述

我将在结帐/订单支付/页面为客户实施“存款支付”。我遵循下面的代码,但它似乎无法正常工作并且没有使用 ajax 更新价格。我感谢任何可以帮助我的人。提前致谢。

挂钩

public function __construct()
        {
            // Redux option
            add_filter('redux/options/electro_ons/sections', array($this, 'add_sec'), 99, 1);

            // add price to product category
            add_action('product_cat_add_form_fields', array($this, 'wh_taxonomy_add_new_meta_field'), 10, 1);
            add_action('product_cat_edit_form_fields', array($this, 'wh_taxonomy_edit_meta_field'), 10, 1);
            add_action('edited_product_cat', array($this, 'wh_save_taxonomy_custom_meta'), 10, 1);
            add_action('create_product_cat', array($this, 'wh_save_taxonomy_custom_meta'), 10, 1);
            add_action('wp_footer', array($this, 'ajax_script_deposit'), 10, 1);

            //  change template for customize
            add_filter('woocommerce_locate_template', array(&$this, 'customize_theme_checkout_page'), 9999, 3);

            // ajax
            add_action( 'wp_ajax_deposit', array($this, 'pay_deposit' ));
            add_action( 'wp_ajax_nopriv_deposit', array($this, 'pay_deposit' ));



            add_action('woocommerce_cart_calculate_fees', array($this, 'custom_percetage_fee'), 100, 1);
        }


阿贾克斯


function ajax_script_deposit()
        {
            // Only on Checkout
            if (self::opt('deposit_status')) :

                WC()->session->__isset('enable_deposit');


                ?>

                <script type="text/javascript">

                    jQuery(function ($) {

                        $('.deposit-pointer').on('click', () => {

                            $('.deposit-text').slideToggle();

                        });

                        btn_pay = $('#pay_deposit');

                        if (typeof wc_add_to_cart_params === 'undefined')
                            return false;

                        btn_pay.on('click', function (e) {

                            // var deposit = $(this).prop('checked') === true ? '1' : '';
                            $.ajax({
                                type: 'POST',
                                url: wc_add_to_cart_params.ajax_url,
                                data: {
                                    'action': 'deposit',
                                    'type':$('#deposit_mount').data('id') ,
                                    'mount':$('#deposit_mount').data('mount'),
                                    'time':Date.now()
                                }
                            }).done(function (data) {
                                $('body').trigger('update_checkout');
                            });
                        });
                    });
                </script>

            <?php
            endif;
        }

Ajax 处理程序 - 我还需要在发票中查看押金和交货付款。

function pay_deposit($cart){
            // Only on checkout
            global $pay_delivery;
            $pay_delivery = $_SESSION['pay_delivery'] ?? '';

            if ((is_admin() && !defined('DOING_AJAX')) || !is_checkout() ){
                return;
            }

            global $type;
            global $mount;

            $type  =  $_POST['type']  ?? '';
            $mount =  $_POST['mount'] ?? '';
            $time  =  $_POST['time']  ?? '';


            $percent = self::opt('percent_price_deposit_category')??'';
            $fix     = self::opt('fix_price_deposit_category')?? '';


            switch ($type) {
                case 'percent':

                    $deposit_percent = (WC()->cart->get_subtotal() * intval($percent)) / 100;

                    if ($deposit_percent == intval($mount)) {

                        session_start();

                        WC()->session->set('pay_delivery', $deposit_percent);

                        $pay_delivery = $deposit_percent;

                    }
                    break;

                case 'fix':

                    $deposit_fix = WC()->cart->get_subtotal() - intval($fix);

                    session_start();

                    WC()->session->set('pay_delivery', $deposit_fix);

                    $pay_delivery  = $deposit_fix;

                    break;
            }

            die();
        }

function custom_percetage_fee($cart)
        {

            if ((is_admin() && !defined('DOING_AJAX')) || !is_checkout())
                return;

            session_start();
            $delivery = $_SESSION['pay_delivery']??'' ;




            if (WC()->session->get('pay_delivery')&& $delivery > 0 ){


                WC()->cart->add_fee('Delivery Pay', ($delivery * -1), false);

            }


        }

标签: phpajaxwordpresswoocommerce

解决方案


推荐阅读