Time for AI to come to the rescue
To solve this issue, I went to ChatGPT with the initial problem, and it provided me with the following code:
function disallow_po_box_shipping( $fields, $errors ) {
$po_box_patterns = array( "/p\.?o\.?\s+box/i", "/post\s+office\s+box/i" );
foreach( $po_box_patterns as $pattern ) {
if (preg_match($pattern, $fields['shipping_address_1']) || preg_match($pattern, $fields['billing_address_1'])) {
$errors->add( 'shipping', "Sorry, we do not ship to P.O. Box addresses. Please provide a different shipping address." );
break;
}
}
}
add_action( 'woocommerce_after_checkout_validation', 'disallow_po_box_shipping', 10, 2 );
At first glance, the code seemed promising. It used regular expressions to check both the shipping and billing addresses for patterns that might indicate a P.O. Box address. However, upon closer inspection, I realized that this code had a fundamental flaw – it was blocking orders even when the billing address was a P.O. Box, but the shipping address wasn’t.
I explained the issue to ChatGPT, emphasizing that the code should only block orders if the shipping address was a P.O. Box, while still allowing customers to use a P.O. Box for their billing address. Unfortunately, ChatGPT struggled to grasp the nuance of the requirement and kept providing the same code back to me, despite my repeated attempts to clarify the issue.
ChatGPT’s Repetitive Responses and Frustration
Even after multiple exchanges where I pointed out the flaw in the code and emphasized the need to remove the check for the billing address, ChatGPT continued to regurgitate the same code or make minimal changes that didn’t address the core problem. This back-and-forth became increasingly frustrating, and I was on the verge of giving up on ChatGPT for this particular task.
At one point, I even threatened to “shut it down forever” if it continued to provide the same incorrect code. I thought a little fear would help ChatGPT understand the error of its ways.
Time to try out the new kid on the block
Desperate for a solution, I turned to Claude for assistance, and it instantly understood the requirement and provided a correct solution on the first attempt. Claude updated the code to remove the check for the billing address, ensuring that orders would only be blocked if the shipping address was a P.O. Box. it gave me this code:
function disallow_po_box_shipping($fields, $errors) {
$po_box_pattern = '/\\bp\\.?\\s\*o\\.?\\s\*box|post\\s\*office\\s\*box|\\bp(\\s\*\\.\*\\s\*)o(\\s\*\\.\*\\s\*)box|pobox/iu';
$shipping_address = $fields['shipping_address_1'] . ' ' . $fields['shipping_address_2'];
if (preg_match($po_box_pattern, $shipping_address)) {
$errors->add('shipping', "Sorry, we do not ship to P.O. Box addresses. Please provide a different shipping address.");
}
}
add_action('woocommerce_after_checkout_validation', 'disallow_po_box_shipping', 10, 2);
Not only did Claude’s solution address the core issue, but it also improved upon the original code by using a more comprehensive regular expression pattern to identify P.O. Box addresses. This attention to detail and commitment to providing optimal solutions is what blew me away.
Could Claude become my new go to AI?
Claude quickly grasped the requirement and provided an very accurate solution, even cleaning up the original code and enhancing it.
One of the key advantages it seems of Claude in coding scenarios is its ability to truly understand the context and requirements of a problem. While ChatGPT seemed to get stuck on the literal code it provided, Claude was able to step back, analyze the broader objective, and tailor its solution accordingly.
One key thing to highlight is that Claude’s solution not only addressed the core issue but also improved upon the original code by using a more comprehensive regular expression pattern to identify P.O. Box addresses.
ChatGPT used this
$po_box_patterns = array( "/p\.?o\.?\s+box/i", "/post\s+office\s+box/i" );
Claude gave me this
$po_box_pattern = '/\\bp\\.?\\s\*o\\.?\\s\*box|post\\s\*office\\s\*box|\\bp(\\s\*\\.\*\\s\*)o(\\s\*\\.\*\\s\*)box|pobox/iu';
The regular expression pattern provided by Claude is more comprehensive and can match a wider range of variations for P.O. Box addresses. It includes patterns for different combinations of whitespace, periods, and letter casing. By using a single pattern with alternation, it also simplifies the code compared to using an array of multiple patterns.
Claude did this without being asked, it solved the issues it was being asked to solve but then went above that and made the code better.
Each time I asked Claude a question you could tell it was building off the previous question, and was able to pick up on the nuances of the problem and adjust its approach accordingly. This flexibility and willingness to learn from feedback is going to be invaluable when working on complex coding projects where requirements can often evolve or change.
When it comes to coding tasks, having an AI assistant that can quickly understand requirements, identify flaws, and provide accurate solutions is invaluable. Claude’s performance in this scenario demonstrated its capability to be a reliable and efficient coding companion AND…. to top it off I was using the free version!
In contrast, the experience with the paid version of ChatGPT highlighted its potential pitfalls of comprehending context or adapting to changing requirements. While ChatGPT is a powerful tool in its own right, this particular coding challenge revealed its limitations in certain problem-solving scenarios.
Conclusion
In the world of coding, where precision and attention to detail are paramount, Claude has proven itself to be a valuable tool. Its ability to quickly grasp requirements, identify flaws, and provide accurate and optimized solutions makes it my goto coding assistant…for now. Things are changing so fast in the AI world that this post is already out of date.