如何在Dokan产品表单中添加新字段来指定新产品和二手产品,并可以按旧产品和新产品过滤产品

问题描述 投票:0回答:1

我正在开发 WordPress/Dokan 插件。我正在寻找一种通过两个可选选项(新产品和二手产品)添加新字段的方法。此外,访问者可以按新产品和二手产品过滤商店页面或搜索结果页面中的产品。如果有人可以帮助我,我真的很感激?

我尝试了以下代码。但是当我将它添加到子主题中时,它会扰乱网站功能。

// ****adding extra field in single product page START****

// Adding extra field on New product popup/without popup form
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );

function new_product_field(){ ?>

     <div class="dokan-form-group">
              <label class="dokan-control-label" for=""><?php _e( 'Item condition', 'dokan' ); ?></label>
                <div class="dokan-form-group">
                    <select name="new_field" class="dokan-form-control">
                        <option value=""><?php _e( '', 'dokan' ) ?></option>
                        <option value="new"><?php _e( 'New', 'dokan' ) ?></option>
                        <option value="used"><?php _e( 'Used', 'dokan' ) ?></option>
                    </select>
                 </div>
        </div>

   <?php
}

/*
* Saving product field data for edit and update
*/

 add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
 add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );

function save_add_product_meta($product_id, $postdata){

    if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
            return;
        }

        // if ( ! empty( $postdata['new_field'] ) ) {
        //     update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
        // }
        update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}

/*
* Showing field data on product edit page
*/

add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);

function show_on_edit_page($post, $post_id){
$new_field         = get_post_meta( $post_id, 'new_field', true );
?>
   <div class="dokan-form-group">
       
        <label class="dokan-control-label" for=""><?php _e( 'Item condition', 'dokan' ); ?></label>
              <select name="new_field" class="dokan-form-control">
                    <option value="new" <?php echo ( "new_field" == 'new' ) ? 'selected' : 'Used' ?>><?php _e( 'New', 'dokan' ) ?></option>
                    <option value="used" <?php echo ( "new_field" == 'used' ) ? 'selected' : 'New' ?>><?php _e( 'Used', 'dokan' ) ?></option>
                </select>

     </div> <?php
    }

// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);

function show_product_code(){
      global $product;

        if ( empty( $product ) ) {
            return;
        }
 $new_field = get_post_meta( $product->get_id(), 'new_field', true );
 if (empty($new_field)) exit;

        if ( ! empty( $new_field ) ) {
            ?>

<span><?php echo esc_attr__( 'Item condition:', 'dokan' ); ?> <strong><?php echo esc_attr(    $new_field ); ?></strong></span>
            <?php
        }
}
// ****adding extra field in single product page END****
php wordpress woocommerce custom-wordpress-pages dokan
1个回答
0
投票

这是您应该尝试的更新代码

<?php 
// ****adding extra field in single product page START****

// Adding extra field on New product popup/without popup form
add_action('dokan_new_product_after_product_tags', 'new_product_field', 10);

function new_product_field() {
    // Security: Nonce field
    wp_nonce_field('new_product_field_nonce', 'new_product_field_nonce_name');
    ?>
    <div class="dokan-form-group">
        <label class="dokan-control-label" for="new_field"><?php _e('Item condition', 'dokan'); ?></label>
        <select name="new_field" class="dokan-form-control">
            <option value=""><?php _e('Select condition', 'dokan'); ?></option>
            <option value="new"><?php _e('New', 'dokan'); ?></option>
            <option value="used"><?php _e('Used', 'dokan'); ?></option>
        </select>
    </div>
    <?php
}

/*
* Saving product field data for edit and update
*/

add_action('dokan_new_product_added', 'save_add_product_meta', 10, 2);
add_action('dokan_product_updated', 'save_add_product_meta', 10, 2);

function save_add_product_meta($product_id, $postdata) {
    if (!isset($_POST['new_product_field_nonce_name']) || !wp_verify_nonce($_POST['new_product_field_nonce_name'], 'new_product_field_nonce')) {
        return;
    }

    if (isset($postdata['new_field']) && !empty($postdata['new_field'])) {
        update_post_meta($product_id, 'new_field', sanitize_text_field($postdata['new_field']));
    }
}


/*
* Showing field data on product edit page
*/

add_action('dokan_product_edit_after_product_tags', 'show_on_edit_page', 99, 2);

function show_on_edit_page($post, $post_id) {
    $new_field = get_post_meta($post_id, 'new_field', true);
    ?>
    <div class="dokan-form-group">
        <label class="dokan-control-label" for="new_field"><?php _e('Item condition', 'dokan'); ?></label>
        <select name="new_field" class="dokan-form-control">
            <option value="new" <?php selected($new_field, 'new'); ?>><?php _e('New', 'dokan'); ?></option>
            <option value="used" <?php selected($new_field, 'used'); ?>><?php _e('Used', 'dokan'); ?></option>
        </select>
    </div>
    <?php
}

// showing on single product page
add_action('woocommerce_single_product_summary', 'show_product_code', 13);

function show_product_code() {
    global $product;

    $new_field = get_post_meta($product->get_id(), 'new_field', true);
    if (!empty($new_field)) {
        echo '<span>' . esc_html__('Item condition:', 'dokan') . ' <strong>' . esc_html($new_field) . '</strong></span>';
    }
}

// ****adding extra field in single product page END****
© www.soinside.com 2019 - 2024. All rights reserved.