在WooCommerce中按商品类别同步自定义价格并添加购物车利润率计算
Hey there! Let's get that WooCommerce custom pricing setup working exactly how you need it. You already started with the woocommerce_product_get_price filter, so let's expand that to handle category-based base prices, plus add the logic to apply the 2.5x markup when items are added to the cart.
Here's the complete code to add to your theme's functions.php file:
First, we'll set the base prices for each category on all frontend pages except the cart/checkout:
// Set base category prices on product pages, archives, etc. add_filter('woocommerce_product_get_price', 'product_price_new', 10, 2); add_filter('woocommerce_product_variation_get_price', 'product_price_new', 10, 2); // Handle variable products too function product_price_new($price, $product) { // Only apply base prices outside cart/checkout if (!is_cart() && !is_checkout()) { // Get all category slugs for the product $product_cats = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'slugs')); // Assign base price based on category if (in_array('category-a', $product_cats)) { $price = 5; } elseif (in_array('category-b', $product_cats)) { $price = 10; } elseif (in_array('category-c', $product_cats)) { $price = 15; } } return $price; }
Next, we'll add the 2.5x markup to items in the cart and checkout:
// Apply 2.5x markup to cart items add_action('woocommerce_before_calculate_totals', 'apply_cart_price_markup', 10, 1); function apply_cart_price_markup($cart) { // Don't run on admin pages unless it's an AJAX request if (is_admin() && !defined('DOING_AJAX')) return; // Loop through each item in the cart foreach ($cart->get_cart() as $cart_item) { $product = $cart_item['data']; $product_cats = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'slugs')); // Get the base price based on category $base_price = 0; if (in_array('category-a', $product_cats)) { $base_price = 5; } elseif (in_array('category-b', $product_cats)) { $base_price = 10; } elseif (in_array('category-c', $product_cats)) { $base_price = 15; } // Apply markup if we have a valid base price if ($base_price > 0) { $marked_up_price = $base_price * 2.5; $cart_item['data']->set_price($marked_up_price); } } }
Important Notes:
- Replace
category-a,category-b,category-cwith your actual product category slugs (you can find these in the WooCommerce Category edit page under "Slug"). - The second filter (
woocommerce_product_variation_get_price) ensures variable products in these categories also get the correct base price. - The cart action runs every time cart totals are calculated, so the markup stays applied even if quantities change.
- We added a check to avoid running the cart logic on admin pages, which prevents unexpected behavior in the backend.
This setup will show the base price (5/10/15) on product pages and archives, then automatically multiply by 2.5 when the item is added to the cart and during checkout.
内容的提问来源于stack exchange,提问作者Abilash Erikson
相关产品推荐
相关产品推荐

