Updating product post meta data in admin meta box field - php

I am trying to update WooCommerce product meta data using update_post_meta() function, but it does''t work.
Here is my code:
function woo_add_deal_general_fields_save( $post_id ){
$post_id = (int)$post_id; // tried to convert into integer
$woocommerce_textarea = $_POST['_deal_textarea'];
if( !empty( $woocommerce_textarea ) )
if ( get_post_meta($post_id, '_deal_textarea', FALSE ) ) {
$test= update_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );
} else {
add_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );
}
var_dump($test);exit;
}
If I try it with a fixed product ID, it works:
$test= update_post_meta(70, '_deal_textarea', $woocommerce_textarea );
Why its not working with $post_id, (int)$post_id, & either get_the_ID();?
Here is the part of my code like function calls:
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_deal_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
$feature_product=get_post_meta(get_the_ID(), '_featured', true );
if($feature_product=='yes'){
echo '<div class="options_group">';
// Custom fields will be created here...
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_deal_textarea',
'label' => __( 'Deal Caption', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' )
)
);
echo '</div>';
}
}
Thanks

Here is your revisited tested and fully functional code, based on this answer:
// Inserting a Custom Admin Field in general tab products pages
add_action( 'woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field' );
function add_deal_custom_general_product_field() {
global $post;
$feature_product = get_post_meta( $post->ID, '_featured', true );
if( $feature_product == 'yes' ){
echo '<div class="options_group">';
woocommerce_wp_textarea_input( array(
'id' => '_deal_textarea',
'label' => __( 'Deal Caption', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' )
) );
echo '</div>';
}
}
// Saving the Custom Admin Field in general tab products pages when submitted
add_action( 'woocommerce_process_product_meta', 'save_deal_custom_general_product_field' );
function save_deal_custom_general_product_field( $post_id ){
$wc_field = $_POST['_deal_textarea'];
$feature_product = get_post_meta( $post_id, '_featured', true );
if( !empty($wc_field) && $feature_product == 'yes')
update_post_meta( $post_id, '_deal_textarea', esc_attr( $wc_field ) );
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works

I don't know where is the bug, but i simplified the code a bit. Please try it:
function woo_add_deal_general_fields_save( $post_id ){
$woocommerce_textarea = $_POST['_deal_textarea'];
if( !empty( $woocommerce_textarea ) ) {
$test = update_post_meta( $post_id, '_deal_textarea', $woocommerce_textarea );
var_dump( $test ); exit;
}
}

Related

Allow content with HTML tags for textarea custom fields in Woocommerce

I have one small problem that dont know how to resolve myself. I have this working function:
// Custom Field Product
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Text Before Add to Cart:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the before add to cart button text here.',
'woocommerce' )
)
);
woocommerce_wp_textarea_input(
array(
'id' => '_textarea1',
'label' => __( 'Text After Add to Cart:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the after add to cart button text here.',
'woocommerce' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
// Starting Save text
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
// Textarea2
$woocommerce_textarea = $_POST['_textarea1'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea1', esc_html(
$woocommerce_textarea ) );
}
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
// Print content to product
add_action( 'woocommerce_before_add_to_cart_form',
'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
// Echo content.
echo get_post_meta( get_the_ID(), '_textarea', true );
}
add_action( 'woocommerce_after_add_to_cart_form',
'add_content_after_addtocart_button_func1' );
function add_content_after_addtocart_button_func1() {
// Echo content.
echo get_post_meta( get_the_ID(), '_textarea1', true );
}
But when insert text like this:
<b>random text. bla bla</b>
into field , and save, all the text is lost, and nothing is shown in product ? So my question is... How to use valid HTML tags like bold, italic, color etc into custom field? Why all the text is lost after save the text with HTML tags?
EDIT : When replace this function:
update_post_meta( $post_id, '_textarea1', esc_html($woocommerce_textarea ));
with this:
update_post_meta( $post_id, '_textarea1', esc_textarea( $woocommerce_textarea ) );
print: <b>blabla</b>
but still nto formating tags as should be.
You should use instead dedicated wp_kses_post() function, that will "sanitize content for allowed HTML tags for post content".
So in your related hooked function code:
// Save custom fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
// Textarea 1
if( ! empty( $_POST['_textarea'] ) )
update_post_meta( $post_id, '_textarea', wp_kses_post( $_POST['_textarea'] ) );
// Textarea 2
if( ! empty( $_POST['_textarea1'] ) )
update_post_meta( $post_id, '_textarea1', wp_kses_post( $_POST['_textarea1'] ) );
}
This code goes on function.php file of your active child theme (or theme). Tested and works.

Add a drop down to product edit pages in product data "General" settings tab

I am trying to figure out how to modify the singe product options so the product admin can pick from the drop down list the condition of product, i.e. new/ used.
Below is a code that allows product admin to enter the condition of product manually.
// Enabling and Displaying Fields in backend
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_text_input( array( // Text Field type
'id' => '_Stan',
'label' => __( 'Stan', 'woocommerce' ),
'placeholder' => 'i.e: nowa; uzywana...',
'desc_tip' => 'true',
'description' => __( 'Podaj stan plyty.', 'woocommerce' )
) );
echo '</div>'; // Closing </div> tag HERE
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$Stan_field = $_POST['_Stan'];
if( !empty( $Stan_field ) )
update_post_meta( $post_id, '_Stan', esc_attr( $Stan_field ) );
}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45);
function woo_display_custom_general_fields_values() {
global $product;
echo '<p class="custom-Stan">Stan: ' . get_post_meta( $product->id, '_Stan', true ) . '</p>';
}
There is some errors and mistakes so I have revisited a little bit your code. Now you will have to replace woocommerce_wp_text_input() by woocommerce_wp_select() to get a select field instead, this way:
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_select( array( // Text Field type
'id' => '_Stan',
'label' => __( 'Stan', 'woocommerce' ),
'description' => __( 'Podaj stan plyty.', 'woocommerce' ),
'desc_tip' => true,
'options' => array(
'' => __( 'Select product condition', 'woocommerce' ),
'Nowa' => __('Nowa', 'woocommerce' ),
'Uzywana' => __('Uzywana', 'woocommerce' ),
)
) );
echo '</div>';
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields', 30, 1 );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$posted_field_value = $_POST['_Stan'];
if( ! empty( $posted_field_value ) )
update_post_meta( $post_id, '_Stan', esc_attr( $posted_field_value ) );
}
// Display In front end
add_action( 'woocommerce_product_meta_start', 'woo_display_custom_general_fields_values', 50 );
function woo_display_custom_general_fields_values() {
global $product;
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo '<span class="stan">Stan: ' . get_post_meta( $product_id, '_Stan', true ) . '</span>';
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
Is better to avoid capitals in meta keys and they should start with an underscore.

Saving custom field data from Admin WooCommerce product data metabox

In Woocommerce I use some custom fields for product specifications, and save the specifications in the post_meta.
I'm trying to make an if loop to write down in the post_meta another product specification.
The code I now use is:
add_action( 'woocommerce_product_options_general_product_data', 'BTW_field' );
function BTW_field() {
woocommerce_wp_radio(
array(
'id' => '_BTW',
'default' => '21% BTW',
'required' => true,
'options' => array(
'Prijs is incl. 21% BTW' => '21% BTW',
'Margeproduct' => 'Marge Product',
)
)
);
}
add_action( 'woocommerce_process_product_meta', 'BTW_save' );
function BTW_save( $post_id ){
$BTW = $_POST['_BTW'];
if( !empty( $BTW ) )
update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
}
An now I try to rewrite the BTW_save function so it will save another post_meta.
function BTW_save( $post_id ){
$BTW = $_POST['_BTW'];
if( !empty( $BTW ) ){
update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
}
if ($BTW == "Margeproduct (vrijgesteld van BTW)"){
$BTW2 = "Margeproduct*"
} else {
$BTW2 = "21%"
}
update_post_meta( $post_id, '_BTW_NAME', esc_attr( $BTW2 ) );
}
I don't know how I can check if $BTW is equal to the post_meta _BTW and how I can rewrite it so $BTW2 will also save in the post meta as _BTW_NAME.
Updated: As you are setting 2 different values, it could be better to use a select field instead.
Also I have make some changes in your code regarding correct variable naming and field keys naming (You should be able to rename them easily keeping in mind that lowercase and underscores are recommended).
Here is the code:
add_action( 'woocommerce_product_options_general_product_data', 'add_btw_field' );
function add_btw_field() {
global $post;
// Get the selected value
$value = get_post_meta( $post->ID, '_btw', true );
if( empty( $value ) ) $value = 'btw'; // Default value
woocommerce_wp_select( array(
'id' => 'btw_select',
'label' => __( 'BTW-prijsopties', 'woocommerce' ),
'options' => array(
'btw' => __( '21% BTW', 'woocommerce' ),
'marge' => __( 'Marge Product', 'woocommerce' ),
),
'value' => $value, // Displaying the selected value
) );
}
add_action( 'woocommerce_process_product_meta', 'save_btw_field' );
function save_btw_field( $post_id ){
if( empty( $_POST['btw_select'] ) ) return; // exit (in case of)
update_post_meta( $post_id, '_btw', esc_attr( $_POST['btw_select'] ) );
if ( $_POST['btw_select'] == 'btw' )
$label = __( 'BTW 21%', 'woocommerce' );
else
$label = __( 'Margeproduct (vrijgesteld van BTW)', 'woocommerce' );
update_post_meta( $post_id, '_btw_label', esc_attr( $label ) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get something like that:
By default when creating or updating a product, both custom fields will be saved for 'btw' option as product meta data…
You will be able to get both product post_meta custom fields values using get_post_meta():
// Set HERE the product ID (or get it dynamically)
$product_id = 37;
$btw = get_post_meta( $product_id, '_btw', true ); // Values can be 'yes' or 'no'
$btw_label = get_post_meta( $product_id, '_btw_label', true );
// Output (testing):
echo $btw_label;

Woocommerce product notes add to order meta

I created textarea field to save woocommerce products notes, i want to save these notes into admin order if any product available in cart and have product notes.
// WooCommerce Products Custom Field
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">';
// Textarea Field
woocommerce_wp_textarea_input(
array(
'id' => 'product_notes',
'label' => __( 'Product Notes', 'woocommerce' ),
'placeholder' => 'Enter product notes here.',
'desc_tip' => 'true',
'description' => __( 'Enter product notes here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Product notes
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['product_notes'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, 'product_notes', esc_html( $woocommerce_textarea ) );
}
Ok, i find solution.
add_action( 'woocommerce_checkout_update_order_meta', 'custom_product_notes_order_meta', 10, 2 );
function custom_product_notes_order_meta( $order_id ) {
global $woocommerce;
$i=1; //product counter
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
$product_note = 'product_notes_'.$i++;
if( !empty(get_post_meta( $product_id, 'product_notes', true )) ){
$product_notes = get_post_meta( $product_id, 'product_notes', true );
add_post_meta( $order_id, $product_note, $product_notes );
}
}
}

WordPress Redirect Trouble

I'm hoping someone could explain a way to use a variable within the wp_redirect() function? Thanks in advance.
There are two variables:
wp_redirect( $location, $status );
$location is the absolute URI which the user will be redirected to.
$status is the status code to use (eg, 301). Default is 302 (so you can leave it blank if you want).
Your code is not storing value of url and even not getting in add_to_cart_redirect function.Try below code
//* Add/Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'rv_woo_add_custom_general_fields' );
function rv_woo_add_custom_general_fields() {
global $post_id;
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_rv_woo_product_custom_redirect_url',
'label' => __( 'Redirect on Add to Cart', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter a URL to redirect the user to after this product is added to the cart.', 'woocommerce' ) ,
'value' => get_post_meta($post_id,'c',true)
)
);
echo '</div>';
}
//* Save Fields
add_action( 'woocommerce_process_product_meta', 'rv_woo_add_custom_general_fields_save' );
function rv_woo_add_custom_general_fields_save( $post_id ){
$rv_woo_redirect_url = get_post_meta($post_id,'c',true);
if( !empty( $rv_woo_redirect_url ) ) {
update_post_meta( $post_id, 'c', esc_url( $rv_woo_redirect_url ) );
}
}
add_filter('add_to_cart_redirect', 'redirect_elsewhere');
function redirect_elsewhere() {
global $woocommerce, $post;
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$rv_woo_redirect_url = get_post_meta($product_id,'c',true);
if( !empty( $rv_woo_redirect_url ) ) {
wp_redirect( $rv_woo_redirect_url );
exit;
}
}

Categories