【发布时间】:2019-11-21 10:15:19
【问题描述】:
我正在尝试在 Woocommerce 中开发一个自定义运费计算插件,但是我无法返回客户输入的 zip 来激活我的公式。在这个例子中,我只放了一个基本公式来测试,但它导致错误“致命错误:调用未定义的方法 Test_Shipping_Br::get_items_needing_shipping() in ...\wp-content\plugins\woocommerce\includes\class- wc-cart.php 在第 1316 行”,当您在添加产品后进入购物车时。
我需要获取客户输入的 zip 和购物车中的包裹信息(数量、重量、高度、宽度、长度),并根据这些信息制定一个特定的公式。我开始了一个基本公式只是试图获取 zip,但它已经导致了我上面报告的错误。
<?php
/*
Plugin Name: Test Shipping
Plugin URI:
Description:
Version: 1.0.0
Author: Wendell Christian
Author URI:
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function Test_Shipping_Br_init() {
if ( ! class_exists( 'Test_Shipping_Br' ) ) {
class Test_Shipping_Br extends WC_Shipping_Method {
public function __construct() {
$this->id = 'Test_Shipping_Br';
$this->method_title = __( 'Test Shipping Brazil' );
$this->method_description = __( '' );
$this->enabled = "yes";
$this->title = "Test Shipping Brazil";
$this->init();
}
function init() {
$this->init_form_fields();
$this->init_settings();
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function calculate_shipping( $package ) {
$array = WC_Cart::get_shipping_packages();
$postcode = $array[0]['destination']['postcode'];
if ($postcode >= 75960000 && $postcode <= 75969999)
{
$cost = 17;
}
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => round($cost,2),
'calc_tax' => 'per_order'
);
$this->add_rate( $rate );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'Test_Shipping_Br_init' );
function add_Test_Shipping_Br( $methods ) {
$methods[] = 'Test_Shipping_Br';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_Test_Shipping_Br' );
}
【问题讨论】:
标签: php wordpress woocommerce