Woocommerce set category minimum cart

2019-05-28 09:17发布

I would like block price for specific woocommerce category, now work only all category woocommerce my code. My category name is: stock . I want only use for this category.

I want that a user if buy stock category have to buy minimum 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' );
        }
    }
}

3条回答
戒情不戒烟
2楼-- · 2019-05-28 09:50

Try below 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( '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;

        }

    }

Let me know the output..

查看更多
啃猪蹄的小仙女
3楼-- · 2019-05-28 09:52

This is my code, i have insert it into function.php : My category id : 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
查看更多
倾城 Initia
4楼-- · 2019-05-28 10:11

Try this code, hope this helps you

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' );
        }
      }
     }
查看更多
登录 后发表回答