• Hi.

    At ledlysp?rer.no I need to make some modification to woocommerce to add more product data. The code below works for making custom fields for the product data. But how to simplify that code? I have made three new fields in that code, but need ten, so simplifying it would be great to keep the size of functions.php down.

    Thanks for all help ??

    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
    
    function woo_add_custom_general_fields() {
    
    global $woocommerce, $post;
    // Text area
    woocommerce_wp_textarea_input(
    array(
    'id' => '_textarea',
    'label' => __( 'Beskrivelse', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter the custom value here.', 'woocommerce' )
    )
    );
    
    global $woocommerce, $post;
    // Text area2
    woocommerce_wp_textarea_input(
    array(
    'id' => '_textarea2',
    'label' => __( '?rstall', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter the custom value here.', 'woocommerce' )
    )
    );
    
    global $woocommerce, $post;
    // Text area3
    woocommerce_wp_textarea_input(
    array(
    'id' => '_textarea3',
    'label' => __( 'Tidsepoke', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter the custom value here.', 'woocommerce' ) )
    );
    }
    
    function woo_add_custom_general_fields_save( $post_id ){
    // Textarea
    $woocommerce_textarea = $_POST['_textarea'];
    if( !empty( $woocommerce_textarea ) )
    update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea ) );
    
    // Textarea2
    $woocommerce_textarea = $_POST['_textarea2'];
    if( !empty( $woocommerce_textarea ) )
    update_post_meta( $post_id, '_textarea2', esc_html( $woocommerce_textarea ) );
    
    // Textarea3
    $woocommerce_textarea = $_POST['_textarea3'];if( !empty( $woocommerce_textarea ) )update_post_meta( $post_id, '_textarea3', esc_html( $woocommerce_textarea ) );
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Give this a try. It is limited in that you can set custom labels, but will not be able to set custom placeholders & descriptions.

    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
    
    function woo_add_custom_general_fields() {
    
    	// Set fields array
    	$additional_woocommerce_fields = array( 'Beskrivelse', '?rstall', 'Tidsepoke', '', '', '', '', '', '', '' );
    
    	// Text area
    	foreach ( $additional_woocommerce_fields as $key => $field ) {
    		global $woocommerce, $post;
    
    		$args = array(
    			'id' => '_textarea' . ( $key + 1 ),
    			'label' => __( $field, 'woocommerce' ),
    			'placeholder' => '',
    			'desc_tip' => 'true',
    			'description' => __( 'Enter the custom value here.', 'woocommerce' )
    			);
    
    		woocommerce_wp_textarea_input( $args );
    	}
    
    }
    
    function woo_add_custom_general_fields_save( $post_id ){
    
    	for ( $i = 1; $i <= 10; $i++ ) {
    		$woocommerce_textarea = $_POST[ '_textarea' . $i ];
    		! empty( $woocommerce_textarea ) ? update_post_meta( $post_id, '_textarea' . $i, esc_html( $woocommerce_textarea ) ) : FALSE;
    	}
    
    }
    Moderator bcworkz

    (@bcworkz)

    It appears the only true variable is the label text. The ID varies, but that can be generated on the fly with a counter. Here is what to do in plain English. It’s organized so it can be directly converted to PHP code.

    1. Define an array containing all the label strings
    2. Declare the globals – only needs to be done once. Add a variable for label array size.
    3. Assign label array size to global
    4. Establish a counter initialized to 1
    5. Start a foreach loop on each label in the array
    6. In the loop, construct an ID using a base string and the counter value
    7. Call woocommerce_wp_textarea_input() supplying the current label and ID, Everything else is static and can be hardcoded.
    8. Increment counter
    9. End of loop

    In a similar vein loop through the $_POST fields and update the post meta by running the same code in a loop, only changing what’s needed between passes. Your code here needs a small adjustment, instead of checking if $woocommerce_textarea is empty, to avoid empty field errors, check if the current array key exists in $_POST. This time we use a for/next loop.

    1. Define the update meta function
    2. Declare label array size global. Use to establish the upper count of the for next loop.
    3. Start the for/next loop with the counter initialized to 1
    4. Build the ID from base string and counter value as was done before.
    5. Check if the ID array key exists in $_POST, if not continue.
    6. Call update_post_meta(). $_POST[$ID] can be passed directly to esc_html().
    7. Next loop, the loop structure increments the counter.
    8. End function definition.
    Thread Starter tyggis

    (@tyggis)

    Thanks a lot both ??

    UaMV, can you please give me the code to display that text area inputs on the single lysp?re product page?

    bcworkz, thanks to you too, that instructions is to advanced for me, hope someone else can enjoy them better ??

    It shouldn’t be much more than this …

    // Get the text from the database and echo
    echo get_option( '_textarea1' );

    You may want to customize the name of your saved options a bit more, as well. _textarea1 is somewhat general. It’s best to steer clear of general option names to reduce the potential for conflict. A general practice is to use a custom prefix.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Simplify code for functions.php’ is closed to new replies.