i try to show a specific meta_value based on post_id and meta_key in woocommerce order dashboard:
First i create a new column:
function mb_set_order_note_column( $columns ) {
$columns['parcel_delivery'] = __('DHL','TEXTDOMAIN');
return $columns;
}
add_filter( 'manage_shop_order_posts_columns', 'mb_set_order_note_column', 99 );
After i have tried to get the specific value from wp_postmeta database
function mb_show_order_note_columns( $column_name, $post_id ) {
switch ( $column_name ) {
case 'parcel_delivery':
$order = new WC_Order( $post_id );
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in' );
print $delivery ;
break;
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'mb_show_order_note_columns', 10, 2 );
this shows only the customer notes from wp_post database. how to change to get specific value...
Thx
I see your custom meta key is _parcel_delivery_opted_in. So to get this value from wp_postmeta with order id and meta key,
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in', true ); // like _billing_email meta
3rd parameter true will return single value, if you omit third parameter an array will be returned.
Related
I have some ACF user fields on posts where users can be selected, I would like to take those fields and combine them into another field. So basically the users in field 1 and field 2 show up in field 3. Here is what I have tried so far. The return format for the fields is user array.
add_action( 'acf/save_post',
'my_acf_save_post', 10 );
function my_acf_save_post( $post_id ) {
// Get value of field 1
$value1 = get_field( 'contractor',
$post_id );
// Get value of field 2
$value2 = get_field( 'architect',
$post_id );
update_field( 'project_user_select',
$value1 . $value2, $post_id );
}
I was able to get this to work using array_merge.
function my_acf_save_post_31( $post_id ) {
// Get new value of field 1
$value1 = get_field( project_manager', $post_id, false );
// Get new value of field 2
$value2 = get_field( 'architect', $post_id, false );
// Get new value of field 3
$value3 = get_field( 'contractor', $post_id, false );
$merge = array_merge($value1,$value2,$value3);
update_field( 'project_user_select', $merge, $post_id );// update
'other_field' with 'my_field' value
}
add_action( 'acf/save_post', 'my_acf_save_post_31', 10 );
If there is a possibility of one of the values being empty I would write it something like this:
// Get new value of field 1 or set value to null
if(!empty(get_field( 'project_manager' )) {
$value1 = get_field( project_manager', $post_id, false );
} else {
$value1 = '';
}
What this does is check if the field has entries, if it does it will assign the value to the variable $value. If the field does not have a value it will just declare the variable so when you try to add each of the $values to $merge it will still work because the variable has been declared.
In our store, during checkout for every customer we create account automatically if it doesn’t already exist with same email. Now I’m looking for a way to somehow display label tag or little notice on orders list if it comes from already existing customer (returning) because these orders are handled a little differently.
Does anybody have an idea how to do that?
Thanks in advance
this snippet adds a column to orders list page in woocommerce backend and checks if customer is returning or not
add_filter( 'manage_shop_order_posts_columns', 'shalior_wc_set_custom_edit_post_columns',99,1 );
function shalior_wc_set_custom_edit_post_columns($columns) {
$columns['is-returning'] = __( 'Is returning?', 'your_text_domain' );
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'shalior_wc_is_returning', 99, 2 );
function shalior_wc_is_returning( $column, $post_id ) {
switch ( $column ) {
case 'is-returning':
$order = new WC_Order( $post_id );
$user_id = $order->get_user_id();
$orders_count = wc_get_customer_order_count( $user_id );
echo $orders_count > 1 ? "Yes" : "NO" ;
break;
}
}
I have an unique project using woocommerce. I created a form with Formidable Forms to add variable products from the front-end. It's working well with the code below. I have several attributes that are created. If the attributes are Not blank, they show up correctly as a variation drop-down option.
My problem is when the attribute - for example "Color" is blank and not submitted. The variation "Color" attribute is still created and the value is blank and the drop-down is empty. I've been searching for days to solve this issue. How to not create a variation/attribute if it's not submitted.
I also tried to programically change the set_variation to '' if a custom field value is empty. But that didn't work.
The code was found here:
Auto add all product attributes when adding a new product in Woocommerce
add_action( 'save_post', 'create_product_attributes_variations', 80, 3 );
function create_product_attributes_variations( $post_id, $post, $update ) {
## --- Checking --- ##
if ( $post->post_type != 'product') return; // Only products
// Exit if it's an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Exit if it's an update
if( $update )
return $post_id;
// Exit if user is not allowed
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
## -- Set product as variable and set quote option --- ##
update_post_meta( $post_id, 'qwc_enable_quotes', 'on');
wp_set_object_terms( $post_id, 'variable', 'product_type', false );
## --- The Settings for your product attributes --- ##
$visible = ''; // can be: '' or '1'
$variation = '1'; // can be: '' or '1'
## --- The code --- ##
// Get all existing product attributes
global $wpdb;
$attributes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies" );
$position = 0; // Auto incremented position value starting at '0'
$data = array(); // initialising (empty array)
// Loop through each exiting product attribute
foreach( $attributes as $attribute ){
// Get the correct taxonomy for product attributes
$taxonomy = 'pa_'.$attribute->attribute_name;
$attribute_id = $attribute->attribute_id;
$term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));
$product_attribute = new WC_Product_Attribute();
// Set the related data in the WC_Product_Attribute object
$product_attribute->set_id( $attribute_id );
$product_attribute->set_name( $taxonomy );
$product_attribute->set_options( $term_ids );
$product_attribute->set_position( $position );
$product_attribute->set_visible( $visible );
$product_attribute->set_variation( $variation );
// Add the product WC_Product_Attribute object in the data array
$data[$taxonomy] = $product_attribute;
$position++; // Incrementing position
}
// Get an instance of the WC_Product object
$product = wc_get_product( $post_id );
// Set the array of WC_Product_Attribute objects in the product
$product->set_attributes( $data );
//Save main product to get its id
$id = $product->save();
///////
$variation = new WC_Product_Variation();
$variation->set_regular_price(5);
$variation->set_parent_id($id);
//Set attributes requires a key/value containing
// tax and term slug
$variation->set_attributes(array(
));
//Save variation, returns variation id
$variation->save();
}
After changing the field value to a new one, it does not save data. I tried update value via function update_field(), but not actions
$post_id = 197;
$value = update_field( 'my_field', 'my_value', $post_id );
You should use field key instead of field name,
try this code.
$post_id = 197;
//Repace field_56e683ab6265f with your field key
update_field( 'field_56e683ab6265f', 'my_value', $post_id );
Or You can also do with this
update_post_meta( $post_id , 'my_field', 'my_value' );
//Repace field_56e683ab6265f with your field key
update_post_meta( $post_id , '_my_field', 'field_56e683ab6265f' );
I am trying to display chained products meta on admin product column but it just seems to display array. What is the bug? Thanks guys!
// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
//add columns
$columns['chain_product'] = __( 'Custom','woocommerce'); // title
return $columns;
}
// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
global $post;
$chained_parent_id = empty( $variation ) ? $post->ID : $variation->ID;
// HERE get the data from your custom field (set the correct meta key below)
$chain_product = get_post_meta( $chained_parent_id, '_chained_product_detail', true );
switch ( $column )
{
case 'chain_product' :
echo $chain_product; // display the data
break;
}
}