Wordpress 试图删除表单字段,但控制台日志中出现空错误

Wordpress 试图删除表单字段,但控制台日志中出现空错误,wordpress,woocommerce,Wordpress,Woocommerce,我试图通过ajax将Woocommerce表单字段值传递到WC\u会话 部分基于“教程和”回答线程,以下是代码尝试的摘录: if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { function my_custom_shipping_method() { if ( ! class_exists( '

我试图通过ajax将Woocommerce表单字段值传递到
WC\u会话

部分基于“教程和”回答线程,以下是代码尝试的摘录:

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

function my_custom_shipping_method() {
        if ( ! class_exists( 'MY_Custome_Shipping_Method' ) ) {
            class my_custom_Shipping_Method extends WC_Shipping_Method {

        public function __construct() {
                    $this->id                 = 'my_custome_shipping_method';  
                    $this->method_title       = __( 'My Customize Shipping','my_custome_shipping_method' );  
                    $this->method_description = __( 'Custom Shipping Method for my custome shipping method', 'my_custome_shipping_method' );
.......
}
.......
//other shipping method code skipped
.......

public function calculate_shipping( $package = array() ) {
.......
$string = json_encode($result);
$decodedArray = json_decode($result,true);
$kekei = $decodedArray['msg'];
$balbal = array();
foreach($kekei as $goods=> $g_values) { 
    $balbal[$g_key['shipping_code'] ] = $g_values['shipping_name'];;              
}

$janggal = (array)$balbal;
WC()->session->set('janel_seesion', $balbal);
.......
}
}
}
}
}               

 add_action( 'woocommerce_shipping_init', 'my_custome_shipping_method' );

    function add_mycustome_shipping_method( $methods ) {
        $methods[] = 'My_Custome_Shipping_Method';
        return $methods;
    }

    add_filter( 'woocommerce_shipping_methods', 'add_mycustome_shipping_method' );


// Enabling delivery options for a specific defined shipping method
function targeted_shipping_method(){
    // HERE below define the shipping method Id that enable the custom delivery options
    //return 'local_pickup:5';
    return 'my_custome_shipping_method';
}

// Customizing Woocommerce checkout radio form field
add_action( 'woocommerce_form_field_radio', 'custom_form_field_radio', 20, 4 );
function custom_form_field_radio( $field, $key, $args, $value ) {
    if ( ! empty( $args['options'] ) && is_checkout() ) {
        $field = str_replace( '</label><input ', '</label><br><input ', $field );
        $field = str_replace( '<label ', '<label style="display:inline;margin-left:8px;" ', $field );
    }
    return $field;
}

add_action( 'woocommerce_review_order_after_shipping', 'custom_shipping_radio_button', 20);

function custom_shipping_radio_button() {

$domain = 'woocommerce';

if (  WC()->session->get( 'chosen_shipping_methods' )[0] == targeted_shipping_method() ) :


echo '<tr class="delivery-radio"><th>' . __('Delivery options', $domain) . '</th><td>';
$hariharikarl = WC()->session->get( 'janel_seesion' );
$chosen = WC()->session->get('chosen_delivery');
$chosen = empty($chosen) ? WC()->checkout->get_value('delivery') : $chosen;
$chosen = empty($chosen) ? 'regular' : $chosen;

// Add a custom checkbox field
woocommerce_form_field( 'radio_delivery', array(
    'type' => 'radio',
    'class' => array( 'form-row-wide' ),
    /*
    'options' => array(
    'regular' => __('Regular', $domain),
    'premium' => __('Premium +'.wc_price(2.00), $domain),
    'big' => __('Big +'.wc_price(3.00), $domain),
    'small' => __('Big +'.wc_price(5.00), $domain),

    */
    'options' => $hariharikari,
    'default' => $chosen,
), $chosen );

echo '</td></tr>';

endif;
}


// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_delivery_script' );
function checkout_delivery_script() {
    // Only checkout page
    if ( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        $('form.checkout').on('change', 'input[name=radio_delivery]', function(e){
            e.preventDefault();
            var d = $(this).val();
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'delivery',
                    'delivery': d,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    console.log(result); // just for testing | TO BE REMOVED
                },
                error: function(error){
                    console.log(error); // just for testing | TO BE REMOVED
                }
            });
        });
    });
    </script>
    <?php

}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_delivery', 'wc_get_delivery_ajax_data' );
add_action( 'wp_ajax_nopriv_delivery', 'wc_get_delivery_ajax_data' );
function wc_get_delivery_ajax_data() {
    if ( isset($_POST['delivery']) ){
        WC()->session->set('chosen_delivery', sanitize_key( $_POST['delivery'] ) );
        echo json_encode( $delivery ); // Return the value to jQuery
    }
    die();
}

// Add a custom dynamic delivery fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {

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

    // Only for targeted shipping method
    if (  WC()->session->get( 'chosen_shipping_methods' )[0] != targeted_shipping_method() )
        return;

    if( WC()->session->get( 'chosen_delivery' ) == 'regular' )
        $cart->add_fee( __( 'Delivery fee', 'woocommerce' ), 2.00 );

    if( WC()->session->get( 'chosen_delivery' ) == 'big' )
        $cart->add_fee( __( 'Delivery fee', 'woocommerce' ), 3.00 );



}
if(在_数组('woocommerce/woocommerce.php',应用_过滤器('active_plugins',获取_选项('active_plugins'))){
函数my\u custom\u shipping\u method(){
如果(!class_存在('MY_Custome_Shipping_Method')){
类my_custom_Shipping_方法扩展了WC_Shipping_方法{
公共函数构造(){
$this->id='my_custome_shipping_method';
$this->method_title=uuuuuuu('My Customize Shipping'、'My custome_Shipping_method');
$this->method\u description=\uuuuuuu('custome Shipping method for my custome Shipping method','my\u custome\u Shipping\u method');
.......
}
.......
//已跳过其他装运方法代码
.......
公共函数calculate\u shipping($package=array()){
.......
$string=json_encode($result);
$decodedArray=json_decode($result,true);
$kekei=$decodedArray['msg'];
$balbal=array();
foreach($kekei作为$goods=>$g_值){
$balbal[$g_key['shipping_code']]=$g_值['shipping_name'];;
}
$janggal=(数组)$balbal;
WC()->session->set('janel\u seision',$balbal);
.......
}
}
}
}
}               
添加操作(“woocommerce\u shipping\u init”、“my\u custome\u shipping\u method”);
函数add\u mycustome\u shipping\u方法($methods){
$methods[]=“我的客户运输方法”;
返回$methods;
}
添加过滤器(“woocommerce\u shipping\u methods”、“add\u mycustome\u shipping\u methods”);
//为特定定义的装运方法启用交付选项
目标函数\u装运\u方法(){
//下面定义启用自定义交付选项的装运方法Id
//返回“本地取货:5”;
返回“我的客户发货方式”;
}
//自定义电子商务签出单选表单字段
添加操作('woocommerce\u form\u field\u radio','custom\u form\u field\u radio',20,4);
函数自定义\表单\字段\收音机($field、$key、$args、$value){
if(!empty($args['options'])和&is_checkout(){

$field=str_replace('我已经编辑并最小化了上面的代码,以便您从一开始就可以看到代码。我已经在“public function calculate shipping”中测试了生成数组的API代码在我的XAMP服务器的另一个部分,它工作了。在这个问题中,您同时要问很多问题…您不应该在shipping method类中使用WC_会话或$GLOBAL。因此,首先您应该解决与自定义shipping method类相关的所有问题(一个接一个)。解释您试图执行的操作…始终使用明确可读的变量名并注释您的代码…您实际的问题太广泛了。请参阅
calculate\u shipping()
函数中的示例,
$kekekei=$decodedArray['msg']
不会给出任何信息,因为自定义传送方法代码中未定义
$decodedArray
。因此,您的代码不可测试。请从头开始,正确地构建自定义传送方法,一步一步地解释问题(再次使用明确易读的内容重命名变量名)。如果您不这样做,您将不会得到任何有用的答案。实际上,您的问题不清楚,不可测试,范围太广。在自定义装运方法代码中定义$decodedArray的位置。
公共函数calculate_shipping()中的代码
可以通过每个按钮上的描述触发阵列所有单选按钮,并且可以看到,但不能将该值作为控制台日志触发错误消息
null