2

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;
    }
Christine
  • 21
  • 2
  • there are few hooks that you can use for updating the cart and checkout - woocommerce_cart_item_price and woocommerce_before_calculate_totals the first one is for your minicart / cart page and the actual items and the second for the checkout if needed to add any other modifications to the price based on checkout data. You can see some examples here - https://stackoverflow.com/questions/43324605/change-cart-item-prices-in-woocommerce-3 and many more if you search for this hooks. Ofc you can use hooks such as woocommerce_add_to_cart too but depends on your goal and what you want to modify. – Snuffy Jan 05 '23 at 09:12
  • The hook in your sample will only change the price you see on the website, but the price for everything else will remain the same. If the price is changing once per day and you don't have thousands of products, then you can run a cron job to update product prices once per day. This is the safest way to make sure the correct price is used and displayed everywhere. But if you have thousands of products, then updating them might not be the most efficient. – Ivo Jerkovic Jan 05 '23 at 12:22
  • You can filter every price at once by "woocommerce_product_get_price" and "woocommerce_product_variation_get_price". – Mizuho Ogino Jan 08 '23 at 08:16
  • @MizuhoOgino Yup, that works but, the price on the Single Product Page doesn't reflect the price on the Cart & Checkout page. – Christine Jan 08 '23 at 20:38
  • @Christine "woocommerce_get_price_html" is just overwrite html, but "woocommerce_product_get_price" and "woocommerce_product_variation_get_price" are reflectable. If they don't, it's probably because somewhere in the template you're calling the price in a way other than the "get_price" tag. – Mizuho Ogino Jan 09 '23 at 07:28

0 Answers0