【问题标题】:Woocommerce set category minimum cartWoocommerce 设置类别最小购物车
【发布时间】:2020-04-26 14:06:03
【问题描述】:

我想要特定 woocommerce 类别的块价格,现在仅适用于我的代码的所有类别 woocommerce。我的类别名称是:股票。我只想用于这个类别。

我希望用户如果购买股票类别必须至少购买 1200。

    // Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set minimum cart total
        $minimum_cart_total = 1200;

        // Total we are going to be using for the Math
        // This is before taxes and shipping charges
        $total = WC()->cart->subtotal;

        // Compare values and add an error is Cart's total
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 10 USD is required before checking out. (Cont. below)
        // Current cart total: 6 USD 
        if( $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
                .'<br />Current cart\'s total: %s %s',
                $minimum_cart_total,
                get_option( 'woocommerce_currency'),
                $total,
                get_option( 'woocommerce_currency') ),
            'error' );
        }
    }
}

【问题讨论】:

  • 你能告诉我stock的类别ID吗?
  • 是的,分类id是:1441
  • 它有父类吗?
  • 不,没什么。这是主要类别。
  • 好的,那就试试我的回答吧!我正在发布..

标签: php wordpress woocommerce


【解决方案1】:

试试下面的代码:

    add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
    function spyr_set_min_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;
            $i=0;
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) :


                // Set minimum cart total
                $minimum_cart_total = 1200;

                // Total we are going to be using for the Math
                // This is before taxes and shipping charges
                $total = WC()->cart->subtotal;

                // See if any product is from the STOCK category or not
                if ( has_term( '112', 'product_cat', $product['product_id'] ) ) :

                    //Get price of that product
                    $regular_price = get_post_meta($product['product_id'], '_sale_price', true); //change to _sale_price if it is in sale
                    //echo $regular_price."<br>";
                    $total = $regular_price * $product['quantity']; 
                    //echo $total."<br>";
                    $subtotal_cat += $total; //get total of 
                    //echo $subtotal_cat;
                    //$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );

                endif;

            endforeach;
            foreach ( $woocommerce->cart->cart_contents as $product ) :

                if ( has_term( '112', 'product_cat', $product['product_id'] ) ) :

                    // Compare values and add an error is Cart's total
                    // happens to be less than the minimum required before checking out.
                    // Will display a message along the lines of
                    // A Minimum of 10 USD is required before checking out. (Cont. below)
                    // Current cart total: 6 USD 
                    if( $subtotal_cat <= $minimum_cart_total  ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required from stock category before checking out.</strong>'
                            .'<br />Current cart\'s total: %s %s',
                            $minimum_cart_total,
                            get_option( 'woocommerce_currency'),
                            $subtotal_cat,
                            get_option( 'woocommerce_currency') ),
                        'error' );
                    }
                endif;
            endforeach;

        }

    }

让我知道输出..

【讨论】:

  • 我测试了这段代码但没有用,我在function.php中有inserti。
  • 它是否给出任何错误?再试一次上面的代码...我已经对代码进行了更改..
  • 你是正价还是特价?如果是销售价格,则需要修改上面的代码。
  • 是的,我使用销售价格
  • 抱歉类别 tag_ID=112 我有更改代码和更新类别 ID,但什么也没有。我的第一个代码适用于所有类别。
【解决方案2】:

这是我的代码,我已将其插入到 function.php 中: 我的分类号:http://goo.gl/R197Bz

// start code
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
    function spyr_set_min_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;
            $i=0;
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) :


                // Set minimum cart total
                $minimum_cart_total = 1200;

                // Total we are going to be using for the Math
                // This is before taxes and shipping charges
                $total = WC()->cart->subtotal;

                // See if any product is from the STOCK category or not
                if ( has_term( '112', 'product_cat', $product['product_id'] ) ) :

                    //Get price of that product
                    $regular_price = get_post_meta($product['product_id'], '_sale_price', true); //change to _sale_price if it is in sale
                    //echo $regular_price."<br>";
                    $total = $regular_price * $product['quantity']; 
                    //echo $total."<br>";
                    $subtotal_cat += $total; //get total of 
                    //echo $subtotal_cat;
                    //$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );

                endif;

            endforeach;
            foreach ( $woocommerce->cart->cart_contents as $product ) :

                if ( has_term( '11', 'product_cat', $product['product_id'] ) ) :

                    // Compare values and add an error is Cart's total
                    // happens to be less than the minimum required before checking out.
                    // Will display a message along the lines of
                    // A Minimum of 10 USD is required before checking out. (Cont. below)
                    // Current cart total: 6 USD 
                    if( $subtotal_cat <= $minimum_cart_total  ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required from stock category before checking out.</strong>'
                            .'<br />Current cart\'s total: %s %s',
                            $minimum_cart_total,
                            get_option( 'woocommerce_currency'),
                            $subtotal_cat,
                            get_option( 'woocommerce_currency') ),
                        'error' );
                    }
                endif;
            endforeach;

        }

    }
    // end

【讨论】:

    【解决方案3】:

    试试这个代码,希望对你有帮助

    http://wordpress-code-snippets.blogspot.in/2017/02/woocommerce-set-minimum-quantity-for.html

         add_action( 'woocommerce_check_cart_items', 'restrict_product_quantity' );
          function restrict_product_quantity() {
        if( is_cart() || is_checkout() ) {  
            global $woocommerce;
    
            $pd_min_quantity= array( 
            array( 'id' => 3587, 'min' => 99),
            array( 'id' => 2554, 'min' => 87),
            array( 'id' => 5587, 'min' => 52),
            array( 'id' => 4488, 'min' => 40),
           array( 'id' => 2555, 'min' => 80),
    );
    
            $i = 0;
            $odd_prod = array();
    
            foreach( $woocommerce->cart->cart_contents as $pd_cart ) {
                foreach( $pd_min_quantity as $pd_quantity ) {
                    if( $pd_quantity['id'] == $pd_cart['product_id'] ) {
                        if( $pd_cart['quantity'] < $pd_quantity['min'] ) {
                            $odd_prod[$i]['id'] = $pd_cart['product_id'];
                            $odd_prod[$i]['in_cart'] = $pd_cart['quantity'];
                            $odd_prod[$i]['min_req'] = $pd_quantity['min'];
                        }
                    }
                }
                $i++;
            }
    
            if( is_array( $odd_prod) && count( $odd_prod ) > 1 ) {
                $message = '<strong>Minimum quantity per product should be added.</strong><br />';
                foreach( $odd_prod as $odd_prods ) {
                    $message .= get_the_title( $odd_prods['id'] ) .' requires a minimum quantity of '
                             . $odd_prods['min_req'] 
                             .'. You currently have: '. $odd_prods['in_cart'] .'.<br />';
                }
                wc_add_notice( $message, 'error' );
            }
          }
         }
    

    【讨论】:

      【解决方案4】:

      这是我的代码!

      add_action( 'woocommerce_check_cart_items', 'oxynergy_set_min_total' );
          function oxynergy_set_min_total() {
          // Only run in the Cart or Checkout pages
          if( is_cart() || is_checkout() ) {
      
              global $woocommerce, $product;
              $i=0;
              // Minimum order checking
              $minimumCheck = false;
              // Set minimum cart total
              $minimum_cart_total = 10;
              //loop through all cart products
              foreach ( $woocommerce->cart->cart_contents as $product ) {
                  // Total we are going to be using for the Math
                  // This is before taxes and shipping charges
                  $total = WC()->cart->total;
      
                  // See if any product is from the STOCK category or not
                  if ( has_term( 'STOCK', 'product_cat', $product['product_id'] ) || has_term( '482', 'product_cat', $product['product_id'] )  || has_term( '495', 'product_cat', $product['product_id'] ) ) {
                      $minimumCheck = true;
                      //Get price of that product
                      $regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
                      //echo $regular_price."<br>";
                      $total = $regular_price * $product['quantity']; 
                      //echo $total."<br>";
                      $subtotal_cat += $total; //get total of 
                      //echo $subtotal_cat;
                      $category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );
      
                  }
      
      
              }
      
      
                  if ( $minimumCheck && $category_price <= $minimum_cart_total) {
      
                      // Compare values and add an error is Cart's total
                      // happens to be less than the minimum required before checking out.
                      // Will display a message along the lines of
                      // A Minimum of 10 USD is required before checking out. (Cont. below)
      
                      wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required category before checking out.</strong>'
                              .'<br />Current cart\'s total: %s %s',
                              $minimum_cart_total,
                              get_option( 'woocommerce_currency'),
                              $category_price,
                              get_option( 'woocommerce_currency') ),
                          'error' );
                  }
      
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-03
        • 2015-05-11
        • 2021-12-21
        • 1970-01-01
        • 2019-09-18
        • 2013-07-22
        相关资源
        最近更新 更多