I'm using this code to add a discount displayed on the cart page however the custom fields do not output the values.
I get the numbers on the cart page when i var_dump
function quantity_discount_percentage( $quantity ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$id = $cart_item['product_id'];
$var_1 = get_post_meta( $id, 'less_than_1', true );
$less_than_1 = $var_1 ? $var_1 : 2;
$var_2 = get_post_meta( $id, 'equal_to_greater_than_2', true );
$equal_to_greater_than_2 = $var_2 ? $var_2 : 2;
$var_3 = get_post_meta( $id, 'less_than_2', true );
$less_than_2 = $var_3 ? $var_3 : 6;
$var_4 = get_post_meta( $id, 'equal_to_greater_than_3', true );
$equal_to_greater_than_3 = $var_4 ? $var_4 : 6;
$var_5 = get_post_meta( $id, 'less_than_3', true );
$less_than_3 = $var_5 ? $var_5 : 12;
$var_6 = get_post_meta( $id, 'equal_to_greater_than_4', true );
$equal_to_greater_than_4 = $var_6 ? $var_6 : 12;
$var_7 = get_post_meta( $id, 'less_than_4', true );
$less_than_4 = $var_7 ? $var_7 : 50;
$var_8 = get_post_meta( $id, 'first_discount', true );
$first_discount = $var_8 ? $var_8 : 10;
$var_9 = get_post_meta( $id, 'second_discount', true);
$second_discount = $var_9 ? $var_9 : 15;
$var_10 = get_post_meta( $id, 'third_discount', true);
$third_discount = $var_10 ? $var_10 : 20;
$var_11 = get_post_meta( $id, 'forth_discount', true);
$forth_discount = $var_11 ? $var_11 : 25;
if ( $quantity < $less_than_1 )
$percent = 0;
elseif ( $quantity >= $equal_to_greater_than_2 && $quantity < $less_than_2 )
$percent = $first_discount;
elseif ( $quantity >= $equal_to_greater_than_3 && $quantity < $less_than_3 )
$percent = $second_discount;
elseif ( $quantity >= $equal_to_greater_than_4 && $quantity < $less_than_4 )
$percent = $third_dscount;
else
$percent = $forth_discount;
return $percent;
}
}
add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ) {
$product_id = $variation_id > 0 ? $variation_id : $product_id;
$first_discount = get_post_meta( $product_id, 'first_discount', true );
$second_discount = get_post_meta( $product_id, 'second_discount', true);
$third_discount = get_post_meta( $product_id, 'third_discount', true);
$forth_discount = get_post_meta( $product_id, 'forth_discount', true);
$percent_10 = $first_discount ? $first_discount : 10;
$percent_15 = $second_discount ? $second_discount : 15;
$percent_20 = $third_discount ? $third_discount : 20;
$percent_25 = $forth_discount ? $forth_discount : 25;
$product = wc_get_product($product_id);
$price = (float) $product->get_price();
$cart_item_data['discount'][0] = $price;
$cart_item_data['discount'][$percent_10] = $price * (100 - $percent_10) / 100;
$cart_item_data['discount'][$percent_15] = $price * (100 - $percent_15) / 100;
$cart_item_data['discount'][$percent_20] = $price * (100 - $percent_20) / 100;
$cart_item_data['discount'][$percent_25] = $price * (100 - $percent_25) / 100;
return $cart_item_data;
}
I've tried using get_post_meta, get_field and the_field with get_the_ID() and $product_id.
I tried with both ACF and WordPress custom fields.
The custom field input is located on the the Edit product page but the output is on the cart page.
What i'm trying to do is using custom fields to set the discount based on quantity per single product which is applied on the cart page.
I have also used $cart_item['discount'] as the 2nd parameter with the_field and get_field.
Works fine when i hard code the values for discount percentages and quantity limits.