Heim  >  Fragen und Antworten  >  Hauptteil

Passen Sie die Woocommerce-Versandgebietsmethode an

<p>Ich habe mehrere Stunden damit verbracht, die richtige Lösung für die Erstellung einer benutzerdefinierten Versandbereichsmethode zu finden. </p> <p>WC:7.2.2 Arbeitsstress: 6.0.3</p> <p>Die Website ist in gutem Zustand, keine Berechtigungsfehler usw. (aktualisiert nur verfügbare Informationen)</p> <p>Ich habe eine Reihe von Quellen verwendet, wie zum Beispiel: 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>Es gibt noch viele andere. </p> <p>Grundsätzlich benötige ich Beispielcode zum Erstellen (Plugin) für eine Versandzonenmethode mit einer benutzerdefinierten Versandkostenfunktion. Wir basieren auf Woocommerce Easy Booking und haben Tage in den Metadaten, deshalb können wir dafür keines der offiziellen Premium-Plugins von Woocommerce verwenden.</p> <p>我最终得到了以下代码:</p> <pre class="brush:php;toolbar:false;"><?php if (!definiert('ABSPATH')) { Ausfahrt; // Beenden, wenn direkt darauf zugegriffen wird } Funktion your_shipping_method_init() { if (!class_exists('WC_Your_Shipping_Method')) { Die Klasse WC_Your_Shipping_Method erweitert WC_Shipping_Method { /*** Konstruktor für Ihre Versandklasse * * @access public * @return void*/ öffentliche Funktion __construct() { $this->id = 'diet_day_shipping'; // ID für Ihre Versandart. Sollte einzigartig sein. $this->method_title = __('Versand pro Diättag'); // Titel wird im Admin angezeigt $this->method_description = __('Versandkosten pro Buchung'); // Beschreibung wird im Admin angezeigt $this->enabled = "yes"; // Dies kann als Einstellung hinzugefügt werden, in diesem Beispiel wird die Aktivierung jedoch erzwungen //$this->title = "Einfache Buchung, Versand pro Tag"; // Dies kann als Einstellung hinzugefügt werden, ist aber in diesem Beispiel erzwungen. //$this->rates = array('calculate_shipping'); $this->supports = array( 'Versandzonen', 'Instanzeinstellungen', 'instance-settings-modal', ); $this->instance_form_fields = Array( 'title' => Array( 'title' => __('Methodentitel'), 'Typ' => 'Text', 'Beschreibung' => __('Dies steuert den Titel, den der Benutzer beim Auschecken sieht.'), 'default' => __('Tägliche Lieferung für Ihre Ernährung'), ), 'Kosten' => Array( 'title' => __('Kosten'), 'Typ' => 'Nummer', 'Beschreibung' => __('Dies steuert die Kosten pro Tag.'), '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')); }/*** Initialisieren Sie Ihre Einstellungen * * @access public * @return void*/ Funktion init() { // Laden Sie die Einstellungs-API $this->init_form_fields(); // Einstellungen im Admin speichern, falls Sie welche definiert haben add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options')); } Funktion init_form_fields() { $this->form_fields = array( 'title' => Array( 'title' => __('Methodentitel'), 'Typ' => 'Text', 'Beschreibung' => __('Dies steuert den Titel, den der Benutzer beim Auschecken sieht.'), 'default' => __('Tägliche Versandkosten für die Diät'), ), 'Kosten' => Array( 'title' => __('Kosten'), 'Typ' => 'Nummer', 'Beschreibung' => __('Dies steuert die Kosten pro Tag.'), 'default' => 7, ) ); } /*** Funktion „Berechnen_Versand“. * @param array $package (Standard: array())*/ öffentliche Funktion berechne_versand($package = array()) { //Hardcode zum Testen $shipping_number = 7; //wahrscheinlich Methode zur Berechnung der endgültigen Versandkosten /* 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“, 'Kosten' => $shipping_number, )); } } } } add_action('woocommerce_shipping_init', 'your_shipping_method_init'); Funktion 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>多人游戏天数的价值.</p> <p></p> <p>Kann mir jemand helfen, meinen Code zu reparieren? </p> <p>Derzeit werden im Admin-Bereich keine Einstellungsoptionen angezeigt. Nicht in der Zeile „Versandbereichsmethode“ und nicht im Bereich „Versandeinstellungen“. </p> <p>Wenn ich außerdem eine Postleitzahl aus einer Zone mit einer benutzerdefinierten Versandmethode sende, wird im Warenkorb angezeigt, dass für diese Postleitzahl keine Versandmethode verfügbar ist)</p>
P粉558478150P粉558478150389 Tage vor420

Antworte allen(1)Ich werde antworten

  • 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'] 检索保存的设置

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

    Antwort
    0
  • StornierenAntwort