Custom stock options doesn't show up in bulk edit (Woocommerce 3.2.1)

守給你的承諾、 提交于 2019-12-20 07:33:09

问题


I used code from these posts (1,2) to create custom stock options:

function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
    jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php   

woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 
'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 
'woocommerce' ), 'options' => array(
    'instock' => __( 'In stock', 'woocommerce' ),
    'outofstock' => __( 'Out of stock', 'woocommerce' ),
    '1to3' => __( 'Διαθέσιμο! Παράδοση σε: 1-3 ημέρες', 'woocommerce' ),
    '3to5' => __( 'Διαθέσιμο! Παράδοση σε: 3-5 ημέρες', 'woocommerce' ),
    '7to10' => __( 'Διαθέσιμο! Παράδοση σε: 7-10 ημέρες', 'woocommerce' ),
    'onrequest' => __( 'Διαθέσιμο κατόπιν παραγγελίας', 'woocommerce' ),// 
    The new option !!!
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the 
product is listed as "in stock" or "out of stock" on the frontend.', 
'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 
'add_custom_stock_type');



function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( 
$_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 
'save_custom_stock_status',99,1);



function woocommerce_get_custom_availability( $data, $product ) {
$stock_status = get_post_meta($product->id , '_stock_status' , true );
switch( $stock_status  ) {
    case 'instock':
        $data = array( 'availability' => __( 'In stock', 'woocommerce' ), 
'class' => 'in-stock' );
    break;
    case 'outofstock':
        $data = array( 'availability' => __( 'Out of stock', 'woocommerce' 
), 'class' => 'out-of-stock' );
    break;
    case '1to3':
        $data = array( 'availability' => __( 'Διαθέσιμο! Παράδοση σε: 1-3 
ημέρες', 'woocommerce' ), 'class' => '1to3' );
    break;
    case '3to5':
        $data = array( 'availability' => __( 'Διαθέσιμο! Παράδοση σε: 3-5 
ημέρες', 'woocommerce' ), 'class' => '3to5' );
    break;
    case '7to10':
        $data = array( 'availability' => __( 'Διαθέσιμο! Παράδοση σε: 7-10 
ημέρες', 'woocommerce' ), 'class' => '7to10' );
    break;
    case 'onrequest':
        $data = array( 'availability' => __( 'Διαθέσιμο κατόπιν 
παραγγελίας', 'woocommerce' ), 'class' => 'on-request' );
    break;
}
return $data;
}
add_action('woocommerce_get_availability', 
'woocommerce_get_custom_availability', 10, 2);

The new stock options show up correctly in the single product edit page, but they won't show up when I try to bulk edit some products. Only "In stock" and "Out of stock" option are shown.

Product page:

Bulk Edit:

How can I bulk edit the products with the new custom stock options? It's a bit frustrating to edit the products one-by-one.


回答1:


Yes it's possible I have done this, code is below for saving an example custom field.

/**
 * Add a custom column to WooCommerce products quick edit.
 *
 */
function es_display_custom_quickedit_product() {
    ?>
    <br class="clear" />
    <h4>Custom Fields</h4>
    <label>
        <span class="title"><?php _e( 'Member Price', 'woocommerce' ); ?></span>
        <span class="input-text-wrap">
            <input type="text" name="member_price" class="text wc_input_price" value="">
        </span>
    </label>
    <br class="clear" />
    <?php
}

add_action( 'woocommerce_product_quick_edit_end', 'es_display_custom_quickedit_product' );

/**
 * Save the quick edit custom WooCommerce fields
 *
 */
function es_save_custom_quickedit_product( $product ) {
    if ( isset( $_REQUEST['member_price'] ) AND ! empty( $_REQUEST['member_price'] ) ) {
        update_post_meta( $product->id, 'member_price', wc_clean( $_REQUEST['member_price'] ) );
    }
    else 
        delete_post_meta( $product->id, 'member_price' );
}

add_action( 'woocommerce_product_quick_edit_save', 'es_save_custom_quickedit_product' );



回答2:


You can add the custom stock status in the quick edit by using the following filter in theme's functions.php

function add_custom_stock_type_in_quick_edit( $status ) {
    return array(
        'instock' => __( 'In stock', 'woocommerce' ),
        'exhibited' => __( 'Exhibited', 'woocommerce' ),
        'sold' => __( 'Sold', 'woocommerce' ),
    );
}
add_filter( 'woocommerce_product_stock_status_options', 'add_custom_stock_type_in_quick_edit' );


来源:https://stackoverflow.com/questions/46747572/custom-stock-options-doesnt-show-up-in-bulk-edit-woocommerce-3-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!