首页  >  问答  >  正文

定制Woocommerce配送区域方法

<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>
id = 'diet_day_shipping'; // 您的运输方式的 ID。应该是独一无二的。
                $this->method_title = __('每个节食日的运费');  // 管理中显示的标题
                $this->method_description = __('每次预订的运费'); // 管理中显示的描述
                $this->enabled = “是”; // 这可以作为设置添加,但在本例中强制启用
                //$this->title = "轻松预订每日送货"; // 这可以作为设置添加,但在本例中是强制的。
                //$this->rates = array('calculate_shipping');
                $this->支持=数组(
                    '运输区域',
                    '实例设置',
                    '实例设置模式',
                );
                $this->instance_form_fields =
                    大批(
                        '标题' =>大批(
                            '标题' => __('方法标题'),
                            '类型' => '文本',
                            '描述' => __('这控制用户在结账时看到的标题。'),
                            '默认' => __('每日配送适合您的饮食'),
                        ),
                        '成本' =>大批(
                            '标题' => __('成本'),
                            '类型' => '数字',
                            '描述' => __('这控制了每天的成本。'),
                            '默认' => 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'));
            }/*** 初始化您的设置
             *
             * @访问公共
             * @返回无效*/
            函数初始化()
            {
                // 加载设置API
                $this->init_form_fields();

                // 如果您有任何定义,请在管理中保存设置
                add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
            }

            函数 init_form_fields()
            {
                $this->form_fields = array(
                    '标题' =>大批(
                        '标题' => __('方法标题'),
                        '类型' => '文本',
                        '描述' => __('这控制用户在结账时看到的标题。'),
                        '默认' => __('日常饮食运费'),
                    ),
                    '成本' =>大批(
                        '标题' => __('成本'),
                        '类型' => '数字',
                        '描述' => __('这控制了每天的成本。'),
                        '默认' => 7、
                    )
                );
            }

            /*** 计算运费函数。
             * @param array $package (默认: array())*/
            公共函数calculate_shipping($package = array())
            {

                //用于测试的硬编码
                $shipping_number = 7;

                //计算最终运费的可能方法 
                /*
                foreach ($package['contents'] as $item_id => $values) {
                    $_product = $values['数据'];
                    $shipping_number = $shipping_number + $_product->wceb_get_product_booking_duration() * $values['quantity'];
                }*/

                $this->add_rate(数组(
                    'id'=> $this->id 。 $this->instance_id,
                    '标签' => 'Dostawa poza Wa-wa',
                    '成本' => $shipping_number,
                ));
            }
        }
    }
}

add_action('woocommerce_shipping_init', 'your_shipping_method_init');

函数 add_your_shipping_method($methods)
{
    $methods['diet_day_shipping'] = 'WC_Your_Shipping_Method';
    返回$方法;
}

add_filter('woocommerce_shipping_methods', 'add_your_shipping_method');</pre>
<p>我需要有 2 个设置(只能在区域、方法中,不需要全局)标题(向客户显示)和成本 - 了解多人游戏天数的价值。</p>
<p>我想要“编辑”按钮显示以下关于运输区域方法的方法,我可以在其中设置这些值。</p>
<p>有人可以帮助修复我的代码吗?</p>
<p>目前管理面板中没有显示任何设置选项。不在运送区域方法行中,并且在 sgipping 设置面板中也没有。</p>
<p>此外,当我使用自定义送货方式从区域发送邮政编码时 - 购物车显示该邮政编码没有可用的送货方式)</p>
P粉558478150P粉558478150389 天前423

全部回复(1)我来回复

  • P粉833546953

    P粉8335469532023-08-30 00:08:08

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

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

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

    设置

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

    https://woocommerce.com/document/shipping-method-api/

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

    要使用设置功能/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'] 检索保存的设置

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

    回复
    0
  • 取消回复