[Support request] Echoing a Generateblock in a custom function

Home Forums Support Echoing a Generateblock in a custom function

  • This topic has 9 replies, 2 voices, and was last updated 4 months ago by David.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1510783
    Marcel

    Hello
    Question: I use a code snippet on my cart page to show a specific message for my customers.
    Is it possible to insert a specific block (made with Generateblocks) in a custom function?

    #1510917
    David
    Staff
    Customer Support

    Hi there,

    Possible but tricky and most likely would create issues maintaining the styles.

    Why not use the Block Element ?

    https://docs.generatepress.com/article/block-element-overview/

    Select the Hook > Custom Hook and add paste in your hook name ie. woocommerce_proceed_to_checkout

    #1511207
    Marcel

    Hmmm, this sounds interesting and it might work. But where should I insert my custom function?
    As you can see on my code snippet (which works fine if I add it in my function.php), I add an action and a function:

    add_action( 'woocommerce_proceed_to_checkout', 'wpdesk_cart_message' );
    /**
     * Add a specific message to WooCommerce cart page
     *
     */
    function wpdesk_cart_message() {
    	echo '<p>This is a specific message</p>';
    }
    #1511225
    David
    Staff
    Customer Support

    If all you want is to display a block in that location then you do not need the function.
    Simply create the Block Element and set the Hook as i explained above.

    #1511279
    Marcel

    Okay, I tried and it works if I want to show the same Block Element for doesn’t matter what products are in the cart, and understand I do not need a function for that.

    However, if I would like to add some conditions, for example, to show the Block Element “A” if product A is in the cart or to show the Block Element “B” if product B is in the cart, do you think it is possible? Of course, I need to use a function here.

    I specify I know how to manipulate conditions (if, elseif and so on) in a function, but I don’t know how I could use the blocks in this case.

    #1511311
    David
    Staff
    Customer Support

    You could use the generate_block_element_display filter to selectively display which block element for example:

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
      if ( 123 === $element_id && is_my_custom_condition() ) {
         $display = false;
      }
    
      return $display;
    }, 10, 2 );

    the 123 is the Block Elements ID.
    In the example this would remove the Block element if:

    a. its ID matches
    AND
    b. your custom condition is met.

    #1511335
    Marcel

    David,

    The filter what are you talking about (generate_block_element_display) should be inserted on my function.php, right?

    #1511339
    David
    Staff
    Customer Support
    #1511364
    Marcel

    Okay, and in my Block Element I have to add in the Custom Hook name:
    generate_block_element_display, is it correct?

    #1511417
    David
    Staff
    Customer Support

    No in your Block Element you add the custom hook name which is the one you had in your add_action:

    woocommerce_proceed_to_checkout

    Create your various block elements all with the same hook.
    You should see all blocks being displayed on the checkout.

    Then the add_filter function i provided here:

    https://generatepress.com/forums/topic/echoing-a-generateblock-in-a-custom-function/#post-1511311

    Goes in your functions PHP.
    As long as your custom condition is valid, it will remove the Block element with the same ID.

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