.ai/skills/woocommerce-backend-dev/coding-conventions.md
Write self-explanatory code. Use comments sparingly - only for non-obvious insights.
Good - Code explains itself:
if ( $order->is_draft() && $user->can_delete_drafts() ) {
$order->delete();
}
Avoid - Over-commented:
// Check if order is draft
if ( $order->is_draft() ) {
// Check if user has permission
if ( $user->can_delete_drafts() ) {
// Delete the order
$order->delete();
}
}
When to add comments:
When NOT to add comments:
Follow WordPress Coding Standards:
'true' === $value not $value === 'true'Use ?? instead of isset checks for array access.
Good:
if ( 34 === ( $foo['bar'] ?? null ) ) {
// ...
}
$value = $options['setting'] ?? 'default';
Avoid:
if ( isset( $foo['bar'] ) && 34 === $foo['bar'] ) {
// ...
}
if ( isset( $options['setting'] ) ) {
$value = $options['setting'];
} else {
$value = 'default';
}
Prefer the ternary operator over if-else statements for simple conditional returns or assignments, except for very complex cases.
Good:
return $condition ? $true_value : $false_value;
$result = $is_enabled ? 'enabled' : 'disabled';
$price = $has_discount ? $product->get_sale_price() : $product->get_regular_price();
Avoid:
if ( $condition ) {
return $true_value;
}
return $false_value;
if ( $is_enabled ) {
$result = 'enabled';
} else {
$result = 'disabled';
}
Exception: Use traditional if-else for complex conditions or when multiple statements are needed in each branch.
// OK to use if-else when complex logic is involved
if ( $order->needs_shipping() && ! $order->has_valid_address() ) {
$this->logger->log( 'Invalid shipping address' );
$this->notify_customer( $order );
return false;
} else {
return true;
}
When using call_user_func_array(), always use positional arguments (numerically indexed array) instead of named/associative arrays for the arguments parameter.
The function uses array values in order and ignores keys, so named keys are misleading.
Correct:
call_user_func_array( array( $obj, 'method' ), array( $code ) );
call_user_func_array( array( $obj, 'process' ), array( $id, $status, $data ) );
Wrong:
call_user_func_array( array( $obj, 'method' ), array( 'country_code' => $code ) );
call_user_func_array( array( $obj, 'process' ), array( 'order_id' => $id, 'status' => $status ) );
Only fix linting errors for code that has been added or modified in the branch you are working on.
Do not fix linting errors in unrelated code unless specifically asked to do so.