定制Woocommerce配送区域方法
P粉558478150
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;">&lt;?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-&gt;id = 'diet_day_shipping'; // Id for your shipping method. Should be uunique. $this-&gt;method_title = __('Shipping per diet day'); // Title shown in admin $this-&gt;method_description = __('Shipping cost per booking'); // Description shown in admin $this-&gt;enabled = &quot;yes&quot;; // This can be added as an setting but for this example its forced enabled //$this-&gt;title = &quot;Easy Booking Shipping per day&quot;; // This can be added as an setting but for this example its forced. //$this-&gt;rates = array('calculate_shipping'); $this-&gt;supports = array( 'shipping-zones', 'instance-settings', 'instance-settings-modal', ); $this-&gt;instance_form_fields = array( 'title' =&gt; array( 'title' =&gt; __('Method Title'), 'type' =&gt; 'text', 'description' =&gt; __('This controls the title which the user sees during checkout.'), 'default' =&gt; __('Daily delivery for Your diet'), ), 'cost' =&gt; array( 'title' =&gt; __('Cost'), 'type' =&gt; 'number', 'description' =&gt; __('This controls the cost per day.'), 'default' =&gt; 7, ) ); $this-&gt;init(); $this-&gt;title = $this-&gt;get_option('title'); $this-&gt;cost = $this-&gt;get_option('cost'); add_action('woocommerce_update_options_shipping_' . $this-&gt;id, array($this, 'process_admin_options')); } /** * Init your settings * * @access public * @return void */ function init() { // Load the settings API $this-&gt;init_form_fields(); // Save settings in admin if you have any defined add_action('woocommerce_update_options_shipping_' . $this-&gt;id, array($this, 'process_admin_options')); } function init_form_fields() { $this-&gt;form_fields = array( 'title' =&gt; array( 'title' =&gt; __('Method Title'), 'type' =&gt; 'text', 'description' =&gt; __('This controls the title which the user sees during checkout.'), 'default' =&gt; __('Daily diet shipping cost'), ), 'cost' =&gt; array( 'title' =&gt; __('Cost'), 'type' =&gt; 'number', 'description' =&gt; __('This controls the cost per day.'), 'default' =&gt; 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 =&gt; $values) { $_product = $values['data']; $shipping_number = $shipping_number + $_product-&gt;wceb_get_product_booking_duration() * $values['quantity']; }*/ $this-&gt;add_rate(array( 'id' =&gt; $this-&gt;id . $this-&gt;instance_id, 'label' =&gt; 'Dostawa poza Wa-wa', 'cost' =&gt; $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>
P粉558478150
P粉558478150

全部回复(1)
P粉833546953

我昨天也花了三个小时,今天又花了一个小时试图找出一个非常相似的问题。我相信我现在有了一个解决方案,或者至少可以帮助您进入正确的方向。官方 WooCommerce 文档似乎在区分设置实例设置(或者我找不到有关实例设置的文档 - 更多内容请参见下一段) )。

以下内容基于我迄今为止通过反复试验发现的内容。如果有人有任何关于此的“官方”文档,我期待对此回复的评论。

区分“设置”和“实例设置”非常重要。请参阅此图片以在 WooCommerce 后端进行视觉比较:

设置

设置指的是“全球运输方式设置”,即它们出现在全局子菜单中,但不在运输区域内。官方 WooCommerce 文档显示了如何使用设置以及如何添加新的设置页面:

https://woocommerce.com/document/shipping-method-api/一个>

https://woocommerce.com/document/settings-api/ p>

要使用设置功能/API,您必须指定

$this->supports = array('settings'); // You may also need to include shipping-zones

在你的构造函数中。

“设置”在全球范围内应用/使用/存储,即与各个送货区域无关。

实例设置

实例设置是可以针对每个运送区域单独保存的设置。要使用它们,您必须使用

$this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal');

在你的构造函数中。

下面的示例让我了解了实例设置 API 的微妙但重要的差异:

https://gist.github.com/malkafly/57a5e07fd03d6fd5cab84c6182d84c86

代码差异以及关于您的原始问题

请注意,这两个 API 的确切功能和属性存在细微差别。根据您的具体情况,您应该决定使用其中一个 API,然后严格使用相应的 API。您混淆了这两个 API,这可能就是您无法使自定义送货方式发挥作用的原因。

如果您希望使用(全局)设置 API:

  • $this->supports = array('shipping-zones', 'settings');
  • 使用 $this->form_fields(而不是 $this->instance_form_fields)
  • 使用 $this->settings['title'] 检索保存的设置

如果您希望使用(特定于配送区域的)实例设置 API:

  • 确保您的构造函数如下所示/包含以下代码:

    public function __construct( $instance_id = 0 ) {
              $this->instance_id = absint( $instance_id );
              //...
        }
  • 使用 $this->instance_settings['title'] 检索保存的设置

我在上面发布的相应设置/实例设置类别中的链接包含每种方法的大量附加(工作)代码示例。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号