Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.