I created a custom field in woocommerce in the general tab, as you can see here
custom textarea
I need to create the text domain for the text I will insert in the textarea , to translate it with WPML string.
I tried inserting the text domain in the placeholder
'placeholder' => __('inserire tipo calzabilità', 'woocommerce')
but it doesn't work!
What's wrong?
below the code
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
//Custom Product Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_custom_product_textarea',
'placeholder' => 'inserire tipo calzabilità',
'label' => __('Campo calzabilità', 'woocommerce')
)
);
echo '</div>';
}
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Textarea Field
$woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
if (!empty($woocommerce_custom_procut_textarea))
update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
}
add_action( 'flatsome_custom_single_product_1', 'dancin_fit', 20 );
function dancin_fit() {
echo '<div class="fit"><span>' . get_post_meta(get_the_ID(), '_custom_product_textarea', true) .'</span></div>';
}
Related
How to add a custom button before Add to Cart form on WooCommerce Product Page? and I want different links for different products.
My PHP Code:
add_action( 'woocommerce_before_add_to_cart_form', 'learnalwayss_after_add_to_cart_btn' );
function learnalwayss_after_add_to_cart_btn() {
global $product;
if ( $product->get_id() == 149685 ) {
echo '<a class="button primary is-small box-shadow-4 box-shadow-2-hover" style="border-radius: 10px; background-color:#FFDB58 !important; color:black;" target="_blank" href="https://myurls.bio/learn_alwayss">Our Links</a>';
}
}
Your solution is simple and effective but you have to add every url manually in the source code.
The solution I am suggesting uses wp_postmeta to store the label and url of the button. This way, a regular user can add/change the value of the url instead of asking a developer to make every change.
We'll add 2 custom fields to the product page to store these values into metadata.
To add custom fields for the custom_button_url and custom_button_text metadata to the edit product page in WooCommerce, you can use the woocommerce_product_options_general_product_data hook to add the custom fields to the product data panel, and the woocommerce_process_product_meta hook to save the custom field values when the product is saved.
function custom_button_product_data_fields() {
global $post;
// Get the values of the "custom_button_url" and "custom_button_text" custom fields
$custom_button_url = get_post_meta( $post->ID, 'custom_button_url', true );
$custom_button_text = get_post_meta( $post->ID, 'custom_button_text', true );
// Add a custom field for the "custom_button_url" metadata
woocommerce_wp_text_input( array(
'id' => 'custom_button_url',
'label' => __( 'Custom Button URL', 'your-text-domain' ),
'placeholder' => 'http://',
'description' => __( 'Enter the URL for the custom button.', 'your-text-domain' ),
'type' => 'url',
'value' => $custom_button_url,
'custom_attributes' => array(
'pattern' => 'https?://.+',
),
) );
// Add a custom field for the "custom_button_text" metadata
woocommerce_wp_text_input( array(
'id' => 'custom_button_text',
'label' => __( 'Custom Button Text', 'your-text-domain' ),
'placeholder' => '',
'description' => __( 'Enter the text for the custom button.', 'your-text-domain' ),
'type' => 'text',
'value' => $custom_button_text,
) );
}
add_action( 'woocommerce_product_options_general_product_data', 'custom_button_product_data_fields' );
And to save the values into the wp_postmeta you can use woocommerce_process_product_meta hook
function save_custom_button_product_data_fields( $post_id ) {
// Save the value of the "custom_button_url" custom field
$custom_button_url = isset( $_POST['custom_button_url'] ) ? sanitize_text_field( $_POST['custom_button_url'] ) : '';
update_post_meta( $post_id, 'custom_button_url', $custom_button_url );
// Save the value of the "custom_button_text" custom field
$custom_button_text = isset( $_POST['custom_button_text'] ) ? sanitize_text_field( $_POST['custom_button_text'] ) : '';
update_post_meta( $post_id, 'custom_button_text', $custom_button_text );
}
add_action( 'woocommerce_process_product_meta', 'save_custom_button_product_data_fields' );
Now we only have to display the button using the woocommerce_before_add_to_cart_form hook. You can use any other hook on the product page to show the button in a different place.
function custom_button_before_add_to_cart() {
global $product;
$product_id = $product->get_id();
// Get the values of the "custom_button_url" and "custom_button_text" custom fields
$custom_button_url = get_post_meta( $product_id, 'custom_button_url', true );
$custom_button_text = get_post_meta( $product_id, 'custom_button_text', true );
// If the "custom_button_url" custom field has a value, show the button
if ( ! empty( $custom_button_url ) ) {
// If the "custom_button_text" custom field has a value, use it as the button text
if ( ! empty( $custom_button_text ) ) {
$button_text = $custom_button_text;
} else {
// If the "custom_button_text" custom field is empty, use the default button text
$button_text = __( 'Default Button Text', 'your-text-domain' );
}
// Use the value of the "custom_button_url" custom field as the button link
$button_link = $custom_button_url;
echo '' . esc_html( $button_text ) . '';
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'custom_button_before_add_to_cart' );
This code enables you to use WordPress or ACF custom fields to add unique button links.
add_action( 'woocommerce_before_add_to_cart_form', 'button_custom_field' );
function button_custom_field() {
$wp = get_post_meta( get_the_ID(), 'wp_button', true );
$acf = class_exists('acf') ? get_field('acf_button') : '';
$field = $acf ? $acf : $wp;
if ( $field && is_singular('product') ) {
printf( '' . __( 'Custom Button' ) . '', $field );
}
}
Note : I wouldn't add the styling inline. Better to add it in your child themes stylesheet. Source
I'm using the following code in my theme functions.php file to add a additional data field to the product inventory tab:
// Add Custom Field to woocommerce inventory tab for product
add_action('woocommerce_product_options_inventory_product_data', function() {
woocommerce_wp_text_input([
'id' => '_number_in_package',
'label' => __('Number of Pages', 'txtdomain'),
'type' => 'number',
]);
});
add_action('woocommerce_process_product_meta', function($post_id) {
$product = wc_get_product($post_id);
$num_package = isset($_POST['_number_in_package']) ? $_POST['_number_in_package'] : '';
$product->update_meta_data('_number_in_package', sanitize_text_field($num_package));
$product->save();
});
add_action('woocommerce_product_meta_start', function() {
global $post;
$product = wc_get_product($post->ID);
$num_package = $product->get_meta('_number_in_package');
if (!empty($num_package)) {
printf('<div class="custom-sku">%s: %s</div>', __('Number of Pages', 'txtdomain'), $num_package);
}
});
add_filter('woocommerce_product_data_tabs', function($tabs) {
$tabs['additional_info'] = [
'label' => __('Additional info', 'txtdomain'),
'target' => 'additional_product_data',
'class' => ['hide_if_external'],
'priority' => 25
];
return $tabs;
});
However, on the single product page, the custom field is added before category and ISBN. I want to place the custom field at the end after the product ISBN. Any advice?
Some comments/suggestions regarding your code attempt
To save fields you can use the woocommerce_admin_process_product_object hook, opposite the outdated woocommerce_process_product_meta hook
WooCommerce contains by default no ISBN field, but it looks like the woocommerce_product_meta_end hook will answer your question
So you get:
// Add custom field
function action_woocommerce_product_options_inventory_product_data() {
woocommerce_wp_text_input( array(
'id' => '_number_in_package',
'label' => __( 'Number of Pages', 'woocommerce' ),
'description' => __( 'This is a custom field, you can write here anything you want.', 'woocommerce' ),
'desc_tip' => 'true',
'type' => 'number'
) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data' );
// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset
if ( isset( $_POST['_number_in_package'] ) ) {
// Update
$product->update_meta_data( '_number_in_package', sanitize_text_field( $_POST['_number_in_package'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// Display on single product page
function action_woocommerce_product_meta_end() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get meta
$number = $product->get_meta( '_number_in_package' );
// NOT empty
if ( ! empty ( $number ) ) {
echo '<p>' . $number . '</p>';
}
}
}
add_action( 'woocommerce_product_meta_end', 'action_woocommerce_product_meta_end', 10 );
I've created a custom field for my products this way:
// The code for displaying WooCommerce Product Custom Fields
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields' );
// Following code Saves WooCommerce Product Custom Fields
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save' );
function woocommerce_product_custom_fields () {
global $woocommerce, $post;
echo '<div class=" product_custom_field ">';
// Custom Product Number Field
woocommerce_wp_text_input(
array(
'id' => '_shipping_days_field',
'placeholder' => 'Días de entrega',
'label' => __('Días estimados de entrega', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '1'
)
)
);
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id){
// Custom Product Number Field
$shipping_days = $_POST['_shipping_days_field'];
if (!empty($shipping_days))
update_post_meta($post_id, '_shipping_days_field', esc_attr($shipping_days));
}
This is working ok on the frontend admin it shows the field and saves it, but it is not working for the CSV impornt, I cannot specify the field in my CSV mapping. Is there a way I can create custom field that works for CSV import as well?
I actually solved it, on the mapping you just should select Meta Attributes, and the label on the CSV column must be as follows:
meta:your_custom_field
I have a small problem
I wrote a simple shortcode function to display my acf value in the grid of the visual grid composer, this is my simple custom shortcode
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_say_hello'] = array(
'name' => __( 'Say Hello', 'my-text-domain' ),
'base' => 'vc_say_hello',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Just outputs Hello World', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
if( get_field('item') ):
the_field('item');
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
When I call the shortcode in the builder, "ITEM Locked" is displayed, even if the value of the element is set in the post !!!
Looks like get_field doesn't know where to get it from in the shortcode. Try adding the current post id as the second parameter
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
$id = get_the_ID();
if( get_field('item', $id) ):
the_field('item', $id);
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
I have added some custom fields in woocommerce when adding product. Now i want to display those custom fields in product listing page ( wp-admin/edit.php?post_type=product ). I also want to quick edit and save like other values can be edit and save in listing page.
I tried bellow code but it did not work.
add_filter( 'manage_edit-product', 'show_product_order' );
function show_product_order($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['product_order'] = 'Product Order';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
I also tried below code but it did not worked too.
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_general_product_data_custom_field' );
function woocommerce_general_product_data_custom_field() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => 'product_order',
'wrapper_class' => 'checkbox_class',
'label' => __('Order for Product', 'woocommerce' ),
'description' => __( 'Order for Product', 'woocommerce' )
)
);
echo '</div>';
}
i also reffered this post WooCommerce show custom column but i did not succeed to get solution
Please help.