Goal
Solve three typical WooCommerce live-coding tasks and explain the solution out loud.
Key concepts
- Task 1: a progress bar "N UAH left until free shipping"
- Task 2: an orders list column compatible with HPOS
- Task 3: wholesale prices for the
wholesalerole, taking the variation price cache into account - During live coding, talk: what you're doing, why, what you'll check
- A simple working solution first, then improvements
Diagram
flowchart LR
U["Clarify the task"] --> S["Simple solution"] --> T["Check"] --> I["Improve:<br/>cache, security, blocks"]
Code
Task 1. How much is left until free shipping:
<?php
function my_free_shipping_left(): float {
$threshold = 1500.0;
$subtotal = (float) WC()->cart->get_displayed_subtotal();
return max( 0.0, $threshold - $subtotal );
}
add_action( 'woocommerce_before_cart', static function () {
$left = my_free_shipping_left();
if ( $left > 0 ) {
/* translators: %s: amount */
wc_print_notice( sprintf( __( '%s left until free shipping', 'my-plugin' ), wc_price( $left ) ), 'notice' );
}
} );
// A point to raise in the interview: this hook doesn't fire in the block cart —
// there you need a block or data through the Store API ExtendSchema.
Task 2. A "Source" column in orders — see lesson 1.3 (both screens).
Task 3. Wholesale prices:
<?php
function my_is_wholesale(): bool {
return is_user_logged_in() && in_array( 'wholesale', (array) wp_get_current_user()->roles, true );
}
function my_wholesale_price( $price, WC_Product $product ) {
if ( ! my_is_wholesale() || '' === $price ) {
return $price;
}
$custom = $product->get_meta( '_wholesale_price' );
return '' !== $custom ? $custom : (string) round( (float) $price * 0.8, wc_get_price_decimals() );
}
add_filter( 'woocommerce_product_get_price', 'my_wholesale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'my_wholesale_price', 10, 2 );
// The variation price range is cached — add the role to the cache hash.
add_filter( 'woocommerce_get_variation_prices_hash', static function ( array $hash ): array {
$hash[] = my_is_wholesale() ? 'wholesale' : 'retail';
return $hash;
} );
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| The wholesale price is in the cart, but the catalog shows the retail range | The variation price cache | woocommerce_get_variation_prices_hash |
| A guest sees wholesale prices | The page cache serves a wholesale client's cached page | Don't cache pages for logged-in wholesale clients |
| The notice isn't visible in the block cart | A classic template hook | Say it out loud and offer a block option |
Weak and strong answer
Question: How do you build wholesale prices for a separate role?
Weak answer. The
woocommerce_product_get_pricefilter.
Strong answer. Price filters for products and variations, with the role checked once. I always add the role to the variation price cache hash, otherwise the ranges will be wrong. For both price types (regular/sale) I decide together with the business. I check the page cache so wholesale prices don't reach guests. If there are many rules — better a ready plugin or an own rules table.
Why the second is stronger: The candidate knows the non-obvious cache nuance and thinks about the risk of leaking prices.
Practice
Solve the three tasks with a timer: 15, 15 and 20 minutes. Explain out loud (record yourself). Then compare with the solutions above and write down what you missed.
Interview Questions
Say what exactly doesn't work and what your hypotheses are. Asking for a hint is better than 5 minutes of silence. The interviewer evaluates the process.
Variations need a separate woocommerce_product_variation_get_price filter, and the price range of a variable product is cached. Without changing the cache hash the catalog will show the old range.
That the woocommerce_before_cart hook works only in the classic cart. For the block cart — extend the Store API with your data and show it through a block or a cart slot.