1

I create a function to add different shiping prices foreach user, all works fine for only one user (the first on the list), but i can't get to make the select options foreach user, it only shows 1 option with all the user in it.

the problem is integrate the foreach in here: 'test' => array. It creates the html elemnt but only 1 and not 1 foreach user

my code:



class WC_Shipping_click extends WC_Shipping_Method {
            public function __construct( $instance_id = 0 ) {
        $args = array(
            'role'    => '',
            'orderby' => 'user_nicename',
            'order'   => 'ASC'
        );
        $users = get_users( $args );
        foreach ( $users as $user ){
          $userinfo .= '['.$user->precio.','.$user->first_name.'] ';
        }
                $this->id                    = 'click_method';
                $this->instance_id           = absint( $instance_id );
                $this->method_title          = __( 'Click Shipping Method' );
                $this->method_description    = __( 'Click Shipping' );
                $this->supports              = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                );
        
                $this->instance_form_fields = array(
                    'enabled' => array(
                        'title'         => __( 'Enable/Disable' ),
                        'type'          => 'checkbox',
                        'label'         => __( 'Enable this shipping method' ),
                        'default'       => 'yes',
                    ),
    
                    'test' => array(
                        'title'         => __( 'Prueba de array' ),
                        'description'   => $user->first_name,
                        'type' => 'select',
                        'options' => array( ''.$userinfo.'' => ''.$userinfo'',), 
                        'label' => __( 'Some field' ),
                        'required' => true,
                        )
    )
    );
                $this->enabled = $this->get_option( 'enabled' );
                $this->prueba = $this->get_option( 'prueba' ); 
    add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
            }
            /**
         * @param array $package (default: array())
         */
        public function calculate_shipping( $package = array() ) {
            $this->add_rate( array(
                'id'    => $this->id . $this->instance_id,
                'label' => $this->title,
                'cost'  => $this->test,
            ) );
        }
        }
2
  • You're telling it to create a single string and then assign that string into the array. Did you expect it to create an array? Commented Dec 6, 2020 at 15:25
  • I am traying to. I am really lost. With the code like that i get one option field with all the users together instead of one option foreach user. I tray tu make the foreach inside the array on the option part but doesnt work eather Commented Dec 6, 2020 at 16:09

1 Answer 1

2

This is the scope of your foreach()

foreach ( $users as $user ){
          $userinfo .= '['.$user->precio.','.$user->first_name.'] ';
        }

Nothing that is below will fall into this foreach() loop.

Sign up to request clarification or add additional context in comments.

2 Comments

I try tu put in the foreach in the option but throws back a sintax error
Provide the content of the error. Without it, it is not possible to comment or solve. and if you change your code you should provide both the change and the result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.