ACF select/dropdown field values based on other true/false fields - wordpress

I struggle a bit in creating a piece of code, that will have an impact on Advanced Custom Fields within my wordpress instance.
What is the way it should work?
On the admin dashboard/backend, while creating a post a user has access to two tabs (genereated through ACF). In the first tab, there are three toggle buttons (true / false) called: fruits_selection, veggies_selection and items_selection. In the second tab there is a dropdown/select field called platform. I want to avoid providing 'fixed' list of values. Instead I wanted to leave it up to a user. So when he/she marks specific button, then on the select field those values should be visible.
So IF a user will set fruits_selection to TRUE, then the dropdown should have available values [value : label]:
apple-bundles : Mix of apples,
banana-bundles : Bananas basket,
kiwi-set : Tasty kiwi set,
IF a user will set veggies_selection to TRUE, then the dropdown should have available value: potato-bag : 10lbs Potato Bag
IF a user will set items_selection to TRUE, then the dropdown should have available value: basket : Wooden basket
Obviously, the more buttons are set to true, the more values will be available in the select field.
This is my current code:
function acf_load_platform_field_choices( $field ) {
// reset choices
$field['choices'] = array();
$choices = array();
$fruits = get_field('fruits_selection');
$veggies = get_field('veggies_selection');
$items = get_field('items_selection');
if( $fruits ) {
$choices = array_push('apple-bundles : Mix of apples', 'banana-bundles : Bananas basket', 'kiwi-set : Tasty kiwi set');
}
if( $veggies ) {
$choices = array_push('potato-bag : 10lbs Potato Bag');
}
if( $items ) {
$choices = array_push('basket : Wooden basket');
}
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=platform', 'acf_load_platform_field_choices');
Unfortunately, my current code doesn't work. It always makes the dropdown blank (no values) no matter of the button (true/false) selection. On top of that while trying to save it i get the info Updating failed. The response is not a valid JSON response.
So what im doing here wrong?

Related

FacetWP filter from custom field checkbox

I got a product custom field named organic with the values yes/no. I want to show a checkbox in our filter that says "Organic". And when checked search for yes values in db. The deafult setup of a checkbox in FacetWP shows the values from db.
How do I set that up in FacetWP?
here is your code for this task, do not forget to re-index after each name change.
/**
* reindex after adding or updating this filter
*/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'new_facet' == $params['facet_name'] ) { // change my_facet to your facet's name/slug
if ( '1' == $params['facet_value'] ) { // be careful with 1 and 0 as values - https://www.php.net/manual/en/types.comparisons.php
$params['facet_display_value'] = 'Organic'; // text to display in the facet choices
}
if ( '0' == $params['facet_value'] ) {
$params['facet_display_value'] = 'McDonalds';
}
}
return $params;
}, 10, 2 );

How set value of existing product attribute in Wordpress?

My every product in WooCommerce has several attributes. One of them is named my-availability and I need to change its value dynamically depending on stock change and some condition logic. So when the amount of pieces on stock is changed (usually decreased by order), the value of my-availability attribute will change. It should not add any new attributes, just change the value of the existing one. I do not use product variations at all.
I am total beginner and trying to build this piece of code for few days using a lot of googling. Now I have something like this:
function changeatt( $order )
{
$items = $order->get_items();
foreach( $items as $item ) {
$value2 = get_field( "naceste", $item['product_id']); //need to get value of custom field "naceste"
$value = get_post_meta( $item['product_id'], '_stock', true ); //need to get how many pcs is on stock
if ($value == 0 && $value2 > 0)
{$avl = 'Coming soon';}
if ($value == 0 && $value2 == 0)
{ $avl = 'Not in stock';}
if ($value > 0)
{ $avl = 'In stock';}
// update_post_meta( $item['product_id'], 'my-availability', $avl );
wp_set_object_terms( $item['product_id'], $avl, 'pa_my-availability', false);
}
}
add_action( 'woocommerce_reduce_order_stock', changeatt );
This code actually creates new custom field my-availability with the correct value, but I need to save it to existing attribute named my-availability instead. What am I missing?
I found it! update_post_meta did not work for changing attributes, do not know why. wp_set_object_terms must be used instead and pa_ must be added to the name of the attribute. I have edited the code in the question to the working version. The previous not-working line has been removed by // for other beginners, who will be solving the same situation to learn from this one. It took me few days to solve this.
Now I am pretty curious, how to find out, which types of variables can be changed using update_post_meta and which types using wp_set_object_terms.

Can't set the default value of some fields in checkout page

I want to put user's previous data at the checkout page fields by default in my website which based on woocommerce. I cant set value for first name, last name and phone fields, although I can set value for other fields. How can I solve this problem?
Looking at the WC_Checkout class (/includes/class-wc-checkout.php), it contains a get_value method that is responsible for the default methods in the form.
This method has a filter woocommerce_checkout_get_value that you can use to override the default value that is loaded for each field.
Here's an example that overrides both the first name (both billing and shipping) and the last name:
add_filter('woocommerce_checkout_get_value', function( $value, $input ) {
// first check if user is logged in
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
switch ($input) {
case 'billing_first_name':
case 'shipping_first_name':
$value = 'Jack'; // fixed default
break;
case 'billing_last_name':
case 'shipping_last_name':
$value = $current_user->user_lastname; // use lastname from user profile
break;
default:
// don't change if we don't have substituting data
break;
}
}
return $value;
},10 ,2 );
In the same method there's also a filter default_checkout_{form_field} that would let you check existing values first (the former is a short-circuit that prevents the normal default from loading), ex:
add_filter('default_checkout_billing_first_name', 'woocommerce_override_default_checkout_values', 10, 2 );
add_filter('default_checkout_billing_last_name', 'woocommerce_override_default_checkout_values', 10, 2 );
add_filter('default_checkout_shipping_first_name', 'woocommerce_override_default_checkout_values', 10, 2 );
add_filter('default_checkout_shipping_last_name', 'woocommerce_override_default_checkout_values', 10, 2 );
function woocommerce_override_default_checkout_values( $value, $input ) {
// first check if user is logged in
if ( is_user_logged_in() ) {
// only load if value is empty
if (empty($value)) {
// read your own
$value = '...';
}
}
return $value;
}

Get all values from Gravity forms multi select field from Entry object

On my Gravity form I have a number of multiple select fields (sets of checkboxes) and my function is using the gform_after_submission hook to get the data from the entry object to send off a request to an external API.
For the multiple select fields, how do I get a list of all selected options? I can see that there are entries like "4.1" => "Option A" but it strikes me as tedious to manually have to try every option to see if its listed or not. And I would assume that I'm just missing something in the documentation that would allow me to extract a list of all selected options either as an array or a comma-separated string or something like that.
Can anyone point me in the right direction?
You can retrieve a comma separated string containing the selected checkbox field choices by using the GF_Field::get_value_export() method which was added in Gravity Forms 1.9.13. Here's an example:
$field_id = 4;
$field = GFFormsModel::get_field( $form, $field_id );
$field_value = is_object( $field ) ? $field->get_value_export( $entry ) : '';
The above would return the values for the selected choices, if you wanted to return the choice text you would set the third parameter of get_value_export() to true e.g.
$field_value = is_object( $field ) ? $field->get_value_export( $entry, $field_id, true ) : '';

Get gravity forms fields

I am using the gravity form on my site. I am working on create the custom report for this I have need gravity form fields name and id based on specific form id.Please let me know how I can do it.
I am using the following function but it is showing all forms info based on it. it is looking very hard to parse it. Please let me know any function so I can get fields name easily.
$forms = RGFormsModel::get_forms_by_id(13);
try this
function get_all_form_fields($form_id){
$form = RGFormsModel::get_form_meta($form_id);
$fields = array();
if(is_array($form["fields"])){
foreach($form["fields"] as $field){
if(isset($field["inputs"]) && is_array($field["inputs"])){
foreach($field["inputs"] as $input)
$fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
}
else if(!rgar($field, 'displayOnly')){
$fields[] = array($field["id"], GFCommon::get_label($field));
}
}
}
//echo "<pre>";
//print_r($fields);
//echo "</pre>";
return $fields;
}
It's not that hard to parse:
$fields=array();
foreach ( $forms['fields'] as $field ) {
$fields[]=array($field['id'] => $field['inputName']);
}
P.S. I'm assuming you use Gravity Forms < 1.7 as RGFormsModel::get_forms_by_id is a deprecated function since 1.7
// Get the Form fields
$form = RGFormsModel::get_form_meta($form_id);
// Run through the fields to grab an object of the desired field
$field = RGFormsModel::get_field( $form, $field_id );
I use the above to get a specific field I want to filter the value of. The $field contains an object with all the properties you want.
echo $field->label // Gets the label
echo $field->inputName // Gets the name
echo $field->type // Gets the type
echo $field->cssClass // Gets the CSS Classes as a string
You are able to get the entered value/content of a field by using rgpost() and by referencing the id ($field->id).
// Check the entered value of every field
foreach( $form['fields'] as &$field ) {
// Get the content for the specific field
$fieldContent = rgpost( "input_".$field->id );
// Check the content
if ( $fieldContent == ... ){}
}

Resources