Asked  7 Months ago    Answers:  5   Viewed   32 times
$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( "256", "1");

The above code add product 256 to the Cart 1 time. But the issue I'm having is that I want to be able to completely override the product price... as far as I can tell, the only thing I can do it apply a coupon to the Cart.

Is there a way to completely override the price to something totally custom?



Here is the code for overriding price of product in cart

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
        // for WooCommerce version 3+ use: 
        // $value['data']->set_price($custom_price);

Hope it will be useful...

Wednesday, March 31, 2021
answered 7 Months ago

I have changed my code as follows. It's working for me. Hope this may helps for some other who have some problem.

`add_filter( 'woocommerce_get_price', function ($price, $productd ) { 
       if($team_id) :
          return get_post_meta($productd->id, 'tax_'.$team_id , true); 
          return $price;

  }, 10, 2 );`

No problems even at 'Add-to-cart' functionality also.

Thanks, Satya

Wednesday, March 31, 2021
answered 7 Months ago

First in reference below you will find how to override properly woocommerce templates via a theme (avoiding editing the plugin templates).

In your first code snippet, as you can see for woocommerce_single_product_summary hook, you have in order all the different templates that are @hooked in this hook location with do_action() WordPress function:

do_action( 'woocommerce_single_product_summary' ); 

So in your customized code (the 2nd code snippet) you have just replaced the hook, by the hooked template slug (that is NOT a hook) and will NOT work as an entry point action hook. See the references at the bottom of this answer for the list of WooCommerce actions and filters existing hooks

Consequences: All other hooked templates in the commented list code (beginning with @hooked) will be missing if you replace a hook by a template slug.

For the hooks used in the templates see this helpful WooCommerce Visual Hook Guide

Explanations (How to):

HOW TO - Concrete example:

You want to customize woocommerce_template_single_title hooked template in woocommerce_single_product_summary hook.

 THE HOOK NAME:  woocommerce_single_product_summary hook.
 THE TEMPLATES HOOKED (+priority order number)  => corresponding template file name:    
— woocommerce_template_single_title       (5) => single-product/title.php
— woocommerce_template_single_rating     (10) => single-product/rating.php
— woocommerce_template_single_price      (10) => single-product/price.php
— woocommerce_template_single_excerpt    (20) => single-product/short-description.php
— woocommerce_template_single_add_to_cart(30) => single-product/add-to-cart/ (6 files depending on product type)
— woocommerce_template_single_meta       (40) => single-product/review-meta.php
— woocommerce_template_single_sharing -  (50) => single-product/share.php

Then you will need to edit the corresponding woocommerce_single_product_summary hook title.php located in single-product (sub folder)… Finally is not so complicated, once we understand the template structure files and the hooks in that templates.

The priority number, gives the order for the hooked templates: Smaller in first, bigger at the end…

See also: Hooks and their hooked functions execution queue in Wordpress and Woocommerce

Others ways:

You can also use all that existing templates hooks to target very specific changes or customizations, with custom functions located in the function.php file of your active child theme (or theme) or any plugin file too.

Example using add_action() WordPress function:

// define the woocommerce_single_product_summary callback function

function my_custom_action() { 
    echo '<p>This is my custom action function</p>';
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 ); 

This function has a priority number of 15 and will display "This is my custom action function" string text, between the product price and the product short description

Optional arguments of this hooked function for this hook:
• The template slug (string).
• The priority (int).


  • Template Structure + Overriding Templates via a Theme
  • Hooks: Action and Filter reference
  • LIST of WooCommerce Action and Filter (Hook Reference)
  • Template Structure + Overriding Templates via a Theme
  • WooCommerce Visual Hook Guide: Single Product Page
Tuesday, June 1, 2021
answered 5 Months ago

All of this code goes into functions.php

  1. This captures additional posted information (all sent in one array)

    function wdm_add_item_data($cart_item_data, $product_id) {
        global $woocommerce;
        $new_value = array();
        $new_value['_custom_options'] = $_POST['custom_options'];
        if(empty($cart_item_data)) {
            return $new_value;
        } else {
            return array_merge($cart_item_data, $new_value);
  2. This captures the information from the previous function and attaches it to the item.

    add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
    function wdm_get_cart_items_from_session($item,$values,$key) {
        if (array_key_exists( '_custom_options', $values ) ) {
            $item['_custom_options'] = $values['_custom_options'];
        return $item;
  3. This displays extra information on basket & checkout from within the added info that was attached to the item.

    function add_usr_custom_session($product_name, $values, $cart_item_key ) {
        $return_string = $product_name . "<br />" . $values['_custom_options']['description'];// . "<br />" . print_r($values['_custom_options']);
        return $return_string;
  4. This adds the information as meta data so that it can be seen as part of the order (to hide any meta data from the customer just start it with an underscore)

    function wdm_add_values_to_order_item_meta($item_id, $values) {
        global $woocommerce,$wpdb;
  5. If you want to override the price you can use information saved against the product to do so

    add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
    function update_custom_price( $cart_object ) {
        foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {       
            // Version 2.x
            //$value['data']->price = $value['_custom_options']['custom_price'];
            // Version 3.x / 4.x

All your custom information will appear in the customer email and order from within wordpress providing you added it as meta data (4.)

Wednesday, July 28, 2021
answered 3 Months ago

Please Try this

function woocommerce_custom_surcharge(){ 
global $woocommerce;
//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity
foreach($cart as $cart_val => $cid){
        $qty += $cid['quantity']; 
//Your Condition
    $excost = 7;
    else if($qty==7){
    $excost = 8;

    $woocommerce->cart->add_fee('Charges delivery', $excost, $taxable = false, $tax_class = '');


From Here I found that We need to get the quantity of the cart by summing up the values of the cart.Hope this will Solve your problem.

Friday, August 6, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :