Skip to content

Instantly share code, notes, and snippets.

@aschroder
Created October 10, 2011 02:14
Show Gist options
  • Save aschroder/1274493 to your computer and use it in GitHub Desktop.
Save aschroder/1274493 to your computer and use it in GitHub Desktop.
Hack Woocommerce to add product custom fields
<form action="<?php echo esc_url( $_product->add_to_cart_url() ); ?>" class="variations_form cart" method="post">
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ($attributes as $name => $options) :?>
<tr>
<td><label for="<?php echo sanitize_title($name); ?>"><?php echo $woocommerce->attribute_label($name); ?></label></td>
// Start custom code to add custom text field
<?php if(is_array($options) && $options[0] == "CUSTOM_TEXT") : ?>
<td> <input type="text" id="<?php echo esc_attr( sanitize_title($name) ); ?>" name="attribute_<?php echo sanitize_title($name); ?>"/></td>
<?php else: ?>
<td><select id="<?php echo esc_attr( sanitize_title($name) ); ?>" name="attribute_<?php echo sanitize_title($name); ?>">
<option value=""><?php echo __('Choose an option', 'woothemes') ?>&hellip;</option>
<?php if(is_array($options)) : ?>
<?php foreach ($options as $option) :
$option_term = get_term_by('slug', $option, $name);
if (!is_wp_error($option_term) && isset($option_term->name)) :
$term_name = $option_term->name;
else :
$term_name = $option;
endif;
?>
<?php echo '<option value="'.$option.'">'.$term_name.'</option>'; ?>
<?php endforeach; ?>
<?php endif;?>
</td>
<?php endif;?>
// End custom code
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="single_variation_wrap" style="display:none;">
<div class="single_variation"></div>
<div class="variations_button">
<input type="hidden" name="variation_id" value="" />
<div class="quantity"><input name="quantity" value="1" size="4" title="Qty" class="input-text qty text" maxlength="12" /></div>
<button type="submit" class="button alt"><?php _e('Add to cart', 'woothemes'); ?></button>
</div>
</div>
<div><input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" /></div>
<?php do_action('woocommerce_add_to_cart_form'); ?>
</form>
@aqeelashraf
Copy link

in which file I need to add the code block?

@101drums
Copy link

Could you please tell me where I need to add this code to get a custom text field in products page? Also is it possible include drop-downs? Also I need to have a list of countries and based on the country selected I need the networks to be listed. I want to know where the code for this goes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment