Dynamic content using wc_get_products

Hello,

I’m trying to display products that are on sale in a WooCommerce store as Dynamic content. For this, I’ve created a custom looper provider using a filter. Per WooCommerce’s Github (https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query), I was using the wc_get_products function as follows:

// 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);

However, that made using the retrieved data unintuitive, and I haven’t found how to access it to display it in my looper consumers. When using the debug function this is the information I get for one of the items:

Index: 0
Current Data:
object(WC_Product_Simple)#22420 (13) {
  ["id":protected]=>
  int(89)
  ["data":protected]=>
  array(50) {
    ["name"]=>
    string(24) "Outdoor Furniture Demo 3"
    ["slug"]=>
    string(24) "outdoor-furniture-demo-3"
    ["date_created"]=>
    object(WC_DateTime)#22423 (4) {
      ["utc_offset":protected]=>
      int(0)
      ["date"]=>
      string(26) "2024-05-27 05:03:19.000000"
      ["timezone_type"]=>
      int(1)
      ["timezone"]=>
      string(6) "+00:00"
    }
    ["date_modified"]=>
    object(WC_DateTime)#22409 (4) {
      ["utc_offset":protected]=>
      int(0)
      ["date"]=>
      string(26) "2024-06-22 04:28:13.000000"
      ["timezone_type"]=>
      int(1)
      ["timezone"]=>
      string(6) "+00:00"
    }
    ["status"]=>
    string(7) "publish"
    ["featured"]=>
    bool(false)
    ["catalog_visibility"]=>
    string(7) "visible"
    ["description"]=>
    string(949) "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Donec sed odio operae, eu vulputate felis rhoncus. Salutantibus vitae elit libero, a pharetra augue. Nihil hic munitissimus habendi senatus locus, nihil horum? A communi observantia non est recedendum.
Fabio vel iudice vincam, sunt in culpa qui officia. Inmensae subtilitatis, obscuris et malesuada fames. Ambitioni dedisse scripsisse iudicaretur. Nec dubitamus multa iter quae et nos invenerat. Petierunt uti sibi concilium totius Galliae in diem certam indicere.
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Me non paenitet nullum festiviorem excogitasse ad hoc. Ambitioni dedisse scripsisse iudicaretur. Unam incolunt Belgae, aliam Aquitani, tertiam. Morbi fringilla convallis sapien, id pulvinar odio volutpat. A communi observantia non est recedendum."
    ["short_description"]=>
    string(59) "This is the short description for Outdoor Furniture Demo 3."
    ["sku"]=>
    string(0) ""
    ["price"]=>
    string(2) "48"
    ["regular_price"]=>
    string(2) "54"
    ["sale_price"]=>
    string(2) "48"
    ["date_on_sale_from"]=>
    object(WC_DateTime)#22417 (4) {
      ["utc_offset":protected]=>
      int(0)
      ["date"]=>
      string(26) "2024-06-19 00:00:00.000000"
      ["timezone_type"]=>
      int(1)
      ["timezone"]=>
      string(6) "+00:00"
    }
    ["date_on_sale_to"]=>
    object(WC_DateTime)#21570 (4) {
      ["utc_offset":protected]=>
      int(0)
      ["date"]=>
      string(26) "2024-06-30 23:59:59.000000"
      ["timezone_type"]=>
      int(1)
      ["timezone"]=>
      string(6) "+00:00"
    }
    ["total_sales"]=>
    int(0)
    ["tax_status"]=>
    string(7) "taxable"
    ["tax_class"]=>
    string(0) ""
    ["manage_stock"]=>
    bool(false)
    ["stock_quantity"]=>
    NULL
    ["stock_status"]=>
    string(7) "instock"
    ["backorders"]=>
    string(2) "no"
    ["low_stock_amount"]=>
    string(0) ""
    ["sold_individually"]=>
    bool(false)
    ["weight"]=>
    string(0) ""
    ["length"]=>
    string(0) ""
    ["width"]=>
    string(0) ""
    ["height"]=>
    string(0) ""
    ["upsell_ids"]=>
    array(0) {
    }
    ["cross_sell_ids"]=>
    array(0) {
    }
    ["parent_id"]=>
    int(0)
    ["reviews_allowed"]=>
    bool(false)
    ["purchase_note"]=>
    string(0) ""
    ["attributes"]=>
    array(0) {
    }
    ["default_attributes"]=>
    array(0) {
    }
    ["menu_order"]=>
    int(0)
    ["post_password"]=>
    string(0) ""
    ["virtual"]=>
    bool(false)
    ["downloadable"]=>
    bool(false)
    ["category_ids"]=>
    array(1) {
      [0]=>
      int(24)
    }
    ["tag_ids"]=>
    array(3) {
      [0]=>
      int(38)
      [1]=>
      int(92)
      [2]=>
      int(23)
    }
    ["shipping_class_id"]=>
    int(0)
    ["downloads"]=>
    array(0) {
    }
    ["image_id"]=>
    string(2) "93"
    ["gallery_image_ids"]=>
    array(0) {
    }
    ["download_limit"]=>
    int(-1)
    ["download_expiry"]=>
    int(-1)
    ["rating_counts"]=>
    array(0) {
    }
    ["average_rating"]=>
    string(1) "0"
    ["review_count"]=>
    int(0)
  }
  ["changes":protected]=>
  array(0) {
  }
  ["object_read":protected]=>
  bool(true)
  ["object_type":protected]=>
  string(7) "product"
  ["extra_data":protected]=>
  array(0) {
  }
  ["default_data":protected]=>
  array(50) {
    ["name"]=>
    string(0) ""
    ["slug"]=>
    string(0) ""
    ["date_created"]=>
    NULL
    ["date_modified"]=>
    NULL
    ["status"]=>
    bool(false)
    ["featured"]=>
    bool(false)
    ["catalog_visibility"]=>
    string(7) "visible"
    ["description"]=>
    string(0) ""
    ["short_description"]=>
    string(0) ""
    ["sku"]=>
    string(0) ""
    ["price"]=>
    string(0) ""
    ["regular_price"]=>
    string(0) ""
    ["sale_price"]=>
    string(0) ""
    ["date_on_sale_from"]=>
    NULL
    ["date_on_sale_to"]=>
    NULL
    ["total_sales"]=>
    string(1) "0"
    ["tax_status"]=>
    string(7) "taxable"
    ["tax_class"]=>
    string(0) ""
    ["manage_stock"]=>
    bool(false)
    ["stock_quantity"]=>
    NULL
    ["stock_status"]=>
    string(7) "instock"
    ["backorders"]=>
    string(2) "no"
    ["low_stock_amount"]=>
    string(0) ""
    ["sold_individually"]=>
    bool(false)
    ["weight"]=>
    string(0) ""
    ["length"]=>
    string(0) ""
    ["width"]=>
    string(0) ""
    ["height"]=>
    string(0) ""
    ["upsell_ids"]=>
    array(0) {
    }
    ["cross_sell_ids"]=>
    array(0) {
    }
    ["parent_id"]=>
    int(0)
    ["reviews_allowed"]=>
    bool(true)
    ["purchase_note"]=>
    string(0) ""
    ["attributes"]=>
    array(0) {
    }
    ["default_attributes"]=>
    array(0) {
    }
    ["menu_order"]=>
    int(0)
    ["post_password"]=>
    string(0) ""
    ["virtual"]=>
    bool(false)
    ["downloadable"]=>
    bool(false)
    ["category_ids"]=>
    array(0) {
    }
    ["tag_ids"]=>
    array(0) {
    }
    ["shipping_class_id"]=>
    int(0)
    ["downloads"]=>
    array(0) {
    }
    ["image_id"]=>
    string(0) ""
    ["gallery_image_ids"]=>
    array(0) {
    }
    ["download_limit"]=>
    int(-1)
    ["download_expiry"]=>
    int(-1)
    ["rating_counts"]=>
    array(0) {
    }
    ["average_rating"]=>
    int(0)
    ["review_count"]=>
    int(0)
  }
  ["data_store":protected]=>
  object(WC_Data_Store)#22430 (4) {
    ["instance":"WC_Data_Store":private]=>
    object(WC_Product_Data_Store_CPT)#22429 (6) {
      ["meta_type":protected]=>
      string(4) "post"
      ["object_id_field_for_meta":protected]=>
      string(0) ""
      ["internal_meta_keys":protected]=>
      array(42) {
        [0]=>
        string(11) "_visibility"
        [1]=>
        string(4) "_sku"
        [2]=>
        string(6) "_price"
        [3]=>
        string(14) "_regular_price"
        [4]=>
        string(11) "_sale_price"
        [5]=>
        string(22) "_sale_price_dates_from"
        [6]=>
        string(20) "_sale_price_dates_to"
        [7]=>
        string(11) "total_sales"
        [8]=>
        string(11) "_tax_status"
        [9]=>
        string(10) "_tax_class"
        [10]=>
        string(13) "_manage_stock"
        [11]=>
        string(6) "_stock"
        [12]=>
        string(13) "_stock_status"
        [13]=>
        string(11) "_backorders"
        [14]=>
        string(17) "_low_stock_amount"
        [15]=>
        string(18) "_sold_individually"
        [16]=>
        string(7) "_weight"
        [17]=>
        string(7) "_length"
        [18]=>
        string(6) "_width"
        [19]=>
        string(7) "_height"
        [20]=>
        string(11) "_upsell_ids"
        [21]=>
        string(14) "_crosssell_ids"
        [22]=>
        string(14) "_purchase_note"
        [23]=>
        string(19) "_default_attributes"
        [24]=>
        string(19) "_product_attributes"
        [25]=>
        string(8) "_virtual"
        [26]=>
        string(13) "_downloadable"
        [27]=>
        string(15) "_download_limit"
        [28]=>
        string(16) "_download_expiry"
        [29]=>
        string(9) "_featured"
        [30]=>
        string(19) "_downloadable_files"
        [31]=>
        string(16) "_wc_rating_count"
        [32]=>
        string(18) "_wc_average_rating"
        [33]=>
        string(16) "_wc_review_count"
        [34]=>
        string(22) "_variation_description"
        [35]=>
        string(13) "_thumbnail_id"
        [36]=>
        string(11) "_file_paths"
        [37]=>
        string(22) "_product_image_gallery"
        [38]=>
        string(16) "_product_version"
        [39]=>
        string(12) "_wp_old_slug"
        [40]=>
        string(10) "_edit_last"
        [41]=>
        string(10) "_edit_lock"
      }
      ["must_exist_meta_keys":protected]=>
      array(1) {
        [0]=>
        string(10) "_tax_class"
      }
      ["extra_data_saved":protected]=>
      bool(false)
      ["updated_props":protected]=>
      array(0) {
      }
    }
    ["stores":"WC_Data_Store":private]=>
    array(38) {
      ["coupon"]=>
      string(24) "WC_Coupon_Data_Store_CPT"
      ["customer"]=>
      string(22) "WC_Customer_Data_Store"
      ["customer-download"]=>
      string(31) "WC_Customer_Download_Data_Store"
      ["customer-download-log"]=>
      string(35) "WC_Customer_Download_Log_Data_Store"
      ["customer-session"]=>
      string(30) "WC_Customer_Data_Store_Session"
      ["order"]=>
      string(23) "WC_Order_Data_Store_CPT"
      ["order-refund"]=>
      string(30) "WC_Order_Refund_Data_Store_CPT"
      ["order-item"]=>
      string(24) "WC_Order_Item_Data_Store"
      ["order-item-coupon"]=>
      string(31) "WC_Order_Item_Coupon_Data_Store"
      ["order-item-fee"]=>
      string(28) "WC_Order_Item_Fee_Data_Store"
      ["order-item-product"]=>
      string(32) "WC_Order_Item_Product_Data_Store"
      ["order-item-shipping"]=>
      string(33) "WC_Order_Item_Shipping_Data_Store"
      ["order-item-tax"]=>
      string(28) "WC_Order_Item_Tax_Data_Store"
      ["payment-token"]=>
      string(27) "WC_Payment_Token_Data_Store"
      ["product"]=>
      string(25) "WC_Product_Data_Store_CPT"
      ["product-grouped"]=>
      string(33) "WC_Product_Grouped_Data_Store_CPT"
      ["product-variable"]=>
      string(34) "WC_Product_Variable_Data_Store_CPT"
      ["product-variation"]=>
      string(35) "WC_Product_Variation_Data_Store_CPT"
      ["shipping-zone"]=>
      string(27) "WC_Shipping_Zone_Data_Store"
      ["webhook"]=>
      string(21) "WC_Webhook_Data_Store"
      ["report-revenue-stats"]=>
      string(63) "Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore"
      ["report-orders"]=>
      string(57) "Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore"
      ["report-orders-stats"]=>
      string(63) "Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore"
      ["report-products"]=>
      string(59) "Automattic\WooCommerce\Admin\API\Reports\Products\DataStore"
      ["report-variations"]=>
      string(61) "Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore"
      ["report-products-stats"]=>
      string(65) "Automattic\WooCommerce\Admin\API\Reports\Products\Stats\DataStore"
      ["report-variations-stats"]=>
      string(67) "Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\DataStore"
      ["report-categories"]=>
      string(61) "Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore"
      ["report-taxes"]=>
      string(56) "Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore"
      ["report-taxes-stats"]=>
      string(62) "Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore"
      ["report-coupons"]=>
      string(58) "Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore"
      ["report-coupons-stats"]=>
      string(64) "Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore"
      ["report-downloads"]=>
      string(60) "Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore"
      ["report-downloads-stats"]=>
      string(66) "Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\DataStore"
      ["admin-note"]=>
      string(44) "Automattic\WooCommerce\Admin\Notes\DataStore"
      ["report-customers"]=>
      string(60) "Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore"
      ["report-customers-stats"]=>
      string(66) "Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore"
      ["report-stock-stats"]=>
      string(62) "Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\DataStore"
    }
    ["current_class_name":"WC_Data_Store":private]=>
    string(25) "WC_Product_Data_Store_CPT"
    ["object_type":"WC_Data_Store":private]=>
    string(14) "product-simple"
  }
  ["cache_group":protected]=>
  string(8) "products"
  ["meta_data":protected]=>
  NULL
  ["legacy_datastore_props":protected]=>
  array(0) {
  }
  ["post_type":protected]=>
  string(7) "product"
  ["supports":protected]=>
  array(1) {
    [0]=>
    string(16) "ajax_add_to_cart"
  }
}

And when I try to use it, for example, with {{dc:post:title}} what I get in the front end is the page-where-it-is-displayed’s title, not the title for the retrieved product.

When using {{ looper.item }} I get the following:

{"id":89,"name":"Outdoor Furniture Demo 3","slug":"outdoor-furniture-demo-3","date_created":{"date":"2024-05-27 05:03:19.000000","timezone_type":1,"timezone":"+00:00"},"date_modified":{"date":"2024-06-22 04:28:13.000000","timezone_type":1,"timezone":"+00:00"},"status":"publish","featured":false,"catalog_visibility":"visible","description":"Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Donec sed odio operae, eu vulputate felis rhoncus. Salutantibus vitae elit libero, a pharetra augue. Nihil hic munitissimus habendi senatus locus, nihil horum? A communi observantia non est recedendum.\r\nFabio vel iudice vincam, sunt in culpa qui officia. Inmensae subtilitatis, obscuris et malesuada fames. Ambitioni dedisse scripsisse iudicaretur. Nec dubitamus multa iter quae et nos invenerat. Petierunt uti sibi concilium totius Galliae in diem certam indicere.\r\nLorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Me non paenitet nullum festiviorem excogitasse ad hoc. Ambitioni dedisse scripsisse iudicaretur. Unam incolunt Belgae, aliam Aquitani, tertiam. Morbi fringilla convallis sapien, id pulvinar odio volutpat. A communi observantia non est recedendum.","short_description":"This is the short description for Outdoor Furniture Demo 3.","sku":"","price":"48","regular_price":"54","sale_price":"48","date_on_sale_from":{"date":"2024-06-19 00:00:00.000000","timezone_type":1,"timezone":"+00:00"},"date_on_sale_to":{"date":"2024-06-30 23:59:59.000000","timezone_type":1,"timezone":"+00:00"},"total_sales":0,"tax_status":"taxable","tax_class":"","manage_stock":false,"stock_quantity":null,"stock_status":"instock","backorders":"no","low_stock_amount":"","sold_individually":false,"weight":"","length":"","width":"","height":"","upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"reviews_allowed":false,"purchase_note":"","attributes":[],"default_attributes":[],"menu_order":0,"post_password":"","virtual":false,"downloadable":false,"category_ids":[24],"tag_ids":[38,92,23],"shipping_class_id":0,"downloads":[],"image_id":"93","gallery_image_ids":[],"download_limit":-1,"download_expiry":-1,"rating_counts":[],"average_rating":"0","review_count":0,"meta_data":[{"id":669,"key":"_cs_states_cache","value":""}]}

What dynamic content formula should I follow to display the required information (i.e. Product Name, Product Featured Image, Permalink, etc.)? I haven’t found the answer in your documentation. I think it might be related to this (https://theme.co/docs/dynamic-content#objects), but I’m unsure about how to implement it.

Changing the custom filter to get_posts instead of wc_get_products makes {{dc:post:title}}, {{dc:post:featured_image_id}}, and {{dc:post:permalink}} all work as expected.

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

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

But I’d rather use wc_get_products as, per WC’s Github:

wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce…

Thanks in advance!

Hello @Oriol,

Thanks for the very detailed post information. You will have to use {{ looper.item key="name" }} in displaying the return sale product item title.

Kindly let us know how it goes.

Thanks for the quick reply! Unfortunately, I have tried that (and just did again) and it doesn’t seem to work.

The website is on Pre-release 6.5.0-rc1, as I’m using Twig Dynamic Content, not sure if that would have something to do with it.

I’ve made examples for both here:
https://melioravivere.com/working-home/

Also, {{dc:post:featured_image_id}}is making the headline element disappear (to show where is should be I copied it and removed the dynamic content that was in the subheadline in one of the instances).

Hello @Oriol,

We only provide support for the stable and current release versions. Please provide access to your site so we can check your looper. You can create a secure note in your next reply with the following info:
– Link to your site
– WP login URL
– WP username
– WP password
– WP Administrator Role
– Confirmation that we can access and make changes to your site

To know how to create a secure note, please check this out: How The Forum Works

image

Best Regards.

Hey @Oriol,

Instead of using the Looper Provider Custom, the Looper Provider Query Builder and use the _sale_price for the Meta Value condition.

By the way, you are using the Pro 6.5 rc-1. Be advised that we only support the current and stable release version. You should be posting any issues related to the unreleased version in our Beta Forum.

Best Regards.

Thanks again Ruenel, unfortunately, this doesn’t resolve the issue, as the Query Builder is a regular WP_Query, which I was trying to avoid in the first place. The custom WP_Query I was using works better than the Query builder version, as it avoids products that are not published.

I have downgraded to 6.4.22 and the issue persists.

Hi @oriol,

The predefined fields like featured image, and title only work with the default Looper with WP_Query, which returns the WP_Query object in a specific format. If you are using some other method that does not return the data in that format, you need to use the specific key to get the data as suggested by my colleague Runel.

Thanks

Hello Tristup, I’ve tried using key as Ruenel suggests, however, that doesn’t work.

Do you have a more comprehensive documentation on dynamic content and loopers?

@ruenel and @tristup

I apologize (and realize the importance of using the correct [beta] forums), I mentioned this in my first reply, but I should have changed forums at that point. I have Twig activated, and that changes the syntax I should have been using. After digging around and doing a ton of tests I found this to be the correct way to access the data:
{{ looper.item.name }} and {{looper.item.image_id}}

I also found that using {{looper.item.slug}} would work instead of {{dc:post:permalink}}.

Thanks for your time and patience.

Hi @oriol,

Glad that you are able to find the solution.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.