我花了几个小时尝试找到正确的方法来实现创建自定义运送区域方法的解决方案。
WC:7.2.2 工作压力:6.0.3
站点运行状况良好,没有权限错误等(仅更新可用信息)
我使用了一些信息源,例如: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#shipping-zone-methods
https://github.com/woocommerce/woocommerce/wiki/Shipping-Method-API#properties
https://gist.github.com/mikejolley/3b37b9cc19a774665f31
https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098
还有许多其他。
基本上,我需要示例代码来为具有自定义运输成本函数的运输区域方法创建(插件)。我们基于 od Woocommerce Easy Booking,并且元数据中有天数,这就是为什么我们不能使用 woocommerce 官方高级插件之一来处理该问题。
我最终得到了以下代码:
<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
function your_shipping_method_init()
{
if (!class_exists('WC_Your_Shipping_Method')) {
class WC_Your_Shipping_Method extends WC_Shipping_Method
{
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct()
{
$this->id = 'diet_day_shipping'; // Id for your shipping method. Should be uunique.
$this->method_title = __('Shipping per diet day'); // Title shown in admin
$this->method_description = __('Shipping cost per booking'); // Description shown in admin
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
//$this->title = "Easy Booking Shipping per day"; // This can be added as an setting but for this example its forced.
//$this->rates = array('calculate_shipping');
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->instance_form_fields =
array(
'title' => array(
'title' => __('Method Title'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.'),
'default' => __('Daily delivery for Your diet'),
),
'cost' => array(
'title' => __('Cost'),
'type' => 'number',
'description' => __('This controls the cost per day.'),
'default' => 7,
)
);
$this->init();
$this->title = $this->get_option('title');
$this->cost = $this->get_option('cost');
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init()
{
// Load the settings API
$this->init_form_fields();
// Save settings in admin if you have any defined
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
}
function init_form_fields()
{
$this->form_fields = array(
'title' => array(
'title' => __('Method Title'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.'),
'default' => __('Daily diet shipping cost'),
),
'cost' => array(
'title' => __('Cost'),
'type' => 'number',
'description' => __('This controls the cost per day.'),
'default' => 7,
)
);
}
/**
* calculate_shipping function.
* @param array $package (default: array())
*/
public function calculate_shipping($package = array())
{
//hardcode for testing
$shipping_number = 7;
//probably way to calculate final shipping cost
/*
foreach ($package['contents'] as $item_id => $values) {
$_product = $values['data'];
$shipping_number = $shipping_number + $_product->wceb_get_product_booking_duration() * $values['quantity'];
}*/
$this->add_rate(array(
'id' => $this->id . $this->instance_id,
'label' => 'Dostawa poza Wa-wa',
'cost' => $shipping_number,
));
}
}
}
}
add_action('woocommerce_shipping_init', 'your_shipping_method_init');
function add_your_shipping_method($methods)
{
$methods['diet_day_shipping'] = 'WC_Your_Shipping_Method';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_your_shipping_method');
我需要有 2 个设置(只能在区域、方法中,不需要全局)标题(向客户显示)和成本 - 了解多人游戏天数的价值。
我想要“编辑”按钮显示以下关于运输区域方法的方法,我可以在其中设置这些值。
有人可以帮助修复我的代码吗?
目前管理面板中没有显示任何设置选项。不在运送区域方法行中,并且在 sgipping 设置面板中也没有。
此外,当我使用自定义送货方式从区域发送邮政编码时 - 购物车显示该邮政编码没有可用的送货方式)
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号