我正在为woocommerce制定一种定制发货方法,我的问题是,可以将其添加到管理区域的特定区域&;调整设置即可。但出于某种原因,它没有出现在结账页面上——我也不知道为什么。
这应该是非常直接的,实际上我以前做过几次(3.0之前)-但我现在根本无法让它工作。
主要类别
abstract class Main_Shipping extends WC_Shipping_Method
{
abstract protected function register_shipping($methods);
abstract protected function addActions();
abstract protected function addFilters();
function init()
{
$this->instance_form_fields = include(\'setting/shipping-settings.php\' );
$this->title = $this->get_option(\'title\' );
$this->tax_status = $this->get_option(\'tax_status\');
$this->shipping_price = $this->get_option(\'shipping_price\');
$this->enable_free_shipping = $this->get_option(\'enable_free_shipping\');
$this->free_shipping_total = $this->get_option(\'free_shipping_total\');
$this->init_settings();
add_action( \'woocommerce_update_options_shipping_\' . $this->id, array( $this, \'process_admin_options\' ) );
}
public function calculate_shipping($package = array())
{
global $woocommerce;
$return_price = $this->shipping_price;
if(!isset($return_price))
{
$return_price = 0;
}
$rate = array(
\'id\' => $this->id .\'_\'. $this->instance_id,
\'label\' => $this->title,
\'cost\' => $return_price,
);
$this->add_rate( $rate );
}
public function oaship_pickuppoint()
{
ob_start();
?>
<div>
test
</div>
<?php
$sContent = ob_get_clean();
echo $sContent;
}
}
特定GLS承运人类别:
require_once(\'main-shipping.php\');
class GLS_Shipping_Pickuppoint extends Main_Shipping
{
public function __construct( $instance_id = 0 )
{
$this->id = \'gls_shipping_pickuppoint\';
$this->instance_id = absint( $instance_id );
$this->method_title = __(\'GLS Pickup Point OA\', \'\');
$this->method_description = __(\'Adds the option to ship with the GLS pickup point to the checkout\', \'\');
$this->supports = array(
\'shipping-zones\',
\'instance-settings\',
);
$this->init();
}
function addActions()
{
add_action(\'woocommerce_after_shipping_rate\', array($this, \'check_choosen_shipping_method\') );
}
function addFilters()
{
add_filter(\'woocommerce_shipping_methods\', array($this, \'register_shipping_method\'));
}
function register_shipping($methods)
{
$methods[\'gls_shipping_pickuppoint\'] = \'GLS_Shipping_Pickuppoint\';
return $methods;
}
function check_choosen_shipping_method($rate)
{
$chosen_methods = WC()->session->get( \'chosen_shipping_methods\' );
$chosen_shipping = preg_replace(\'/[0-9]+/\', \'\', $chosen_methods[0]);;
if($chosen_shipping === \'gls_shipping_pickuppoint\')
{
if ($rate->method_id == \'gls_shipping_pickuppoint\') {
$this->oaship_pickuppoint();
}
}
}
}
$oOAGlsPickupPoint = new GLS_Shipping_Pickuppoint();
$oOAGlsPickupPoint->addActions();
$oOAGlsPickupPoint->addFilters();
最合适的回答,由SO网友:Yossely Mendoza Meneses 整理而成
我现在正在开发相同的功能,类似的事情发生在我身上(我能够在管理仪表板中看到设置,但在签出页面中看不到)。。。
我所做的是检查选项Enable debug mode 在里面WooCommerce/Settings/Shipping/Shipping options
然后我可以在结帐页面上看到我的定制发货。之后,我再次禁用该选项,并且我的发货仍在结帐页面中可见(我不知道原因,但它起作用)
我正在检查您的代码,注意到您缺少一个添加自定义发货的重要操作:
add_action( \'woocommerce_shipping_init\', \'tutsplus_shipping_method\' );
其中
tutsplus_shipping_method
是一个定义扩展WC\\u Shipping\\u方法的类的函数。
行动woocommerce_shipping_init
是WooCommerce Shippings的主要操作,其中包括实例化之前的所有shipping类。通过使用该操作,我们可以确保在WooCommerce初始化后,我们的发货方法将被包括在内,并放在WooCommerce可以使用它的正确位置。
我还注意到你的过滤器add_filter(\'woocommerce_shipping_methods\', array($this, \'register_shipping_method\'));
指向不存在的函数,可能是类型错误,您希望指向register_shipping
作用
我遵循了本教程,一切都很好:https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098