Reputation: 623
I Made a custom form option on site to update price and send that data through ajax and try to get in filter woocommerce_before_calculate_totals Session.
Here is code
add_action( 'woocommerce_before_calculate_totals', 'calculate_total_price', 99 );
function calculate_total_price( $cart_object )
{
global $woocommerce;
session_start();
$tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
$target_product_id = $_SESSION['wdm_user_product_id_data'].'<br/>.';
$_SESSION['productlist'][] =
[
'price' => $tac_dd_discounted_price,
'productid' => $target_product_id
];
$arrys = array_merge( $_SESSION[ "productlist" ]);
$_SESSION[ "productlist" ] = array_unique($arrys);
// This unique array created in seesion fro multi product which show correct data.
$price_blank="1";
foreach ( $cart_object->get_cart() as $cart_item ) {
$id= $cart_item['data']->get_id();
//$target_product_id=$arrys['productlist']['productid'];
//$tac_dd_discounted_price=$arrys['productlist']['price'];
if ( $id == $target_product_id ) {
$cart_item['data']->set_price($tac_dd_discounted_price);
}
else
{
$cart_item['data']->set_price($my_price['productlist']['price']);
}
}
}
But issue is for one product in cart price show correct but when try to add two product the Seession variable append same value in both product
Upvotes: 1
Views: 4781
Reputation: 623
I did it anyone who used custom session for price need to update the cart and checkout page this is the hook which will help you a lot.
add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');
add_filter( 'woocommerce_get_cart_item_from_session', 'set_session_prices', 20 , 3 );
function set_woo_prices( $woo_data ) {
session_start();
$tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
$target_product_id = $_SESSION['wdm_user_product_id_data'];
if ( ! isset($tac_dd_discounted_price ) || empty ($tac_dd_discounted_price ) ) { return $woo_data; }
$woo_data['data']->set_price( $tac_dd_discounted_price );
$woo_data['my_price'] = $tac_dd_discounted_price;
return $woo_data;
}
function set_session_prices ( $woo_data , $values , $key ) {
if ( ! isset( $woo_data['my_price'] ) || empty ( $woo_data['my_price'] ) ) { return $woo_data; }
$woo_data['data']->set_price( $woo_data['my_price'] );
return $woo_data;
}
Upvotes: 0
Reputation: 254373
First, instead of using PHP $_SESSION
you should better use WooCommerce dedicated WC_Session
class:
// Set the data (the value can be also an indexed array)
WC()->session->set( 'custom_key', 'value' );
// Get the data
WC()->session->get( 'custom_key' );
Now in your code function, you are just getting from PHP Session one product and one price in:
$tac_dd_discounted_price = $_SESSION['wdm_user_price_data'];
$target_product_id = $_SESSION['wdm_user_product_id_data']; // ==> Removed .'<br/>.'
Instead you should need to get an array of product IDs and prices when there is many products in cart.
Also, you don't need global $woocommerce;
…
As you don't show all other related JS/Ajax/PHP code and as your question is not detailed, is not possible to help you more than that.
Upvotes: 2