skip to Main Content

Created a catalog via CPT (NOT WooCommerce) and each product has a meta field (via ACF) – Price (price)
Please tell me how you can massively (for all products), change this meta field price (price) to the required percentage (for example, 15%)

Ideally, I need to set the required percentage on the settings page and, if this setting is saved, the meta field price (price) for all products in the catalog will change

I tried

function change_price( )
{
    global $wpdb;
    $wpdb->query("UPDATE " . $wpdb->prefix . "postmeta SET meta_value = meta_value * 1.135 WHERE meta_key = 'price'");
}

But I dont understand how it work on settings page, on how "action"

2

Answers


  1. You can do by filtering the post meta via PHP:

    function jm_change_post_meta_price( $meta_value, $post_id, $meta_key ) {
        if( 'price' == $meta_key ) {
           $meta_value = $meta_value * 1.135;
        }
            
        return $meta_value;
    }
    add_filter( 'get_post_metadata', 'jm_change_post_meta_price', 10, 3 );
    
    Login or Signup to reply.
  2. Add the below code to your theme’s functions.php file or a custom plugin.

    The code will only execute the price update if this parameter is present and its value is set to 'true'. This allows you to control when the price update should happen by appending ?update_prices=true to the URL.

    // Function to update the price meta field for a specific custom post type
    function update_catalog_prices() {
        global $wpdb;
        
        // Check if the required parameter is present in the request
        if ( isset( $_GET['update_prices'] ) && $_GET['update_prices'] === 'true' ) {
            // Get the required percentage from the settings page
            $required_percentage = get_option('required_percentage');
            
            // Check if the required percentage is set
            if ($required_percentage) {
                // Define the custom post type to update
                $post_type = 'catalog';
                
                // Calculate the new price based on the required percentage
                $new_price = $required_percentage / 100;
                
                // Update the meta field 'price' for all products of the specified custom post type
                $wpdb->query(
                    $wpdb->prepare(
                        "UPDATE {$wpdb->prefix}postmeta SET meta_value = meta_value * %f WHERE meta_key = %s AND post_id IN (SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = %s)",
                        $new_price,
                        'price',
                        $post_type
                    )
                );
            }
        }
    }
    
    // Hook the function to an action to execute it
    add_action('init', 'update_catalog_prices');
    

    Please make sure to replace 'price' with the actual meta key for the price field in your ACF setup.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search