Syntax created by the Dynamic Content button not working

Hello, Using 7.5 RC1, Twig activated

When using the Dynamic Content button on the bottom right corner of an element, the syntax (if I’m using the correct word for this) I’m getting doesn’t seem to work.

I’m using the following custom hook in the looper provider:

// loop through WooCommerce Products On Sale
add_filter('cs_looper_custom_onsale', function($result, $args){

$on_sale = wc_get_product_ids_on_sale();
    
$products = wc_get_products (
		array('include' => $on_sale,)
	);
return $products;
    }, 10, 2);

When I use the dynamic content button to insert Looper > Current Item I get {{ looper.item }}.
However, if I insert Looper > Field > Key= "name" I get {{ looper.field({"key":"name"}) }} . And this doesn’t work.
After a lot of testing, I found that the correct way to get the name was {{ looper.item.name }}.

1 Like

You’re sending a WC product which expects the name to be grabbed from the get_name() method. You’ll notice it doesn’t work with the standard {{dc:looper:field key="name"}} Dynamic Content either. I’m surprised it works with {{ looper.item.name }}, but there is probably additional Twig behind the scenes processing going on which allows this to work. Which is cool.

To do this in a standard WooCommerce way is to use {{ looper.item.get_name() }}, although it looks like what you discovered will also work fine. Let me know if this helps clear things up.

https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_get_name

1 Like