定制Woocommerce配送区域方法
P粉558478150
2023-08-29 10:57:38
[PHP讨论组]
<p>我花了几个小时尝试找到正确的方法来实现创建自定义运送区域方法的解决方案。</p>
<p>WC:7.2.2
工作压力:6.0.3</p>
<p>站点运行状况良好,没有权限错误等(仅更新可用信息)</p>
<p>我使用了一些信息源,例如:
https://woocommerce.github.io/woocommerce-rest-api-docs/?php#shipping-zone-methods</p>
<p>https://github.com/woocommerce/woocommerce/wiki/Shipping-Method-API#properties</p>
<p>https://gist.github.com/mikejolley/3b37b9cc19a774665f31</p>
<p>https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098</p>
<p>还有许多其他。</p>
<p>基本上,我需要示例代码来为具有自定义运输成本函数的运输区域方法创建(插件)。我们基于 od Woocommerce Easy Booking,并且元数据中有天数,这就是为什么我们不能使用 woocommerce 官方高级插件之一来处理该问题。</p>
<p>我最终得到了以下代码:</p>
<pre class="brush:php;toolbar:false;"><?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');</pre>
<p>我需要有 2 个设置(只能在区域、方法中,不需要全局)标题(向客户显示)和成本 - 了解多人游戏天数的价值。</p>
<p>我想要“编辑”按钮显示以下关于运输区域方法的方法,我可以在其中设置这些值。</p>
<p>有人可以帮助修复我的代码吗?</p>
<p>目前管理面板中没有显示任何设置选项。不在运送区域方法行中,并且在 sgipping 设置面板中也没有。</p>
<p>此外,当我使用自定义送货方式从区域发送邮政编码时 - 购物车显示该邮政编码没有可用的送货方式)</p>
我昨天也花了三个小时,今天又花了一个小时试图找出一个非常相似的问题。我相信我现在有了一个解决方案,或者至少可以帮助您进入正确的方向。官方 WooCommerce 文档似乎在区分设置和实例设置(或者我找不到有关实例设置的文档 - 更多内容请参见下一段) )。
以下内容基于我迄今为止通过反复试验发现的内容。如果有人有任何关于此的“官方”文档,我期待对此回复的评论。
区分“设置”和“实例设置”非常重要。请参阅此图片以在 WooCommerce 后端进行视觉比较:
设置
设置指的是“全球运输方式设置”,即它们出现在全局子菜单中,但不在运输区域内。官方 WooCommerce 文档显示了如何使用设置以及如何添加新的设置页面:
https://woocommerce.com/document/shipping-method-api/一个>
https://woocommerce.com/document/settings-api/ p>
要使用设置功能/API,您必须指定
在你的构造函数中。
“设置”在全球范围内应用/使用/存储,即与各个送货区域无关。
实例设置
实例设置是可以针对每个运送区域单独保存的设置。要使用它们,您必须使用
在你的构造函数中。
下面的示例让我了解了实例设置 API 的微妙但重要的差异:
https://gist.github.com/malkafly/57a5e07fd03d6fd5cab84c6182d84c86
代码差异以及关于您的原始问题
请注意,这两个 API 的确切功能和属性存在细微差别。根据您的具体情况,您应该决定使用其中一个 API,然后严格使用相应的 API。您混淆了这两个 API,这可能就是您无法使自定义送货方式发挥作用的原因。
如果您希望使用(全局)设置 API:
如果您希望使用(特定于配送区域的)实例设置 API:
确保您的构造函数如下所示/包含以下代码:
使用 $this->instance_settings['title'] 检索保存的设置
我在上面发布的相应设置/实例设置类别中的链接包含每种方法的大量附加(工作)代码示例。