I'm trying to change the cart item price from a product variations by a bulk price defined as a product custom field (product custom meta data), when the cart item quantity reaches a specific threshold.
I'm Working from: WooCommerce: Get custom field from product variations and display it on the “additional information area” And WooCommerce: Bulk Dynamic Pricing Without a Plugin
This is what I have:
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );
function bbloomer_quantity_based_pricing( $cart, $variation_data ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
//get
$bulk_price = get_post_meta( $variation_data[ 'variation_id' ], 'bulk_price', true);
if ( $bulk_price ) {
$threshold1 = 6; // Change price if items > 6
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['quantity'] >= $threshold1 ) {
$price = $bulk_price;
$cart_item['data']->set_price( $price );
}
}
}
}
But it doesn't work, as I can't get the custom field value for the bulk price.