Public Ticket #5000202

Check product price against a value

Closed
Comments
5 Tickets 1
Benjamin Jessen started the conversation 3 weeks ago on October 30, 2026 at 10:35am

I made my own plugin that lets me set a limit on user spending for a specific time period, the plugin checks if adding a product (and in the chosen quantity) to the cart exceeds the limit, and if it does, it removes the added quantity from the cart and shows a message that the limit was exceeded.

The issue I've run into is that I'm using the following code to get the product, which I run on the 'woocommerce_add_to_cart' hook that brings along the Cart ID, Product ID, and Product Quantity.

$product = wc_get_product($productId);
$productPrice = $product->get_price();

The issue is that this only gets the base price, not any prices set on a B2Bking group/user set using B2Bking itself. Is there a way to get the listed price on the user's shop page specifically? The alternative is that I compare their new cart total to the old, but that requires a pageload and I feel like that's not a good solution.

2,765
KingsPlugins replied 3 weeks ago on October 31, 2026 at 1:43pm

Hi Benjamin,

Thank you for reaching out,

 

I'd recommend reading the product from the cart item - that uses the final price applied by B2BKing and also gets the price of the specific variation for variable products.

The code would look like this:

add_action( 'woocommerce_add_to_cart', 'check_spending_limit', 30, 6 );
function check_spending_limit( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
$item = WC()->cart->get_cart_item( $cart_item_key );
if ( empty( $item['data'] ) ) { return; }
$product = $item['data'];
$price = wc_get_price_to_display( $product, array( 'qty' => $quantity ) );
}

 

Alternatively, another option would be to get it as follows (use get_sale_price and get_regular_price rather than get_price). However I'd recommend the option above as the 1st choice:

$raw_price = $product->is_on_sale()
? $product->get_sale_price()
: $product->get_regular_price();
$price = wc_get_price_to_display( $product, array(
'price' => $raw_price,
'qty' => $quantity,
) );

 

Let me know if that works for you,

Kind Regards,
Stefan

Sign In