Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

_price with 4 decimals

  • Hey there, i use Woocommerce with JTL and everytime JTL updates the data i have 4 decimals in the _price meta field. I use the meta field to get the prices in query loop blocks but not the price isnt rounded and there are 4 decimals. Any idea to format it correctly?

    Here is the link to my problem –> https://pottauchocolat.de/pralinenkurs/#Buchen

  • Hi there,

    Try this:

    1. add a CSS class to the headline block which pulls the price, eg. rounded-price.

    2. add this PHP code to alter the dynamic content output:

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block) {
        if (!is_admin() && !empty($attributes['className']) && strpos($attributes['className'], 'rounded-price') !== false) {
            global $post;
            $product_id = $post->ID; // Assuming the post ID is the product ID
            
            $price = get_post_meta($product_id, '_price', true);
            $rounded_price = round($price);
            $content = $rounded_price;
        }
        return $content;
    }, 10, 3);

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • I tested it and it crashed the site 😀

    I then put the code in chatGPT and it found a solution.

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block) {
        // Überprüfen, ob wir uns nicht im Admin-Bereich befinden und die Klasse 'round-price' enthalten ist
        if (!is_admin() && !empty($attributes['className']) && strpos($attributes['className'], 'round-price') !== false) {
            // Versuchen, die Post-ID zu erhalten
            $product_id = get_the_ID();
    
            // Überprüfen, ob eine Produkt-ID vorhanden ist
            if ( $product_id ) {
                // Produktobjekt abrufen
                $product = wc_get_product( $product_id );
    
                if ( $product ) {
                    // Preis abrufen
                    $price = $product->get_price();
    
                    // Preis runden
                    $rounded_price = round( $price );
    
                    // Preis formatieren
                    $formatted_price = wc_price( $rounded_price );
    
                    // Inhalt setzen
                    $content = $formatted_price;
                }
            }
        }
        return $content;
    }, 10, 3);
    

    Thank you!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.