I have 2 custom fields called 'Rating' and 'Count' in a custom field group assigned to a Custom post type.
I want to update the 'Rating' and 'Count' fields whenever a user submits a new rating.
The desired formula for updating the 'Rating' field is 'Rating = (Rating * Count + New Rating Value Given By User)/(Count+1)',
and for the 'Count' field, 'Count = Count + 1'.
Solution:
Add the following code to your functions.php or custom plugin file:
// Attach the 'update_rating_and_count' function to the 'cred_before_save_data' hook
add_action('cred_before_save_data', 'update_rating_and_count', 10, 2);
// Define the 'update_rating_and_count' function
function update_rating_and_count($form_data) {
// Check if the form submitted has the correct ID (Replace 600 with your Toolset form ID)
if ($form_data['id'] == 600) {
// Check if the '_cred_cred_prefix_post_id' field exists in the form data
if (isset($_POST['_cred_cred_prefix_post_id'])) {
// Get the post ID from the form data
$post_id = $_POST['_cred_cred_prefix_post_id'];
// Get the current rating and count values from the post meta
$rating = (float) get_post_meta($post_id, 'wpcf-rating', true);
$count = (int) get_post_meta($post_id, 'wpcf-count', true);
// Get the new rating value submitted by the user
$new_rating = (float) $_POST['wpcf-rating'];
// Calculate the new average rating value using the provided formula
$new_rating_value = ($rating * $count + $new_rating) / ($count + 1);
// Update the submitted rating value with the new average rating value
$_POST['wpcf-rating'] = $new_rating_value;
// Increment the count by 1
$new_count = $count + 1;
// Update the count value in the post meta
update_post_meta($post_id, 'wpcf-count', $new_count);
}
}
}
Replace 600 with your Toolset form ID, and ensure the field names in the code match your setup.
This code attaches the 'update_rating_and_count' function to the 'cred_before_save_data' hook.
When a form with the specified ID (in this case, 600) is submitted, the function retrieves the current rating and count values for the post being edited.
It then calculates the new average rating value based on the user's submitted rating and updates the rating and count values in the post meta.
The user wanted to display a view of products according to the ones which are currently in the cart on the checkout page. Although not possible using native Toolset tools, custom PHP code can be used to achieve this.
Solution:
- Utilize WooCommerce hooks to get the list of products currently in the cart.
- Use the following custom PHP code and add it to your website:
function wpc_elementor_shortcode( $atts ) {
// Get full cart object
$items = WC()->cart->get_cart();
// Initialize array to store product IDs
$product_ids = array();
// Loop through cart items
foreach( $items as $item => $values ) {
// Load product object
$product = wc_get_product( $values['data']->get_id() );
// Access product data using product object
$product_id = $product->get_id();
// Add product ID to array
$product_ids[] = $product_id;
}
// Join product IDs into comma-separated string
$product_ids_string = implode( ', ', $product_ids );
// Output comma-separated list of product IDs
echo $product_ids_string;
}
add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');
- Use the shortcode [my_elementor_php_output] to output the comma-separated list of product IDs currently in the cart.
This code uses [wpv-post-url] for the post URL and [wpv-post-title] for the post title. The target="_blank" attribute ensures the link opens in a new tab.
For CSS customization, apply styles to the "ptitle" class:
The user had a custom field group called 'cf' with a number custom field called 'count'. They had two custom post types called 'saas' and 'saas1', both with posts containing data in the 'count' custom field. After removing the 'cf' custom field group from 'saas1', the user still saw the 'count' value displayed on the frontend. They wanted to stop displaying the 'count' value without deleting it.
Solution:
Create a separate Elementor layout for 'saas' and 'saas1'. In the layout for 'saas', don't use the 'cf' custom field group. Continue to use 'cf' in the 'saas1' Elementor layout. This way, the 'count' values won't be displayed for the 'saas' post type while still being available for the 'saas1' post type.
Problem:
There are 3 Relevanssi Plugins. Which one should I install? Solution:
The main Relevanssi plugin is the first in your screenshot, so it would be this one, but you could also use the 'lite' version.
I am having an issue with loading multiple articles on the same page using a Load More button.
Instead of loading the next page below the first, the button opens a second page even though I have an infinite scroll set up.
Solution:
Remove the Pagination block and use the shortcode block with the following shortcode:
[wpv-pager-next-page force="true"][wpml-string context="wpv-views"]Load More Button here ...[/wpml-string][/wpv-pager-next-page]
Make sure to add the block inside the View Output block. If the issue persists, try creating a new view with the authors and check with the Block Editor instead of the classic view.