允许客户在Woocommerce产品上全局设置产品数量 [英] Allow customer to set the product quantity globally on Woocommerce products

查看:136
本文介绍了允许客户在Woocommerce产品上全局设置产品数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将woocommerce用于餐饮开业.我与为客户订购午餐的公司合作,他们必须为每个人选择相同的产品.因此,所有产品的数量都是相同的.

I use woocommerce for my catering start-up. I work with companies that order lunches for their clients, and they have to choose the same products for each person. So, the quantities will be the same for all products.

例如,一个30人的小组想要一个三明治,一个苏打水和一个水果=> 30个三明治+ 30个苏打水+ 30个水果.

For example, a group of 30 people want a sandwich, a soda, and a fruit => 30 sandwiches + 30 sodas + 30 fruits.

进入商店时是否可以设置组的大小,以便自动设置数量?

Is that a way to set the size of the group when entering the shop, so the quantities will be automatically set?

推荐答案

下面的代码将允许客户通过由简码显示的自定义表格为所有产品设置特定数量的产品数量…这将使您可以将其显示在所需的位置,并且您会得到它:

The code below will allow customers to set a specific quantity product amount for all products, through a custom form displayed by a shortcode… This will allow you to make it appear where you want and you will get that:

一旦客户设置了数量,他将被重定向到商店页面,并通过一条自定义消息通知他:

Once customer will set his quantity, he will be redirected to shop page and he will be notified by a custom message:

在每个单一产品上;您将看到类似这样的内容:

And on each single product; you will have something like:

客户可以更改此数量或重设它……这是代码:

The customer can change this quantity or reset it… Here is that code:

// Shortcode with form fields for quantity
if( ! function_exists('set_bulk_product_quantity') ) {
    function set_bulk_product_quantity() {
        $bulk_qty = WC()->session->get( 'bulk_qty' );
        if( empty($bulk_qty) )
            $bulk_qty = '0';

        $label_name    = __('Set Products minimal Bulk Quantity ', 'woocommerce');
        $submit_button = __('Set quantity', 'woocommerce');
        $reset_button  = __('Reset', 'woocommerce');
        $style         = 'style="max-width:80px;text-align:right"';

        return '<form id="bulk_qty" method="post" action="">
        <p class="form-row form-row-wide" id="bulk_product_quantity_field">
            <label for="bulk_qty">'.$label_name.'</label>
            <input type="number" class="input-text qty text" name="bulk_qty" '.$style.' value="'.$bulk_qty.'">
            <input type="submit" class="button alt" name="bulk_qty_submit" id="bulk_qty_submit" value="'.$submit_button.'">
            <input type="reset" class="button alt" id="bulk_qty_reset" value="'.$reset_button.'">
        </p>
        </form>
        <script type="text/javascript">
            jQuery(function($){
                if( $("input[name=bulk_qty]").val() == "")
                    $("input[name=bulk_qty]").attr("value", "0");;
                $("#bulk_qty_reset").click( function(){
                    console.log($("input[name=bulk_qty]").val());
                    $("input[name=bulk_qty]").attr("value", "0");
                    $("form#bulk_qty").submit();
                });
            });
        </script>';
        }
    add_shortcode( 'bulk_qty', 'set_bulk_product_quantity' );
}



// Setting the bulk quantity in session data, displaying the notice and redirecting to shop
add_action( 'template_redirect', 'special_bulk_product_quantity' );
function special_bulk_product_quantity() {
    if ( isset($_POST['bulk_qty']) ){
        $bulk_qty = sanitize_text_field( trim($_POST['bulk_qty']) );
        if( $bulk_qty == 0 ){
            $qty_display = $bulk_qty;
            $bulk_qty = '';
        } else {
            $qty_display = $bulk_qty;
        }
        // Set the quantity value in customer WC_Sessions
        WC()->session->set( 'bulk_qty', $bulk_qty );

        // Add and display a custom notice (message)
        wc_add_notice( __('The bulk quantity is now set to ', 'woocommerce') . $qty_display, 'notice' );

        // Redirect to shop page
        wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
        exit(); // always exit at the end
    }
}


// Setting the bulk quantity amount in all products
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
    if( ! is_cart() ){
        $bulk_qty = WC()->session->get( 'bulk_qty' );

        if( ! empty($bulk_qty) ) {
            $args['input_value'] = $bulk_qty; // Start from this value (default = 1)
            // $args['min_value']   = 20; // Min value (default = 0)
            // $args['max_value']   = -1; // Min value (default = 0)
        }
    }
    return $args;
}

代码进入您的活动子主题(或主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or theme). Tested and works.

使用简码:

  • 在Wordpress页面或帖子编辑器上,只需使用:[bulk_qty]
  • 在PHP代码上,使用:echo do_shortcode("[bulk_qty]");
  • On Wordpress page or post editor just use: [bulk_qty]
  • On PHP code use: echo do_shortcode("[bulk_qty]");

这篇关于允许客户在Woocommerce产品上全局设置产品数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆