Title: Help with resolving snippet function
Last modified: August 7, 2023

---

# Help with resolving snippet function

 *  Resolved [Royah Marie](https://wordpress.org/support/users/hautecreations/)
 * (@hautecreations)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/help-with-resolving-snippet-function/)
 * Hi. I’m trying to add a Code Snippet which changes my drop down menus to input
   fields. It was recommended that I reach out to you on how to work around the 
   issue. Here is the code:
 * `function woocommerce_variable_add_to_cart(){ global $product, $post; $variations
   = find_valid_variations(); // Check if the special 'price_grid' meta is set, 
   if it is, load the default template: if ( get_post_meta($post->ID, 'price_grid',
   true) ) { // Enqueue variation scripts wp_enqueue_script( 'wc-add-to-cart-variation');//
   Load the template wc_get_template( 'single-product/add-to-cart/variable.php',
   array( 'available_variations' => $product->get_available_variations(), 'attributes'
   => $product->get_variation_attributes(), 'selected_attributes' => $product->get_variation_default_attributes()));
   return; } // Cool, lets do our own template! ?> <table class="variations variations-
   grid" cellspacing="0"> <tbody> <?php foreach ($variations as $key => $value) {
   if( !$value['variation_is_visible'] ) continue; ?> <tr> <td> <?php foreach($value['
   attributes'] as $key => $val ) { $val = str_replace(array('-','_'), ' ', $val);
   printf('<span class="attr attr-%s">%s</span>', $key, ucwords($val));} ?> </td
   > <td> <?php echo $value['price_html'];?> </td> <td> <?php if( $value['is_in_stock']){?
   > <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() );?
   >" method="post" enctype='multipart/form-data'> <?php woocommerce_quantity_input();?
   > <?php if(!empty($value['attributes'])){ foreach ($value['attributes'] as $attr_key
   => $attr_value) { ?> <input type="hidden" name="<?php echo $attr_key?>" value
   ="<?php echo $attr_value?>"> <?php } } ?> <button type="submit" class="single_add_to_cart_button
   btn btn-primary"><span class="glyphicon glyphicon-tag"></span> Add to cart</button
   > <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?
   >" /> <input type="hidden" name="product_id" value="<?php echo esc_attr( $post-
   >ID ); ?>" /> <input type="hidden" name="add-to-cart" value="<?php echo esc_attr(
   $post->ID ); ?>" /> </form> <?php } else { ?> <p class="stock out-of-stock"><?
   php _e( 'This product is currently out of stock and unavailable.', 'woocommerce');?
   ></p> <?php } ?> </td> </tr> <?php } ?> </tbody> </table> <?php } function find_valid_variations(){
   global $product; $variations = $product->get_available_variations(); $attributes
   = $product->get_attributes(); $new_variants = array(); // Loop through all variations
   foreach( $variations as $variation ) { // Peruse the attributes. // 1. If both
   are explicitly set, this is a valid variation // 2. If one is not set, that means
   any, and we must 'create' the rest. $valid = true; // so far foreach( $attributes
   as $slug => $args ) { if( array_key_exists("attribute_$slug", $variation['attributes'])&&!
   empty($variation['attributes']["attribute_$slug"]) ) { // Exists } else { // 
   Not exists, create $valid = false; // it contains 'anys' // loop through all 
   options for the 'ANY' attribute, and add each foreach( explode( '|', $attributes[
   $slug]['value']) as $attribute ) { $attribute = trim( $attribute ); $new_variant
   = $variation; $new_variant['attributes']["attribute_$slug"] = $attribute; $new_variants[]
   = $new_variant; } } } // This contains ALL set attributes, and is itself a 'valid'
   variation. if( $valid ) $new_variants[] = $variation; } return $new_variants;}`
 * Ok so that pasted horribly. Here is the link to the code: [https://gist.github.com/Babylon1999/09150556b9e1c28ca8408194397a6c45](https://gist.github.com/Babylon1999/09150556b9e1c28ca8408194397a6c45)
 * The error is:
 * **Snippet automatically deactivated due to an error on line 2:**
 * > Cannot redeclare function woocommerce_variable_add_to_cart.
 * Any help you can offer is greatly appreciated.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhelp-with-resolving-snippet-function%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  [Yordan Soares](https://wordpress.org/support/users/yordansoares/)
 * (@yordansoares)
 * [2 years, 8 months ago](https://wordpress.org/support/topic/help-with-resolving-snippet-function/#post-17069781)
 * Hi [@hautecreations](https://wordpress.org/support/users/hautecreations/),
 * As the error states, you cannot use the same name for two functions, and this
   is already declared on WooCommerce ([source](https://github.com/woocommerce/woocommerce/blob/bdbd590819d2e059e4e2b63560197d95fbb40b37/plugins/woocommerce/includes/wc-template-functions.php#L1741-L1760)),
   which seems to be running before yours, although if you manage to runs yours 
   before, the one from WooCommerce will not be executed.
 * At this point, my recommendation is to run your custom function within a _must
   use plugin_ ([see](https://wpmudev.com/blog/wordpress-mu-plugins/)) which have
   a high priority in the WordPress loading sequence ([see](https://wp-kama.com/handbook/wordpress/loading)).
 * Hope it helps!

Viewing 1 replies (of 1 total)

The topic ‘Help with resolving snippet function’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Yordan Soares](https://wordpress.org/support/users/yordansoares/)
 * Last activity: [2 years, 8 months ago](https://wordpress.org/support/topic/help-with-resolving-snippet-function/#post-17069781)
 * Status: resolved