0

I'm trying to find if it's possible to dynamically generate functions and hooks. All functions do the same thing, and all hooks are essentially the same.

Here's an example of a hook/function combo:

add_action( 'woocommerce_single_product_summary', 'cx_inputKVA_show', 5 );
function cx_inputKVA_show() {
    global $product;
    // Do not show this on variable products
    if ( $product->product_type <> 'variable' ) {
        $inputKVA = get_post_meta( $product->id, 'cx_inputKVA', true );
        if ( $inputKVA <> '' ) {
            echo '<div class="woocommerce_msrp">';
            _e( 'Input KVA: ', 'woocommerce' );
            echo '<span class="woocommerce-cx_inputKVA">' . _e( $inputKVA ) . '</span>';
            echo '</div>';
        }
    }
}

I want to use an array like:

$fieldset = array(
    array(
        1 => 'slug',
        2 => 'title'
    )
    array(
        1 => 'slug',
        2 => 'title'
    )
);

And then use a foreach() loop to generate 3 hooks and 3 functions for each nested array. Before, I was just writing each one separately, but now I need to generate for 23 items and it's too much code, too messy to change.

It would make the hook like:

add_action( 'woocommerce_single_product_summary', 'cx_[plug]_show', 5 );

and the function like

function cx_inputKVA_show() {
    global $product;
    // Do not show this on variable products
    if ( $product->product_type <> 'variable' ) {
        $[plug] = get_post_meta( $product->id, 'cx_[plug]', true );
        if ( $[plug] <> '' ) {
            echo '<div class="woocommerce_msrp">';
            _e( '[title]: ', 'woocommerce' );
            echo '<span class="woocommerce-cx_[plug]">' . _e( $[plug] ) . '</span>';
            echo '</div>';
        }
    }
}

Answer------------------ Since Milo is a genius, I got this done much simpler by just looping through the array inside of the function, instead of trying to generate a new function dynamically for each item. The array contains 'slug' and 'title' attributes, and here's the function:

function cx_product_field() {
    global $fieldset;
    foreach ( $fieldset as $field ){
        woocommerce_wp_text_input(
            array( 
                'id' => 'cx_'.$field['slug'], 
                'class' => 'wc_input_text', 
                'label' => __( $field['title'], 'woocommerce' )
            ) 
        );
    }
}
4
  • 3
    maybe I'm missing the point here, but why do you need 23 identical functions rather than looping over an array of data in a single hooked function?
    – Milo
    Commented Jun 17, 2014 at 4:04
  • I see your point. Let me give that a go. Occam's razor and all that, lol.
    – user54312
    Commented Jun 17, 2014 at 5:38
  • yeah, thanks Milo. updated my post with the solution. If you want to submit your comment as an answer, I'll select it.
    – user54312
    Commented Jun 17, 2014 at 5:48
  • 4
    Please post your solution as an answer, and return to this page in two days to accept your own answer :-) Commented Jun 17, 2014 at 6:05

1 Answer 1

1

Since Milo is a genius, I got this done much simpler by just looping through the array inside of the function, instead of trying to generate a new function dynamically for each item. The array contains 'slug' and 'title' attributes, and here's the function:

function cx_product_field() {
    global $fieldset;
    foreach ( $fieldset as $field ){
        woocommerce_wp_text_input(
            array( 
                'id' => 'cx_'.$field['slug'], 
                'class' => 'wc_input_text', 
                'label' => __( $field['title'], 'woocommerce' )
            ) 
        );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.