【发布时间】:2014-05-29 14:26:16
【问题描述】:
我对 php 和 woocommerce 还很陌生。我正在开发一个使用 wordpress 和 woocommerce 的电子商务网站。该公司使用 API 根据购物车中物品的面积和尺寸以及物品数量来计算运费。我已经设法将所有需要的值解析到 API,并且确实得到了响应。我为提供的 3 种测试费率创建了一个自定义运输插件。 在我的脚本中,我创建了会话,其中包含每个运输方法的总数,如下所示:
$_SESSION['overnight']= $quoteResponse->results[0]->rates[2]->total;
然后在我的自定义运输插件中,我启动会话并将成本设置为等于会话内的值:
<?php
session_start();
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function WC_Overnight_Express_init() {
if ( ! class_exists( 'WC_Overnight_Express' ) ) {
class WC_Overnight_Express extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'overnight express'; // Id for your shipping method. Should be uunique.
$this->method_title = __( 'Overnight Express' ); // Title shown in admin
$this->method_description = __( 'Overnight Express Shipping method' );
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->title = "Overnight Express"; // This can be added as an setting but for this example its forced.
$this->init();
}
/**
* 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
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping() {
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' =>$_SESSION['overnight'],
'calc_tax' => 'per_item'
);
// Register the rate
$this->add_rate( $rate );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'WC_Overnight_Express_init' );
function add_overnight_method( $methods ) {
$methods[] = 'WC_Overnight_Express';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_overnight_method' );
}
?>
我的问题是,在从 API 返回报价后,他们不会更新显示“免费”的运输总额,并且只有在我打开购物车并返回结帐页面时才会更新。我想做的是在从 API 返回总数时在运行时更新它们。这是我在 stackoverlow 上的第一篇文章,任何帮助将不胜感激。谢谢
【问题讨论】:
标签: php jquery ajax wordpress woocommerce