我正在创建woocommerce定制运输方法。它工作正常,但是当我使用运输计算器时,它不会刷新。
<?php
/**
* Plugin Name: Expresso Jundiaí for WooCommerce
* Plugin URI: https://valoremarketing.com.br
* Description: Adds Expresso Jundiaí shipping methods to your WooCommerce store.
* Author: Valore + NSC
* Author URI: https://valoremarketing.com.br
* Version: 1.0.0
* License: GPLv2 or later
*
*/
if (!defined('ABSPATH')) {
exit;
}
/*
* Check if WooCommerce is active
*/
if (in_array('woocommerce/woocommerce.php',apply_filters('active_plugins',get_option('active_plugins')))) {
function expresso_jundiai_shipping_method()
{
if (!class_exists('Expresso_Jundiai_Shipping_Method')) {
class Expresso_Jundiai_Shipping_Method extends WC_Shipping_Method
{
private $args = '';
/**
* Constructor
*
* @access public
* @return void
*/
public function __construct($instance_id = 0)
{
$this->id = 'expresso-jundiai';
$this->title = 'Expresso Jundiaí';
$this->instance_id = absint($instance_id);
$this->method_title = __('Expresso Jundiaí Entregas','expresso-jundiai');
$this->method_description = __('Entregas feitas por Expresso Jundiaí','expresso-jundiai');
/**
* Utilized to woocommerce to verify the supported features by the shipping method
*/
$this->supports = array(
'shipping-zones','instance-settings','instance-settings-modal','settings'
);
$this->init();
}
/**
* Init your settings
*
* @access public
* @return void
*/
public function init()
{
$this->init_form_fields();
$this->init_settings();
/// actions.
add_action('woocommerce_update_options_shipping_' . $this->id,array($this,'process_admin_options'));
}
/**
* Define settings field for this shipping
*
* To access the fields:
* $shipping_methods = WC()->shipping->get_shipping_methods();
* foreach ( $shipping_methods as $key => $shipping_method ) {
* $code = $shipping_method->settings->code;
* }
*
* @return void
*/
public function init_form_fields()
{...}
/**
* Get the args needed to make a post request to API target
*
* @param $package
* @return mixed
*/
private function get_request_args( $package )
{...}
/**
* This function is used to calculate the shipping cost.
* Within this function we can check for weights,dimensions and other parameters in package.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package = array() )
{
$args = $this->get_request_args( $package );
$uri = $this->get_option( 'apiUri' );
$response = $this->request_to_api( $uri,$args );
if ($response !== NULL) {
$this->add_rate( array(
'id' => $this->id,'label' => $this->title,'package' => $package,'cost' => $response->vlrFrete,) );
}
}
/**
* Calculates the cubage of all products.
*
* @param $package
* @return float|int
*/
private function get_total_cubage( $package ) {
$total = 0;
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$height = (float) $_product->get_height();
$width = (float) $_product->get_width();
$length = (float) $_product->get_length();
$total += $height * $width * $length;
}
return ( ($total > 0) ? ( $total/1000000 ) : 0 );
}
/**
* @param $package
* @return int
*/
private function get_total_volumes( $package ) {
$total = 0;
foreach ( $package['contents'] as $item_id => $values ) {
$total += (int) $values['quantity'];
}
return $total;
}
/**
* Calculate the total wheight of all products of the order
* @param $package
* @return float|int
*/
private function get_order_total_weight( $package ) {
$total = 0;
foreach ( $package['contents'] as $item_id => $values )
{
$_product = $values['data'];
$_product_weight = (float) $_product->get_weight();
$total += $_product_weight * $values['quantity'];
}
$total = wc_get_weight( $total,'kg' );
return $total;
}
/**
* @param $uri
* @param $args
* @return mixed|void
*/
private function request_to_api( $uri,$args )
{
$response = wp_remote_post( $uri,$args );
var_dump($args,$response);
if ( is_wp_error( $response ) || '200' != wp_remote_retrieve_response_code( $response )) {
return;
}
$body = json_decode( wp_remote_retrieve_body( $response ) );
if ( empty( $body ) )
return;
return $body;
}
}
}
}
add_action('woocommerce_shipping_init','expresso_jundiai_shipping_method');
/**
* Adds the name of class into shipping methods
*
* @param $methods
* @return array
*/
function add_expresso_jundiai_shipping_method($methods)
{
/**
* The key of the array $method must match the shipping method id
*/
$methods['expresso-jundiai'] = 'Expresso_Jundiai_Shipping_Method';
return $methods;
}
add_filter('woocommerce_shipping_methods','add_expresso_jundiai_shipping_method');
}
当我在购物车中使用货运计算器时,我需要使用相同的规则进行重新计算...当wordpress调用/?wc-ajax = update_shipping_method
我正在尝试两天...请...帮助