lundi 26 octobre 2015

Woocommerce add checkbox for stock option

I have eshop and I created there more options for stock states. I did it like this by putting this code to functions.php page in my theme.

 //add 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' ),
        'instock1' => __( 'Produkt bude skladem do 3 dnů', 'woocommerce' ),
        'onrequest2' => __( 'Produkt bude skladem do 5 dnů', 'woocommerce' ),
        'onrequest3' => __( 'Produkt bude skladem do 7 dnů', '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 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 'instock1':
            $data = array( 'availability' => __( 'Produkt bude skladem do 3 dnů', 'woocommerce' ), 'class' => 'instock1' );
        break;
        case 'onrequest2':
            $data = array( 'availability' => __( 'Produkt bude skladem do 5 dnů', 'woocommerce' ), 'class' => 'on-request2' );
        break;
        case 'onrequest3':
            $data = array( 'availability' => __( 'Produkt bude skladem do 7 dnů', 'woocommerce' ), 'class' => 'on-request3' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

So I have 5 stock states for my products: instock, outofstock, instock1, onrequest2, onrequest3. I need to create some popup with checkbox, that you must confirm this product before adding to cart. Or just some checkbox to payment page, or product page. This checkbox must be just for these 3 new options. And need to look like:

Something will be written...

checkbox [there will be text from stock state]

And if there will be option instock1 and onrequest2 in one cart, so both option with 2 checkbox will be there.

Another question: I need to hide and disable adding to cart with stock state outofstock only. This is not possible to do in Woocommerce settings.

Thanks a lot for reply.




Aucun commentaire:

Enregistrer un commentaire