商店页面和产品页面上的Woocommerce库存状态

时间:2018-11-09 18:45:02

标签: php wordpress woocommerce status stock

美好的一天,

我正在使用此代码,但是库存状态仍为“有货”和“缺货”,还必须为“预购”和“根据要求”。

它发生在Wordpress Jupiter主题更新之后,大约几天后才寻找原因和解决方案。你能帮忙吗?

/* add custom stock status */
function woocommerce_add_custom_stock_status() {
    ?>
    <script type="text/javascript">
    jQuery(function(){
        jQuery('._stock_status_field').not('.custom-stock-status').remove();
    });
    </script>
    <?php   
    /* update custom status if backorder if varations updated */
    $real_stock_status = get_post_meta($_REQUEST['post'], '_stock_status', true );
    if($real_stock_status=="onpreorder") {
        $stock_status = get_post_meta($_REQUEST['post'], '_custom_stock_status', true ); //get status from custom meta
        update_post_meta($_REQUEST['post'], '_stock_status', wc_clean( $stock_status ));
    }

    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock' => __( 'In stock', 'woocommerce' ),
        'outofstock' => __( 'Out of stock', 'woocommerce' ),
        'onpreorder' => __( 'Preorder', 'woocommerce' ),
        'onrequest' => __( 'On request', 'woocommerce' ),
         'eof' => __( 'End of life', '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', 'woocommerce_add_custom_stock_status');

/* save custom stock status */
function woocommerce_save_custom_stock_status( $product_id ) {
    update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
    update_post_meta( $product_id, '_custom_stock_status', wc_clean( $_POST['_stock_status'] ) ); //save another custom meta since '_stock_status' will be overridden
}
add_action('woocommerce_process_product_meta', 'woocommerce_save_custom_stock_status',99,1);

/* get custom stock status */
function get_custom_stock_status( $data, $product ) {
  $stock_status = get_post_meta($product->id , '_stock_status' , true );
    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 'onpreorder':
            $data = array( 'availability' => __( 'Preorder', 'woocommerce' ), 'class' => 'on-preorder' );
        break;
        case 'onrequest':
            $data = array( 'availability' => __( 'On request', 'woocommerce' ), 'class' => 'on-request' );
        break;
        case 'eof':
            $data = array( 'availability' => __( 'End of life', 'woocommerce' ), 'class' => 'end-of-life' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'get_custom_stock_status', 10, 2);

0 个答案:

没有答案