wordpresswoocommercedrop-down-menudropdowncontact-form-7

Display all products in dropdown list inside the form in the woocommerce


I'm new to wordpress and woocommerce and how can I list all products, that are in the webshop, in the dropdown list inside the form? I'm using contact form7 and Smart Grid-Layout Design for Contact Form 7 plugins but can't figure it out.

This is the code I'm using for dynamic-dropdown. I just selected all product categories for the 'post source' but there are no products in the list:

[dynamic_select* dropdown-product-list id:dropdownProductList 
"source:post:product" "taxonomy:product_cat:nekategorizirane" 
"taxonomy:product_cat:dom" "taxonomy:product_cat:elektronika" 
"taxonomy:product_cat:fitness" "taxonomy:product_cat:dodaci-prehrani" 
"taxonomy:product_cat:fitness-pomagala-i-sprave" 
"taxonomy:product_cat:kutak-za-klince" "taxonomy:product_cat:igracke" 
"taxonomy:product_cat:ljubimci" "taxonomy:product_cat:lov-i-ribolov" 
"taxonomy:product_cat:muskarci" "taxonomy:product_cat:libido" 
"taxonomy:product_cat:ljepota-i-njega" "taxonomy:product_cat:zdravlje" 
"taxonomy:product_cat:pokloni" "taxonomy:product_cat:poslovni-pokloni" 
"taxonomy:product_cat:program-za-mrsavljenje" 
"taxonomy:product_cat:zene" "taxonomy:product_cat:ljepota-i-njega-zene" 
"taxonomy:product_cat:odjeca" "taxonomy:product_cat:zdravlje-zene"]

Solution

  • I am the author the Smart-grid layout plugin extension, and I just came across this question, apologies for the delayed answer, but in case someone else needs it, here is the answer.

    The dynamic-dropdown allows you to populate your dropdown menu using using posts titles as a dynamic source (system posts or custom post types). By default it will use all the posts it finds, it has the option to filter a particular lists of posts that match one or more of the category terms associated with these posts.

    To get all your products, simply use the following shortcode,

    [dynamic_select* dropdown-product-list id:dropdownProductList 
    "source:post:product"]
    

    if you want to get the posts associated with the category term nekategorizirane, then

    [dynamic_select* dropdown-product-list id:dropdownProductList 
    "source:post:product" "taxonomy:product_cat:nekategorizirane"]
    

    However, if you customise your field to include posts with multiple category terms,

    [dynamic_select* dropdown-product-list id:dropdownProductList 
    "source:post:product" "taxonomy:product_cat:nekategorizirane" 
    "taxonomy:product_cat:dom" "taxonomy:product_cat:elektronika"]
    

    this is interpreted as a taxonomy post query with both the term nekategorizirane AND elektronika and not OR as I think you are expecting in your question.

    However, if you need more control on the actual lists of products returned, for example you want products from multiple categories, then you need to use a hook and programmatically modify the posts query associated with the dynamic-dropdown. The plugin provides a helper code associated with the field cell in which your dropdown is located. You can post it in your functions.php file,

    enter image description here

    Each helper code is formated to your form and field, so it is important to get it from your form UI editor directly. So using the default "all posts" dropdown field,

    [dynamic_select* dropdown-product-list id:dropdownProductList 
    "source:post:product"]
    

    and the following code will populate the dropdown list with product posts that are either associated with the term nekategorizirane OR elektronika

    add_filter( 'cf7sg_dynamic_list_post_query','dropdown_product_list_dynamic_list',10,3);
    /**
    * Filter post query for dynamic dropdown options.
    * @param array $args an arra of query terms.
    * @param WPCF7_FormTag $tag the field being populated.
    * @param string $cf7_key  the form unique key.
    * @return array an arra of query terms.
    */
    function dropdown_product_list_dynamic_list($query_args, $tag, $cf7_key){
      //if you have a dynamic dropdown field with posts as list source.
      //$query_args array to filter query arguments used to populate dynamic dropdown of posts.
      //these arguments are passed to the function get_posts($query_args). (codex: https://codex.wordpress.org/Template_Tags/get_posts)
      if('my-custom-form'!==$cf7_key || 'dropdown-product-list' !== $tag->name){
        return $query_args; //in case this is not the target form/field
      }
      //setup your custom query...
      $query_args['tax_query'] => array(
        'relation' => 'OR',
        array(
          'taxonomy' => 'nekategorizirane',
          'field' => 'slug',
          'terms' => 'bob',
        ),
        array(
          'taxonomy' => 'elektronika',
          'field' => 'slug',
          'terms' => 'bob',
        ),
      );
      return $query_args;
    }
    

    For more details on taxonomy query parameters, see the documentation.