在WooCommerce中为foreach()提供了无效的参数

时间:2019-05-30 23:42:57

标签: php wordpress woocommerce

我有一个小问题,我正在WooCommerce中修改“我的帐户”的标签结构,我想将“地址”与“帐户详细信息”链接起来,当我尝试打印该字段时,“地址”出现问题“帐户详细信息”中的“标签”出现:

Warning: Invalid argument supplied for foreach () in C: \ xampp \ htdocs \ wp-content \ plugins \ woocommerce \ templates \ myaccount \ form-edit-address.php on line 34

这是错误代码:

<?php 
foreach ($address as $key => $field) { // **line 34**
woocommerce_form_field ($key, $field, wc_get_post_data_by_key ($key, $field ['value']));
}
?>

在var_dump中抛出NULL的地方。看到所有这些,我试图在class-wc-shortcode-my-account.php中修改函数,但我没有达到目标。我认为存在一些限制,因此只能在该模板中使用该数组。我不知道我是否正确,在wc-template-functions.php中发现的内容是:

function woocommerce_account_edit_address( $type ) {
        $type = wc_edit_address_i18n( sanitize_title( $type ), true );

        WC_Shortcode_My_Account::edit_address( $type );
    }
}

如何在自定义选项卡中打印$ address数组?谢谢!

升级:

我的尝试之一是将文件form-edit-address.php包含在form-edit-account.php中

<?php
include("form-edit-address.php");
?>

另一种尝试是将form-edit-address.php文件的内容复制到form-edit-account中。还要尝试放置全局$ address和$ load-address变量而没有任何作用:

public static function edit_address( $load_address = 'billing' ) {
        $current_user = wp_get_current_user();
        global $load_address;
        $load_address = sanitize_key( $load_address );
        $country      = get_user_meta( get_current_user_id(), $load_address . '_country', true );

        if ( ! $country ) {
            $country = WC()->countries->get_base_country();
        }

        if ( 'billing' === $load_address ) {
            $allowed_countries = WC()->countries->get_allowed_countries();

            if ( ! array_key_exists( $country, $allowed_countries ) ) {
                $country = current( array_keys( $allowed_countries ) );
            }
        }

        if ( 'shipping' === $load_address ) {
            $allowed_countries = WC()->countries->get_shipping_countries();

            if ( ! array_key_exists( $country, $allowed_countries ) ) {
                $country = current( array_keys( $allowed_countries ) );
            }
        }

        global $address;

        $address = WC()->countries->get_address_fields( $country, $load_address . '_' );

        // Enqueue scripts.
        wp_enqueue_script( 'wc-country-select' );
        wp_enqueue_script( 'wc-address-i18n' );

        // Prepare values.
        foreach ( $address as $key => $field ) {

            $value = get_user_meta( get_current_user_id(), $key, true );

            if ( ! $value ) {
                switch ( $key ) {
                    case 'billing_email':
                    case 'shipping_email':
                        $value = $current_user->user_email;
                        break;
                }
            }

            $address[ $key ]['value'] = apply_filters( 'woocommerce_my_account_edit_address_field_value', $value, $key, $load_address );
        }

        wc_get_template(
            'myaccount/form-edit-address.php',
            array(
                'load_address' => $load_address,
                'address'      => apply_filters( 'woocommerce_address_to_edit', $address, $load_address ),
            )
        );
    }

1 个答案:

答案 0 :(得分:0)

我需要仔细看一下您的代码,但是我猜想这是您键入变量时遇到的问题。

尝试用$address代替$ address