We are developing a Gold Jewelry Store. We've managed to write a code that determines a product’s category - whether it’s 24K or 22K, then it assigns a formula that multiplies the daily gold price to the product's weight.
All is good, and the price gets updated on the Single Product Page, however when I check out the product, the price doesn’t reflect on the Cart/Checkout page. It would say the base price which is "$0.00".
What am I missing? Thank you for any help.
add_filter( 'woocommerce_get_price_html', 'calculate_price_by_weight', 10, 2 );
function calculate_price_by_weight($price, $product){
// define gold type prices
$GOLD24K_PRICE = 98.00;
$GOLD22K_PRICE = 92.00;
// get the product weight and regular price
$weight = $product->get_weight();
$regular_price = $product->get_regular_price();
// check if the product has a 24k and 22k category
// 1. get product category list
$product_categories = wc_get_product_category_list($product->ID);
// 2. check if the category list has a 24k in it
$has_24k_category = strpos(strtolower($product_categories), '24k') !== false;
$has_22k_category = strpos(strtolower($product_categories), '22k') !== false;
// get the final gold price base on the category
if ($has_24k_category) {
$gold_price = $GOLD24K_PRICE;
} else if ($has_22k_category) {
$gold_price = $GOLD22K_PRICE;
} else {
// non gold products
return $regular_price;
}
// calculate product price based on gold type price, product weight
$price = $gold_price * $weight;
return $price;
}