I'm developing a WordPress plugin and I'm stuck on something.
I created two custom post types, the first one is "Clients" and the second one is "Actions clients".
In the "Clients" CPT, I added a ACF number field to store the number of available credits.
In the "Actions clients" CPT, I list different actions with credits assigned to each of them.
By adding a new client action, I want update the total of available credits in the first CPT. Like $totalCredits = $totalCredits - $actionCredits.
I tried different codes :
//UPDATING DATA ACF FIELDS
public function update_data_acf($post_id) {
$field_key = get_field('credits_disponibles');// credits available
// credits_available - credits number (of an action)
$value = get_field('credits_disponibles') - get_field('nombre_de_credits');
update_field( 'credits_disponibles', $value, $post_id );
return $field_key;
}
Or like this :
public function update_credits_acf( $post_id ) {
$post_type = get_post_type( $post_id );
$post_status = get_post_status( $post_id );
if ( $post_type == 'npluginclients' ) {
$args = array( 'post_type' => 'npluginclients');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$number_of_credits_available = get_field('credits_disponibles', the_ID());
$number_of_credits = count(get_field('nombre_de_credits', the_ID()));
$number_of_credits_left = $number_of_credits_available - $number_of_credits;
update_field('credits_disponibles', $number_of_credits_left, the_ID());
endwhile;
}
}
Then I'm using this (Boiler Plate Plugin Generator ):
$this->loader->add_action( 'acf/save_post', $plugin_admin, 'update_credits_acf' );
I'm probably making a lot of big mistakes, but I'm new in developing.
Thanks in advance !
My Answer:
"product" and the second one is "action". Each product has a number field called "quantity" and each action has a number field called "quantity_change".
I want to create a functionality where, when a new action is added, the quantity of the corresponding product is updated by adding the "quantity_change" of the action.
Here's a sample code snippet to achieve this:
php
function update_product_quantity_on_action_save( $post_id ) {
// Check if this is an action post
if ( get_post_type( $post_id ) == 'action' ) {
// Get the quantity_change value
$quantity_change = get_post_meta( $post_id, 'quantity_change', true );
// Get the product ID associated with this action
$product_id = get_post_meta( $post_id, 'product_id', true );
// Get the current quantity of the product
$current_quantity = get_post_meta( $product_id, 'quantity', true );
// Update the quantity of the product by adding the quantity_change of the action
update_post_meta( $product_id, 'quantity', $current_quantity + $quantity_change );
}
}
add_action( 'save_post', 'update_product_quantity_on_action_save' );
In this code snippet, we are hooking into the `save_post` action and checking if the saved post is of type "action". If it is, we retrieve the `quantity_change` value and the `product_id` associated with the action. We then get the current quantity of the product and update it by adding the `quantity_change` of the action.
Make sure to replace `'quantity_change'`, `'product_id'`, and `'quantity'` with the actual meta keys you are using in your custom post types.
I hope this helps! Let me know if you have any questions.
Rate this post
3 of 5 based on 6475 votesComments