Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4178
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T14:08:39+00:00 2022-06-03T14:08:39+00:00

php – How to change product prices in WooCommerce cart when product amount is more than one?

  • 0

[ad_1]

I am a PHP noob. I’m helping my friend set up a WooCommerce shop. He sells digital products. His pricing plan is as follows:

-buy the first copy of a product for full price (10€)
-buy all other copies for a discounted price (1€)

So, when a customer adds a product to their shopping cart, the first copy is full price. The prices of any additional copies of said product are added to the cart with discounted prices.

According to the pricing plan above, if a customer has two copies of a product in their cart, the total cart price should be 11€.

I’ve cobbled together this code:

add_action('woocommerce_before_calculate_totals', 'change_cart_price', 9999);

function change_cart_price($price_html) {
foreach( WC()->cart->get_cart() as $cart_item ){
    //get the quantity of each separate product in the cart
    $quantity = $cart_item['quantity'];
    
    //if the quantity of an item is higher than one,
    //decrease the for loop iterations by one
    if ($quantity > 1) {
        $iterations = $quantity -1;
            
        //change the prices of all but one copy of the product
        for ($i = 0; $i < $iterations; $i++) {
            $price = $cart_item['data']->get_price();
            $cart_item['data']->set_price(1);
        }
    }
    
}
return $price_html;
}

This code fires when the amount of a product in the cart is above one, but it changes the prices of all copies of the product. So, the cart total for two copies of a single product is 2€, instead of 11€.

Obviously, my “for” loop sucks. I’ve Googled different resources, but they tend to focus on changing the price of all the items in the cart.

I’ve thought about using an array that contains all the products. Maybe if I run through it and change prices according to Product ID?

Any help would be appreciated. Cheers.

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8087 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1871 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1957 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.