1

I am able to get the cart price to change, and the subtotal to change... however, I want to change the actually price of the product itself, not just in the cart.

    function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
    
    if ( 5825 === $cart_item['product_id'] ) {
        $price = '$ 1500.00';
        
    }
    return $price;
}

function sv_change_product_price_actual( $price, $product ) {
    $product_id->get_id();
    return $product_id;
    if ( $product_id === 5825 ) {
        $price = "$ 6000.00";
    }
}

function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity, $instance ) { 
    $product_subtotal = $product->get_price() * $quantity;
    return $product_subtotal; 
  }; 

function sv_change_product_price( $product, $price ) {
    $product_id->get_id();
    return $product_id;
    if ($product_id == 5825) {
        $product_price = ' $ 1500.00';
    }
}
add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );
add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 4 );
add_filter( 'woocommerce_product_price', 'sv_change_product_price_actual', 10, 3 );

Here's what I have already, and of course for testing purposes, the prices are hardcoded in.

Any thoughts or ideas?

Session Code Below (JavaScript):

    function getColorOption() { 
    selectElement =  
                   document.querySelector('#selectedColor'); 
                      
    outputColor = selectElement.value; 
  
    document.querySelector('.outputColor').textContent 
                   = outputColor; 
    // Check browser support
if (typeof(Storage) !== "undefined") {
  // Store
  sessionStorage.setItem("hiddenColor", document.getElementById('selectedColor').value);
  // Retrieve
  document.getElementById("result2").innerHTML = sessionStorage.getItem("hiddenColor");
} else {
  document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
        }; 
CapnQuack
  • 141
  • 12
  • 1
    You mean to want to change in the database also? – Bhautik Apr 01 '21 at 19:36
  • @Bhautik Yes! I want the actual price of the product to update based on certain requirements – CapnQuack Apr 01 '21 at 19:38
  • @Bhautik Correction. My apologies, I simply want to charge the customer a certain amount based on what they select on the product page. If I change it on the database it will change it for everybody. That's not what I want. – CapnQuack Apr 01 '21 at 19:43

1 Answers1

2

You can use woocommerce_before_calculate_totals action hook. loop through cart_object and check with your product id and you can use set_price to override price. check below code. code will go in your active theme function.php file.

add_action( 'woocommerce_before_calculate_totals', 'update_price_based_on_customer_selection' );

function update_price_based_on_customer_selection( $cart_object ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // change prices
    foreach ( $cart_object->get_cart() as $hash => $value ) {
        // check if the product is 5825 
        if(  $value['product_id'] == 5825 ) {
            $value['data']->set_price( 1500.00 );
        }

    }

}
Bhautik
  • 11,125
  • 3
  • 16
  • 38