Skip to content

Instantly share code, notes, and snippets.

@amitramani
Created June 4, 2016 17:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amitramani/794f51b7a56bd4e4d884283dc98cce0e to your computer and use it in GitHub Desktop.
Save amitramani/794f51b7a56bd4e4d884283dc98cce0e to your computer and use it in GitHub Desktop.
[WooCommerce] How to add Custom Fields to Products
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => '_no_free_shipping_checkbox',
'wrapper_class' => '',
'label' => __('Exclude From Free Shipping', 'woocommerce' ),
'description' => __( 'Dis-allow Free Shipping', 'woocommerce' )
)
);
woocommerce_wp_checkbox(
array(
'id' => '_discontinued_product_checkbox',
'wrapper_class' => '',
'label' => __('Discontinued Product', 'woocommerce' ),
'description' => __( 'No longer in Production', 'woocommerce' )
)
);
woocommerce_wp_checkbox(
array(
'id' => '_custom_product',
'wrapper_class' => '',
'label' => __('Custom Order Product', 'woocommerce' ),
'description' => __( 'Product can be customized', 'woocommerce' )
)
);
echo '</div>';
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
// Checkbox
$woocommerce_checkbox = isset( $_POST['_no_free_shipping_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_no_free_shipping_checkbox', $woocommerce_checkbox );
$woocommerce_product_checkbox = isset( $_POST['_discontinued_product_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_discontinued_product_checkbox', $woocommerce_product_checkbox );
$woocommerce_custom_product_checkbox = isset( $_POST['_custom_product'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_custom_product', $woocommerce_custom_product_checkbox );
}
add_filter('woocommerce_is_purchasable', 'ar_custom_is_purchasable', 10, 2);
function ar_custom_is_purchasable( $is_purchasable, $object ) {
// get the product id first
$product_id = $object->get_id();
// get the product meta data
$is_custom = get_post_meta($product_id, '_custom_product', true);
if ($is_custom == "yes"){
return false;
}
else {
return true;
}
}
add_action( 'woocommerce_single_product_summary', 'ar_custom_product_cta', 60);
function ar_custom_product_cta()
{
global $product;
// get the product id first
$product_id = $product->get_id();
// get the product meta data
$is_custom = get_post_meta($product_id, '_custom_product', true);
// Show the Form if product is Custom
if ($is_custom == "yes"){
echo '<h5> Questions? We respond in minutes! </h5>';
echo do_shortcode( '[contact-form-7 id="19502" title="Custom Product Inquiry"]' );
}
}
@amitramani
Copy link
Author

Hi @waynep16
That would be via custom code. It will just be an extension of this code. You would check if cart/checkout page, iterate through the cart items, and show the corresponding meta data for each product.

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