【问题标题】:Check if WooCommerce custom order status exist to execute code检查是否存在 WooCommerce 自定义订单状态以执行代码
【发布时间】:2020-10-26 10:08:22
【问题描述】:

我正在创建 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”状态存在,我想显示这些插件选项,但我遇到错误。

如何解决这个问题?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce action-hook


    【解决方案1】:

    您的挂钩函数有 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()...

    【讨论】:

    • 先生,它不起作用,但我尝试了 $wc_statuses = wc_get_order_statuses(); foreach ($wc_statuses as $key => $value) { echo $value;然后我得到这个值:Pending paymentProcessingOn holdCompletedCancelledRefundedFailedShipped。如果发货状态存在,我们如何添加这个值?问候
    • 谢谢先生,这行有效 $wc_statuses = wc_get_order_statuses(); if( isset( $wc_statuses['wc-shipped'] ) ) { ?> 问候
    猜你喜欢
    • 2018-12-30
    • 2020-02-01
    • 2019-11-13
    • 2018-08-30
    • 2019-08-27
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    相关资源
    最近更新 更多