0

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: Product page

Bulk Edit: 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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jim.k_
  • 1

2 Answers2

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' );
Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44
  • This code adds a custom edit option in the single product quick edit. I want to bulk edit 5-6 products together (not one by one) but the custom stock options that I put before, are not there. They only exist in the single product edit page. – Jim.k_ Oct 16 '17 at 13:33
0

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' );
Achintha Samindika
  • 1,944
  • 1
  • 20
  • 31