Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Dokan add new stock status

$
0
0

Replies: 0

Hello, I am trying to add a new stock status on my product, called on demand. I’ve been able to add the option on inventory.php on Dokan, adding a new option in the Stock Status part. This is the code:

<?php dokan_post_input_box( $post_id, '_stock_status', array( 'options' => array(
                'instock'     => __( 'In Stock', 'dokan-lite' ),
                'outofstock'  => __( 'Out of Stock', 'dokan-lite' ),
                'onbackorder' => __( 'On back order', 'dokan-lite' ),
                'ondemand'      => __( 'On demand', 'dokan-lite' ),
            ) ), 'select' ); ?>

I’ve been able to add this new option into the product pannel on the admin back-end, adding this code to my functions.php

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' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock'     => __( 'In stock', 'woocommerce' ),
        'outofstock'  => __( 'Out of stock', 'woocommerce' ),
        'onbackorder' => __( 'On back order', 'woocommerce' ),
        'ondemand'      => __( 'On demand', 'woocommerce' ),
    ), '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 woo_add_custom_general_fields_save_two( $post_id ){
    // Select
    $woocommerce_select = $_POST['_stock_status'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_stock_status', esc_attr( $woocommerce_select ) );
    else
    update_post_meta( $post_id, '_stock_status', '' );
    }

function woocommerce_get_custom_availability( $data, $product ) {
    switch( $product->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 'onbackorder':
            $data = array( 'availability' => __( 'On back order', 'woocommerce' ), 'class' => 'onbackorder' );
        break;
        case 'ondemand':
            $data = array( 'availability' => __( 'On demand', 'woocommerce' ), 'class' => 'ondemand' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

However, when I select the new stock status on the edit product page of Dokan, it doesn’t apply on the admin product page. Instead, it changes to the In Stock option, and therefore it changes back to In Stock on the dokan product page.

How can I make it so when I select the new option in Dokan, it’s also selected on the Admin product page, Inventory tab?


Viewing all articles
Browse latest Browse all 59525

Trending Articles