Site logo

Archived topic

GB block elements in the WooCommerce checkout for a specific product

7 replies · Started by Marcel on June 6, 2021

Viewing posts 1–8 of 8

Hello,

I want to use GenerateBlocks to add 3 different blocks to the Woocommerce checkout page. The hooks I want to use are "woocommerce_before_checkout_billing_form", "woocommerce_checkout_before_order_review" and "woocommerce_after_order_notes".

I know how to create the three blocks and link them to the necessary hooks.

My question is, how can I make these blocks only appear on my checkout page when - for example - the product "Flying Ninja" (ID = 218) is in the cart.

Thank you!

Hi there,

My question is, how can I make these blocks only appear on my checkout page when – for example – the product “Flying Ninja” (ID = 218) is in the cart.

I believe that will require some pretty complicated conditional tags from WooCommerce which you will need to check with their support team.

If they can provide the conditional tags you need, then we should be able to use this filter:
https://generatepress.com/forums/topic/different-header-element-options-for-amp-pages/#post-1811847

I know how to add it as a code snippet on my function.php, and it works. But I do not know how to use the filter generate_element_display in combination with the function I created.

But instead to show on the checkout page the message: "I WANT TO INSERT GB BLOCK ELEMENT ID = 4193", I would like to show the element with id 4193. I know I am close to the final result, but I did not get the trick yet.

Here is the function I use:

add_action( 'woocommerce_before_checkout_billing_form', 'function_user_click_woocommerce_checkout_before_order_review' );
    
function function_user_click_woocommerce_checkout_before_order_review() {
  
   $product_id = 218;
  
   $product_cart_id = WC()->cart->generate_cart_id( $product_id );
   $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
  
   if ( $in_cart ) {
  
      echo ' <p>I WANT TO INSERT GB BLOCK ELEMENT ID = 4193</p> ';
  
   }

}

It's not possible to insert blocks in a function as far as I'm aware of.

My idea is to use a block element and select woocommerce_before_checkout_billing_form as the hook.

Then use the generate_element_display with the specific conditional tag to display the elements.

OK, let's see what I have done so far, what is good and what is not.

I created the function that checks if a certain product is in the cart:

function woo_in_cart($product_id) {
    global $woocommerce;
 
    foreach($woocommerce->cart->get_cart() as $key => $val ) {
        $_product = $val['data'];
 
        if($product_id == $_product->id ) {
            return true;
        }
    }
 
    return false;
}

I added the filter generate_block_element_display:

add_filter( 'generate_block_element_display', function( $display, $element_id ) {
  if ( 4193 === $element_id && woo_in_cart(4187) ) {
     $display = true;
  }

  return $display;
}, 10, 2 );

Conclusion 1: if I use $ display = true; nothing happens (i.e. the block element is displayed regardless of whether a specific product is in the cart or not).

Conclusion 2: if I use $ display = false; the block element will not be displayed for that specific product.

I'm going back to a thread I opened last year and David told me that the block element is removed if:
a. its ID matches
AND
b) my custom condition is met.

However, I want the opposite: the block element to only appear if:
a. its ID matches
AND
b) my custom condition is met.

What do you think?

Can you make sure there isn't a display rule set in the elements itself?

Thank you! I was using the location rule. It is working now!

Last Q:
Can I use more element IDS in the same filter? For example, if IDs of block elements are 4193, 4194, 4195, 4196 can I use them on the same snippet or I have to create one for each?

add_filter( 'generate_block_element_display', function( $display, $element_id ) {
  if ( 4193, 4194, 4195, 4196 === $element_id && woo_in_cart(4187) ) {
     $display = true;
  }

  return $display;
}, 10, 2 );

Hi Marcel,

Last Q:
Can I use more element IDS in the same filter? For example, if IDs of block elements are 4193, 4194, 4195, 4196 can I use them on the same snippet or I have to create one for each?

Yes you can use multiple IDs but not by using comma. Use PHP's in_array().

Example:

add_filter( 'generate_block_element_display', function( $display, $element_id ) {

$id_targets = array(4193, 4194, 4195, 4196);
  if ( in_array($element_id, $id_targets ) && woo_in_cart(4187) ) {
     $display = true;
  }

  return $display;
}, 10, 2 );
This archived topic is closed to new replies.