使用WooCommerce类别填充额外的字段

时间:2019-02-07 作者:mrblade

我有woocommerce+dokan marketplace插件的wordpress。在dokan商店设置中,我会添加一个类别下拉字段。在我的功能中。php:

<?php
add_filter( \'dokan_settings_form_bottom\', \'extra_fields_cat\', 10, 2);
function extra_fields_cat( $current_user, $profile_info ){
$cat_shop= isset( $profile_info[\'cat_shop\'] ) ? $profile_info[\'cat_shop\'] : \'\'; ?>
<div>
<?php (isset($_POST["category1"])) ? $company = $_POST["category1"] : $company=1; ?>
<select name="cat_shop" id="reg_cat_shop" class="dokan-form-control input-md valid" >
<option <?php if ($cat_shop == \'category1\' ) echo \'selected\' ; ?> value="category1">category1</option>
<option <?php if ($cat_shop == \'category2\' ) echo \'selected\' ; ?> value="category2">category2</option>
</select></div>
<?php }
//save the field value
add_action( \'dokan_store_profile_saved\', \'save_extra_fields_cat\', 15 );
function save_extra_fields_cat( $store_id ) {
$dokan_settings = dokan_get_store_info($store_id);
if ( isset( $_POST[\'cat_shop\'] ) ) {
$dokan_settings[\'cat_shop\'] = $_POST[\'cat_shop\'];
}
update_user_meta( $store_id, \'dokan_profile_settings\', $dokan_settings );
}
此示例工作正常,但我想用woocommerce类别填充此字段。有可能吗?谢谢

1 个回复
SO网友:mrblade

好的,解决方案:

$args = array(
    \'order\'      => \'ASC\',
    \'hide_empty\' => $hide_empty,
    \'include\'    => $ids,
    \'posts_per_page\' =>\'-1\'
);
$product_categories = get_terms( \'product_cat\', $args );
 (isset($_POST["Cianfrusaglie"])) ? $company = $_POST["Cianfrusaglie"] : $company=1;

echo "<select name=\'cat_shop\' id=\'reg_cat_shop\' class=\'dokan-form-control input-md valid\'>";
foreach( $product_categories as $category ){
    echo "<option "; ?><?php if ($cat_shop == esc_html( $category->name ) ) echo \'selected\' ; ?>
    <?php echo "value = \'" . esc_attr( $category->name ) . "\'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
现在,在我的woocommerce新产品页面中,如何选择我的类别?

相关推荐