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粉8335469532023-08-30 00:08:08
我昨天也花了三個小時,今天又花了一個小時試圖找出一個非常相似的問題。我相信我現在有了一個解決方案,或者至少可以幫助您進入正確的方向。官方 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:
如果您希望使用(特定於配送區域的)實例設定 API:
確保您的建構子如下所示/包含以下程式碼:
public function __construct( $instance_id = 0 ) { $this->instance_id = absint( $instance_id ); //... }
使用 $this->instance_settings['title'] 檢索已儲存的設定
我在上面發布的相應設定/實例設定類別中的連結包含每種方法的大量附加(工作)程式碼範例。