【问题标题】:Allow customer to set the product quantity globally on Woocommerce products [closed]允许客户在 Woocommerce 产品上全局设置产品数量 [关闭]
【发布时间】:2018-03-22 11:56:33
【问题描述】:

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

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

进店的时候可以设置成团的大小,所以数量会自动设置吗?

【问题讨论】:

  • 你只需要问我或这个网站上的其他人之一来修复它。我们大多数人都会提供帮助。
  • #TheOneWhoMade 感谢您的回答。我是stackoverflow的新手,我没有找到如何给你发私信,我什至不知道这是否可能。我该如何寻求您的帮助?
  • NasBEN 在聊天中 ping 我,并附上问题的链接,不,不可能发送“私人消息”。
  • #TheOneWhoMade 我终于找到了聊天,但我无法发送任何消息,因为我是新手,而且我没有足够的“声誉”......我会尽快给你发消息!如果您有替代品,那将是完美的!再次感谢。

标签: php jquery wordpress woocommerce product


【解决方案1】:

下面的代码将允许客户通过 由短代码显示的自定义表单为所有产品设置特定数量的产品数量......这将允许您将其显示在您想要的位置,并且您将明白了:

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

并且在每个单品上;你会有类似的东西:

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

// 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 文件中。经过测试并且可以工作。

与短代码一起使用:

  • 在 Wordpress 页面或帖子编辑器上,只需使用:[bulk_qty]
  • 关于 PHP 代码使用:echo do_shortcode("[bulk_qty]");

【讨论】:

  • 感谢您的回答!我是 PHP 新手,也许我不知道如何使用代码。我只是把你给我的代码放在我孩子主题的function.php中,什么也没发生。我必须为我的商店自定义代码还是通用代码?会不会是翻译的问题? (我用的是翻译版)再次感谢
  • “这将允许你让它出现在你想要的地方,你会得到那个”我怎样才能让它出现在我想要的地方?对不起这个愚蠢的问题
猜你喜欢
  • 2019-09-13
  • 2022-07-11
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2018-07-23
  • 2019-05-01
相关资源
最近更新 更多