{"id":138638,"date":"2020-10-10T02:08:10","date_gmt":"2020-10-10T05:08:10","guid":{"rendered":"https:\/\/quadlayers.com\/?p=138638"},"modified":"2021-05-06T11:11:08","modified_gmt":"2021-05-06T14:11:08","slug":"update-product-price-programmatically-in-woocommerce","status":"publish","type":"post","link":"https:\/\/quadlayers.com\/update-product-price-programmatically-in-woocommerce\/","title":{"rendered":"How to update product price programmatically in WooCommerce"},"content":{"rendered":"

Do you want to learn how to update the product price programmatically in WooCommerce<\/strong>? In this guide, we’ll show you how to change prices in WooCommerce without using any plugins or installing any extra tool.<\/p>\n

If you use them smartly, discounts can help you improve your conversion rates and increase your sales. There are several ways to implement discounts on your e-commerce store. For example, you can apply WooCommerce coupons programmatically<\/a>.<\/p>\n

However, you can also update the product price without using coupons<\/strong>. For example, you could give an exclusive discount to users that are subscribed to your newsletter or who have spent more than $100 in your store.<\/p>\n

In this guide, you’ll learn to change the price when customers add a product to the cart without using any coupons and by accessing the WooCommerce cart object directly. We\u2019ll have a look at some examples and apply some logic when updating the price. The goal is that you understand the logic so you can customize the scripts and apply them to your store.<\/p>\n

How to update the product price programmatically in WooCommerce<\/h2>\n

In this section, you’ll learn how to update the product price programmatically in WooCommerce<\/strong>. We’ll have a look at different examples to give you some ideas of what you can do in your store.<\/p>\n

    \n
  1. Update product price when a checkbox is selected\n
      \n
    1. Add the checkbox input field to the products page<\/li>\n
    2. Update the price when a user adds a product to the cart<\/li>\n
    3. Recalculate the total price of the cart<\/li>\n<\/ol>\n<\/li>\n
    4. Edit the product price based on user roles<\/li>\n
    5. Update the product price based on product taxonomy<\/li>\n<\/ol>\n

      Keep in mind that we’ll use several WooCommerce hooks so it’s a good idea to check out this guide<\/a> if you’re not familiar with them.<\/p>\n

      Before we start…<\/h4>\n

      Before we start, as we’ll make modifications to some core files, we recommend you install a child theme on your site.\u00a0If you don\u2019t have a child theme and you don\u2019t know how to install it, check out our guide to create a child theme<\/a> or our list of the best child theme plugins<\/a>.<\/p>\n

      NOTE<\/strong>: To apply these scripts, copy and paste them in the functions.php<\/code> file of the child theme. However, keep in mind that they’re intended for didactic purposes only so customize them before taking them to production.<\/em><\/p>\n

      1) Update product price when a checkbox is selected<\/h3>\n

      In the following sample script, we’ll add a checkbox input in the cart form on the product page<\/strong>. This way, we can apply custom logic and dynamically update the price of any product that customers add to the cart only when the checkbox is selected. \"How<\/p>\n

      1.1 Add the checkbox input field to the products page<\/h4>\n

      Before we update the WooCommerce product price programmatically, let’s add the checkbox to the products page<\/strong>. To do that, simply copy and paste the following script:<\/p>\n

      add_action('woocommerce_after_add_to_cart_button', 'add_check_box_to_product_page', 30 );\r\nfunction add_check_box_to_product_page(){ ?>     \r\n       <div style=\"margin-top:20px\">           \r\n<label for=\"extra_pack\"> <?php _e( 'Extra packaging', 'quadlayers' ); ?>\r\n<input type=\"checkbox\" name=\"extra_pack\" value=\"extra_pack\"> \r\n<\/label>\r\n                    \r\n<\/div>\r\n     <?php\r\n}<\/pre>\n

      The woocommerce_after_add_to_cart_button<\/code> hook allows us to print the checkbox right after the button as shown in the image above.<\/p>\n

      1.2 Update the price when the user adds a product to the cart<\/h4>\n

      Another interesting option is to update the price dynamically when customers add a product to their carts<\/strong>. So, in this case, to update the price programmatically in WooCommerce simply paste this script right after the previous one.<\/p>\n

      add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 3 );\r\n \r\nfunction add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {\r\n     \/\/ get product id & price\r\n    $product = wc_get_product( $product_id );\r\n    $price = $product->get_price();\r\n    \/\/ extra pack checkbox\r\n    if( ! empty( $_POST['extra_pack'] ) ) {\r\n       \r\n        $cart_item_data['new_price'] = $price + 15;\r\n    }\r\nreturn $cart_item_data;\r\n}<\/pre>\n

      woocommerce_add_cart_item_data<\/code> is the WooCommerce hook<\/a> that will allow us to edit the price of the current product. Additionally, the if() <\/code>conditional checks whether the checkbox is selected or not, and if it is, updates the price in the following line. Now, let’s break down the code to better understand what each part does.<\/p>\n