Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/5c2483b491879b0c9acf66f291fd2a10 to your computer and use it in GitHub Desktop.
Save nczz/5c2483b491879b0c9acf66f291fd2a10 to your computer and use it in GitHub Desktop.
WooCommerce 建立商品下拉選單
<?php
$args = array('post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => '分類名稱', 'orderby' => 'ASC');
$loop = new WP_Query($args);
$dropdownlist = '<select id="cate_name">';
while ($loop->have_posts()) {
$loop->the_post();
$_product = wc_get_product($loop->post->ID);
$dropdownlist .= '<option value="' . $loop->post->ID . '" data-price="' . $_product->get_price() . '">' . $loop->post->post_title . '</option>';
}
$dropdownlist .= '</select>';
wp_reset_query();
echo '分類名稱:' . $dropdownlist;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment