Skip to content

Instantly share code, notes, and snippets.

@jamesckemp
Created November 17, 2017 13:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesckemp/9dd19312a5f5bcd523277d99016d9f2e to your computer and use it in GitHub Desktop.
Save jamesckemp/9dd19312a5f5bcd523277d99016d9f2e to your computer and use it in GitHub Desktop.
Enable custom field for specific products
<?php
/**
* Output engraving field.
*/
function iconic_output_engraving_field() {
global $product;
if ( $product->get_id() !== 1741 ) {
return;
}
?>
<div class="iconic-engraving-field">
<label for="iconic-engraving"><?php _e( 'Engraving (10 characters)', 'iconic' ); ?></label>
<input type="text" id="iconic-engraving" name="iconic-engraving" placeholder="<?php _e( 'Enter engraving text', 'iconic' ); ?>" maxlength="10">
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'iconic_output_engraving_field', 10 );
// You can change the conditional on lines 7 - 9 above to include or exlude products.
// Remove it completely to enable the field to show on all products.
// Show on multiple hard-coded products
if ( $product->get_id() !== 1741 && $product->get_id() !== 1742 && $product->get_id() !== 1743 ) {
return;
}
// Exclude a product
if ( $product->get_id() === 1741 ) {
return;
}
// Show only if an ACF (Advanced Custom Fields) checkbox is ticked
// https://www.advancedcustomfields.com/
if ( ! get_field( 'show_engraving_field', $product->get_id() ) ) {
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment