检查 WooCommerce 自定义订单状态是否存在以执行代码

Check if WooCommerce custom order status exist to execute code

我正在创建 woocommerce 插件,我有一个自定义订单状态“wc-shipped”。 我只想为此自定义订单状态执行一些插件选项。这是我的代码


add_action('text_sms_form_fields', 'sms_woocommerce_fields', 10, 1);
function sms_woocommerce_fields($textme_option, $wc_statuses_arr) { ?>
if( isset( $wc_statuses_arr['wc-shipped'] ) ) {  ?>
                <hr>
                <fieldset>
                    <label for="textme_order_shipped">
                        <input name="textme_order_shipped" type="checkbox"
                               id="textme_order_shipped" <?php if ($textme_option['textme_order_shipped'] == "1") {
                            echo 'checked';
                            
                        } ?>
                               value="1"/>
                        <span><?php esc_html_e('Send SMS when order status is shipped ', 'send_sms'); ?></span>
                    </label>
                </fieldset>

                <div class="textme_order_shipped_content <?php if ($textme_option['textme_order_shipped'] != '1') {
                    echo 'hidden';
                } ?>">
                    <table>
                        <tr>
                            <td></td>
                            
                                <td><textarea id="textme_order_shipped_sms_customer" name="textme_order_shipped_sms_customer"
                                          cols="80" rows="3"
                                          class="all-options"><?php if ($textme_option['textme_order_shipped_sms_customer']) {
                                        echo $textme_option['textme_order_shipped_sms_customer'];
                                    } ?></textarea>
                            </td>
                        </tr>
                    </table>
                </div>
                <?php } ?>

如果存在“wc-shipped”状态,我想显示这些插件选项,但我遇到以下错误。

如何解决这个问题?

你的钩子函数有 2 个变量(参数),所以你需要声明这 2 个变量来替换代码的第一行:

add_action( 'text_sms_form_fields', 'sms_woocommerce_fields', 10, 1 );

只需:

add_action( 'text_sms_form_fields', 'sms_woocommerce_fields', 10, 2 );

这应该可以解决这个问题。参见 WordPress add_action()...