这是一个针对woocommerce特定配送方式的插件。
我正在尝试将表单添加到我创建的WooCommerce的新发货方法中。在该表单中,我希望用户输入街道和邮政编码,然后按Ok按钮。单击应该从街道和邮政编码中提取值,然后根据这些值从DHL返回交货地点。
单击按钮时执行click事件(在javascript中)没有问题,但javascript随后会运行一个ajax调用,该调用会失败并返回400个坏网关。
我知道原因是这些行:
add_action( \'wp_ajax_checklocation\', array ( $this, \'location\') );
add_action( \'wp_ajax_nopriv_checklocation\', array ( $this, \'location\') );
因为某种原因没有注册,但我不知道为什么?代码是否位于错误的代码位置?或者我错过了什么?即使javascript中的ajax调用如下所示,也不会调用location函数:
var check_dhl_location = $.ajax({
type: \'POST\',
data:{
action: \'checklocation\',
dhlsweden_shipping_adr: shipping_adr,
dhlsweden_shipping_postalcode: shipping_postalcode
},
url: MyAjax.ajaxurl,
dataType: \'json\'
});
请帮助我提供线索或更好的解决方案:-)下面几乎是所有代码:
<?php
defined( \'ABSPATH\' ) or die( \'No access allowed!\' );
//Check if WooCommerce is active
if ( in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) ) {
function dhlsweden_shipping_method_init() {
if ( ! class_exists( \'WC_Shipping_DHLsweden\' ) ) {
class WC_Shipping_DHLsweden extends WC_Shipping_Method {
const client_key = \'I wont tell here!!!\'; //Client-key is genereated in DHL Dashboard
const base_url = \'https://api.freight-logistics.dhl.com/\';
const homedelivery_url = \'homedeliverylocatorapi/v1/\';
const ping = \'homedeliverylocator/ping\';
const servicepointlocator_url = \'servicepointlocatorapi/\';
const nearest_servicepoints = \'servicepoint/findnearestservicepoints\';
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct( $instance_id = 0 )
{
//Initate ajax-functionality both admin and frontend
add_action( \'wp_ajax_checklocation\', array ( $this, \'location\') );
add_action( \'wp_ajax_nopriv_checklocation\', array ( $this, \'location\') );
$this->load_js();
}
/* THIS function is NOT called */
public function location() {
$location = array();
/*
* $shipping_adr = filter_input( INPUT_POST, \'dhlsweden_shipping_adr\' );
$postal_code = filter_input( INPUT_POST , \'dhlsweden_shipping_postalcode\' );
*/
error_log($shipping_adr, 3, \'dhlsweden.log\');
error_log($postal_code, 3, \'dhlsweden.log\');
error_log(print_r($location,true), 3, \'dhlsweden.log\');
echo json_encode($location);
wp_die();
}
public function api_getlocations() {
$fieldset = WC()->checkout()->checkout_fields;
//var_dump($fieldset);
$shipping_adr = WC()->checkout()->get_value(\'shipping_address_1\');
$postal_code = WC()->checkout()->get_value(\'shipping_postcode\');
//Default values
if (!isset($shipping_adr) || !isset($postal_code))
{
//Form with ajax functionality
$form = \'<form id="frmGetLocations" action="" name="">\';
$form .= \'<input type="text" id="dhlsweden_shipping_adr" name="dhlsweden_shipping_adr" placeholder="Ange gatuadress">\';
$form .= \'<input type="text" id="dhlsweden_shipping_postalcode" name="dhlsweden_shipping_postalcode" placeholder="Ange postnr">\';
$form .= \'<input type="button" id="dhlsweden_button_shippinglocations" name="dhlsweden_button_shippinglocations" value="Ok">\';
$form .= \'</form>\';
$form .= \'<select id="dhlsweden_delivery" name="delivery">\';
$form .= \'</select>\';
return array(\'userform\' => $form); //This function returns array only
}
$adr_arr = explode(\' \', $shipping_adr);
$street = $adr_arr[0];
$street_number = $adr_arr[1];
//var_dump($fn2);
$api_url = self::base_url . self::servicepointlocator_url . self::nearest_servicepoints;
$addr[\'address\'] = array(
\'street\' => $street,
\'streetNumber\' => $street_number,
\'additionalAddressInfo\' => \'\',
\'cityName\' => \'\',
\'postalCode\' => $postal_code,
\'countryCode\' => \'\'
);
$feature_codes = array(\'featureCodes\' => array(\'\'));
$bit_cat_codes = array(\'bitCatCodes\' => array(\'\'));
$max_nr_items = array(\'maxNumberOfItems\' => 0);
$json_data = array_merge($addr, $feature_codes,$bit_cat_codes,$max_nr_items);
$jd = json_encode($json_data);
/*
* var_dump($jd);
*/
//Response from DHL server
$dhl_response = wp_remote_post(
$api_url ,
array(
\'headers\' => array( \'client-key\' => self::client_key, \'accept\' => \'text/plain\', \'Content-Type\' => \'application/json-patch+json\' ),
\'body\' => $jd
) //Client-key is genereated in DHL Dashboard
);
$json_arr = json_decode($dhl_response[\'body\'],true);
$service_points = array();
foreach($json_arr[\'servicePoints\'] as $sp) {
$service_points[] = array(\'id\' => $sp[\'id\'],
\'name\' => $sp[\'name\']);
}
return $service_points;
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
$this->init_settings(); // This is part of the settings API. Loads settings you previously init.
// Save settings in admin if you have any defined
$this->supports = array(
\'settings\',
\'shipping-zones\',
\'instance-settings\',
\'instance-settings-modal\'
);
add_action( \'woocommerce_update_options_shipping_\' . $this->id, array( $this, \'process_admin_options\' ) );
}
public function load_js() {
wp_enqueue_script(
\'dhlswedenjs\',
plugins_url(\'/js/wibergsweb.js\', __FILE__)
);
wp_localize_script( \'dhlswedenjs\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) ); //Frontend ajax url
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package = array() ) {
$this->add_rate(
array(
\'label\' => $this->title,
\'cost\' => 0,
\'taxes\' => false,
\'package\' => $package,
)
);
}
}
}
}
add_filter( \'woocommerce_shipping_methods\', \'add_shipping_DHLSweden\' );
function add_shipping_DHLSweden( $methods ) {
$methods[\'dhl_sweden\'] = \'WC_Shipping_DHLsweden\';
return $methods;
}
add_action( \'woocommerce_shipping_init\', \'dhlsweden_shipping_method_init\' );
function shippingmethod_template($template, $template_name, $template_path) {
if ($template_name == \'cart/cart-shipping.php\') {
$template = plugin_dir_path(__FILE__) . \'/woocommerce/cart/cart-shipping.php\';
}
return $template;
}
add_filter(\'woocommerce_locate_template\', \'shippingmethod_template\', 20, 3);
}
我试图弄清楚如何执行ajax调用(我以前做过,但不是在“发货方法上下文”中)
我的javascript文件如下所示:
$(\'#dhlsweden_delivery\').hide();
$(\'#dhlsweden_button_shippinglocations\').on(\'click\', \'\', function() {
var shipping_adr = $(\'#dhlsweden_shipping_adr\').val();
var shipping_postalcode = $(\'#dhlsweden_shipping_postalcode\').val();
alert(shipping_adr);
alert(shipping_postalcode);
var check_dhl_location = $.ajax({
type: \'POST\',
data:{
action: \'checklocation\',
dhlsweden_shipping_adr: shipping_adr,
dhlsweden_shipping_postalcode: shipping_postalcode
},
url: MyAjax.ajaxurl,
dataType: \'json\'
});
check_dhl_location.done(function(content_data) {
alert(\'ok!\');
console.log( content_data );
});
check_dhl_location.fail(function(ts) {
alert(\'Något fallerade\');
alert(ts.responseText);
});
});