Wordpress 用另一个自定义字段更新自定义字段

Wordpress 用另一个自定义字段更新自定义字段,wordpress,woocommerce,custom-fields,Wordpress,Woocommerce,Custom Fields,我在WordPress中使用了很多自定义字段 目前我正在与WooCommerce合作,我不知道我的问题是WP还是仅仅与Woo有关 我有一个自定义字段设置作为选择框。 我可以在以下几个项目中进行选择: 新的 库存 卖完 当我选择“已售完”并保存帖子/产品时,我不仅要保存此字段,还要将“\u stock\u status”设置为“outofstock” 字段“\u stock\u status”是默认字段。它也是一个下拉框。您可以选择值“instock”或“outofstock” 问题是我正在使用W

我在WordPress中使用了很多自定义字段

目前我正在与WooCommerce合作,我不知道我的问题是WP还是仅仅与Woo有关

我有一个自定义字段设置作为选择框。 我可以在以下几个项目中进行选择:

  • 新的
  • 库存
  • 卖完
  • 当我选择“已售完”并保存帖子/产品时,我不仅要保存此字段,还要将“\u stock\u status”设置为“outofstock”

    字段“\u stock\u status”是默认字段。它也是一个下拉框。您可以选择值“instock”或“outofstock”

    问题是我正在使用WooCommerce保存功能,它被称为,
    WooCommerce\u process\u product\u meta

    我想我可以运行两个
    update\u post\u meta
    函数。但这不起作用

    我尝试了以下方法,为了测试,请检查我的自定义字段是否为空。如果它不是空的,则使用所选值更新它,并更新“\u stock\u status”

    使用此功能,我可以保存“我的自定义”字段,但“库存”状态不变

    我还尝试了这个函数和其他if函数的变体。 但我似乎无法像这样更新该字段

    $woocommerce_select = $_POST['_my_custom_field'];
    
    if( $woocommerce_select == 'sold_out' ) {
                   update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
                   update_post_meta( $post_id, '_stock_status', 'outofstock' );
    } else {
                   update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
    }
    
    我不知道我做错了什么,也许有人可以指点我

    谢谢, 钼


    更新:新增功能/挂钩:

    function woo_add_custom_general_fields_save( $post_id ){
    
    $woocommerce_select = $_POST['_my_custom_field'];
    
    if( !empty( $woocommerce_select ) ) {
           update_post_meta( $post_id, '_my_custom_field', esc_attr( $woocommerce_select ) );
           update_post_meta( $post_id, '_stock_status', 'outofstock' );
    }
    
    }
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 999 ); 
    
    我还尝试了一个较低或没有优先权

    以下是创建自定义字段的方式:

    function woo_add_custom_general_fields() {
    
    global $woocommerce, $post; ?>
    
    <div class="options_group">
            <p class="form-field custom_stock">
                <label for="custom_stock"><?php echo __( 'Custom Stock', 'aat-net-theme' ); ?></label>
                <span class="wrap">
                    <?php $custom_stock = get_post_meta( $post->ID, '_my_custom_field', true ); ?>  
                    <select id="custom_stock" name="_my_custom_field">
                        <option value="" <?php selected( $custom_stock, '' ); ?>> - Select Stock - </option>
                        <option value="new" <?php selected( $custom_stock, 'new' ); ?>>New</option>
                        <option value="in_stock" <?php selected( $custom_stock, 'in_stock' ); ?>>In Stock</option>
                        <option value="on_request" <?php selected( $custom_stock, 'on_request' ); ?>>On Request</option>
                        <option value="in_transit" <?php selected( $custom_stock, 'in_transit' ); ?>>In Transit</option>
                        <option value="not_available" <?php selected( $custom_stock, 'not_available' ); ?>>Not Available</option>
                    </select> 
                </span>
                <span class="description"><?php _e( 'Select the custom stock-status here.', 'aat-net-theme' ); ?></span>
            </p>
    
    <?php }
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    
    函数woo\u add\u custom\u general\u fields(){
    全球$woocmerce,$post;?>
    

    >新的 >应要求 >不可用


    也许这对其他人会有帮助

    我尝试了很多不同的方法,在某个时候我停用了我的MetaBox保存功能,字段仍然可以毫无问题地保存

    我的问题似乎是由使用两个默认的WooCommerce操作来创建和保存我的字段/元框引起的

    add_action( 'woocommerce_process_product_meta'...
    

    由于找不到解决方案,我现在创建了一个普通的WordPress元框,如下所示:

    function lwsaat_custom_meta() {
        add_meta_box( 
            'lwsaat_meta', // HTML 'id' attribute of the edit screen section
            __( 'Heading', 'lwsaat-plugin' ), // Title of the edit screen section
            'lwsaat_meta_callback', // Function that prints out the HTML for the edit screen section
            'product', // The type of writing screen on which to show the edit screen section
            'normal', // The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side')
            'high' // The priority within the context where the boxes should show ('high', 'core', 'default' or 'low')
        );
    }
    add_action( 'add_meta_boxes', 'lwsaat_custom_meta' );
    
    function lwsaat_meta_save( $post_id ) {
    
        // Checks save status
        $is_autosave = wp_is_post_autosave( $post_id );
        $is_revision = wp_is_post_revision( $post_id );
        $is_valid_nonce = ( isset( $_POST[ 'lwsaat_nonce' ] ) && wp_verify_nonce( $_POST[ 'lwsaat_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    
        // Exits script depending on save status
        if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
            return;
        }
    
    
        // Save custom stock select
        $custom_stock = $_POST[ '_aat_stock_select' ];
    
        if( $custom_stock == 'new' ) {
    
            update_post_meta( $post_id, '_aat_stock_select', $_POST[ '_aat_stock_select' ] );
            update_post_meta( $post_id, '_stock_status', 'instock' );
    
        } elseif ( $custom_stock == 'not_available' ) {
    
            update_post_meta( $post_id, '_aat_stock_select', $_POST[ '_aat_stock_select' ] );
            update_post_meta( $post_id, '_stock_status', 'outofstock' );
    
        } else {
            update_post_meta( $post_id, '_aat_stock_select', $_POST[ '_aat_stock_select' ] );
        }
    }
    add_action( 'save_post', 'lwsaat_meta_save' );
    
    我创建了回调函数,如下所示:

    function lwsaat_meta_callback( $post ) {
    
        wp_nonce_field( basename( __FILE__ ), 'lwsaat_nonce' );
        $lwsaat_stored_meta = get_post_meta( $post->ID );
        ?>
    
            <p class="form-field custom_stock">
                <label for="custom_stock"><?php echo __( 'Custom Stock', 'lwsaat-plugin' ); ?></label>
                    <?php $custom_stock = $lwsaat_stored_meta['_aat_stock_select'][0]; ?>   
                    <select id="custom_stock" name="_aat_stock_select">
                        <option value="new" <?php selected( $custom_stock, 'new' ); ?>>New</option>
                        <option value="in_stock" <?php selected( $custom_stock, 'in_stock' ); ?>>In Stock</option>
                        <option value="not_available" <?php selected( $custom_stock, 'not_available' ); ?>>Not Available</option>
                        <option value="eu_no_import" <?php selected( $custom_stock, 'eu_no_import' ); ?>>Import to EU not allowed</option>
                    </select> 
            </p>
        <?php
    }
    
    使用此代码,我可以将产品库存与自定义字段一起重新保存。
    我无法使用本机的WooCoommerce功能。

    你能给我看看钩子吗。
    function lwsaat_meta_callback( $post ) {
    
        wp_nonce_field( basename( __FILE__ ), 'lwsaat_nonce' );
        $lwsaat_stored_meta = get_post_meta( $post->ID );
        ?>
    
            <p class="form-field custom_stock">
                <label for="custom_stock"><?php echo __( 'Custom Stock', 'lwsaat-plugin' ); ?></label>
                    <?php $custom_stock = $lwsaat_stored_meta['_aat_stock_select'][0]; ?>   
                    <select id="custom_stock" name="_aat_stock_select">
                        <option value="new" <?php selected( $custom_stock, 'new' ); ?>>New</option>
                        <option value="in_stock" <?php selected( $custom_stock, 'in_stock' ); ?>>In Stock</option>
                        <option value="not_available" <?php selected( $custom_stock, 'not_available' ); ?>>Not Available</option>
                        <option value="eu_no_import" <?php selected( $custom_stock, 'eu_no_import' ); ?>>Import to EU not allowed</option>
                    </select> 
            </p>
        <?php
    }
    
    function lwsaat_meta_save( $post_id ) {
    
        // Checks save status
        $is_autosave = wp_is_post_autosave( $post_id );
        $is_revision = wp_is_post_revision( $post_id );
        $is_valid_nonce = ( isset( $_POST[ 'lwsaat_nonce' ] ) && wp_verify_nonce( $_POST[ 'lwsaat_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    
        // Exits script depending on save status
        if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
            return;
        }
    
    
        // Save custom stock select
        $custom_stock = $_POST[ '_aat_stock_select' ];
    
        if( $custom_stock == 'new' ) {
    
            update_post_meta( $post_id, '_aat_stock_select', $_POST[ '_aat_stock_select' ] );
            update_post_meta( $post_id, '_stock_status', 'instock' );
    
        } elseif ( $custom_stock == 'not_available' ) {
    
            update_post_meta( $post_id, '_aat_stock_select', $_POST[ '_aat_stock_select' ] );
            update_post_meta( $post_id, '_stock_status', 'outofstock' );
    
        } else {
            update_post_meta( $post_id, '_aat_stock_select', $_POST[ '_aat_stock_select' ] );
        }
    }
    add_action( 'save_post', 'lwsaat_meta_save' );