- This topic has 9 replies, 2 voices, and was last updated 3 years, 5 months ago by David.
-
AuthorPosts
-
October 30, 2020 at 5:16 am #1510783Marcel
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?October 30, 2020 at 7:24 am #1510917DavidStaffCustomer SupportHi 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
October 30, 2020 at 7:51 am #1511207MarcelHmmm, 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>'; }
October 30, 2020 at 8:01 am #1511225DavidStaffCustomer SupportIf 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.October 30, 2020 at 8:19 am #1511279MarcelOkay, 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.
October 30, 2020 at 8:36 am #1511311DavidStaffCustomer SupportYou 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.October 30, 2020 at 8:48 am #1511335MarcelDavid,
The filter what are you talking about (generate_block_element_display) should be inserted on my function.php, right?
October 30, 2020 at 8:52 am #1511339DavidStaffCustomer SupportThats correct.
October 30, 2020 at 9:22 am #1511364MarcelOkay, and in my Block Element I have to add in the Custom Hook name:
generate_block_element_display
, is it correct?October 30, 2020 at 10:09 am #1511417DavidStaffCustomer SupportNo 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. -
AuthorPosts
- You must be logged in to reply to this topic.