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.
Related
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.
In Woocommerce, I used jQuery to calculate a custom price on a single product pages, and now need to pass this value to the cart.
The desired behavior is to pass the new price retrieved from the hidden field to the cart item price.
Here is my actual code:
// Hidden input field in single product page
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11, 0 );
function custom_hidden_product_field() {
echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="">';
}
// The code to pass this data to the cart:
add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( ! empty( $_REQUEST['custom_price'] ) ) {
// Set the custom data in the cart item
$cart_item_data['custom_data']['custom_price'] = $_REQUEST['custom_price'];
$data = array( 'custom_price' => $_REQUEST['custom_price'] );
// below statement make sure every add to cart action as unique line item
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $data );
}
return $cart_item_data;
}
And check both $data and $cart_item_data to see that they both return the custom_price data that is calculated on the page.
However, I go to view cart, and the value of the line item is still 0.
I set a var equal to the WC()->session->set( 'custom_data', $data ); and then var_dump to check it, but this returns NULL which might just be what it returns, I'm not entirely sure because I've never used it.
I should also add that I have the regular_price in the product backend set to 0. When I erase this (and leave it blank) I get back the error:
Warning: A non-numeric value encountered in
C:\xampp\htdocs\my-transfer-source\wp-content\plugins\woocommerce\includes\class-wc-discounts.php on line 85
I'm wondering if I've missed something here, and if someone could lend some light onto this? Thanks
Update 2021 - Handling custom price item in mini cart
First for testing purpose we add a price in the hidden input field as you don't give the code that calculate the price:
// Add a hidden input field (With a value of 20 for testing purpose)
add_action( 'woocommerce_before_add_to_cart_button', 'custom_hidden_product_field', 11 );
function custom_hidden_product_field() {
echo '<input type="hidden" id="hidden_field" name="custom_price" class="custom_price" value="20">'; // Price is 20 for testing
}
Then you will use the following to change the cart item price (WC_Session is not needed):
// Save custom calculated price as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_price'] ) && ! empty( $_POST['custom_price'] ) ) {
// Set the custom data in the cart item
$cart_item_data['custom_price'] = (float) sanitize_text_field( $_POST['custom_price'] );
// Make each item as a unique separated cart item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
// For mini cart
add_action( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 2 );
function filter_cart_item_price( $price, $cart_item ) {
if ( isset($cart_item['custom_price']) ) {
$args = array( 'price' => floatval( $cart_item['custom_price'] ) );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price;
}
// Updating cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price', 30, 1 );
function change_cart_item_price( $cart ) {
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Set the new price
if( isset($cart_item['custom_price']) ){
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
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();
}
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;
}
}
I've added a couple of custom field columns to our Woocommerce orders list in the admin of WordPress using the methods below, but the sort is not working....
add_filter( 'manage_edit-shop_order_columns', 'my_wc_columns' );
function my_wc_columns($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['program_id'] = 'Program';
$new_columns['constituent_id'] = 'Constituent ID';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'my_wc_column_values', 2 );
function my_wc_column_values($column){
global $post;
if ( $column == 'program_id' ) {
$program = get_post_meta( $post->ID, '_program_id', true );
$program_title = get_the_title($program);
$column_val = (isset($program) && $program>0 ? $program_title : 'All');
echo '<span>' . my_programs_get_name( $column_val ) . ' (' . $program . ')</span>';
}
if ( $column == 'constituent_id' ) {
$consid = get_post_meta( $post->ID, 'constituent_id', true );
$column_val = (isset($consid) && $consid != "") ? $consid : "";
echo '<span>' . $column_val . '</span>';
}
}
// Make column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'my_wc_column_sort' );
function my_wc_column_sort( $columns ) {
$custom = array(
'program_id' => '_program_id',
'constituent_id' => 'constituent_id',
);
return wp_parse_args( $custom, $columns );
}
I expected to have an issue perhaps with the program name, since it is an id that needs to be translated via a custom function to a name, but neither column is sorting properly. The records change order after clicking their column titles, but I cannot tell how the sort is being done. The program is not sorting on name or ID and both are seem random but consistent. Keep in mind both fields are custom fields that may or may not have a value defined. How can I make this sortable?
Here's a good tutorial on custom sortable columns. After you register the column, you need to handle the actual sorting. Sadly, that part doesn't happen automagically. Untested, but adapted from the above tutorial:
add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
function manage_wp_posts_be_qe_pre_get_posts( $query ) {
/**
* We only want our code to run in the main WP query
* AND if an orderby query variable is designated.
*/
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
// If we're ordering by 'program_id'
case 'program_id':
// set our query's meta_key, which is used for custom fields
$query->set( 'meta_key', '_program_id' );
/**
* Tell the query to order by our custom field/meta_key's
* value
*
* If your meta value are numbers, change 'meta_value'
* to 'meta_value_num'.
*/
$query->set( 'orderby', 'meta_value' );
break;
}
}
}