We're excited to announce a new mode, now available for public usage: WooCommerce PHP.
Like the existing General WordPress PHP mode, this mode is specifically tailored towards WooCommerce generations. With it, we can create complex queries, shortcodes, actions, and more (examples can be found below) using AI.
WooCommerce is an incredibly powerful and popular e-commerce solution for WordPress. People from all walks of life, use this platform, including developers, small business owners, and non-techies. This new mode for CodeWP should help everybody create higher quality code snippets without the need to hire developers or spend hours researching StackOverflow.
Ask for the solution you want in plain English
Get the code generated and install it on your (staging first) site
Use the new, custom snippet WITHOUT needing to code anything
This platform is in it's alpha testing phase. You can access it free for the duration of the testing period by: 1) signing up, 2) clicking 5 day free trial, 3) entering code isotropic_alpha .
Already Logged In? Click Here To Launch WooCommerce PHP Mode
WooCommerce Mode Prompt Examples
Let's take a look at some real world WooCommerce Code Snippets, generated by CodeWP.
Prompt: auto apply coupon with the "coupon_code" when the product with the id of 10 is in the cart
add_action( 'woocommerce_before_calculate_totals', 'apply_coupon_automatically' );
function apply_coupon_automatically( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE define your coupon code
$coupon_code = 'coupon_code';
// HERE define the product ID
$product_id = 10;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if( $cart_item['product_id'] == $product_id ) {
$cart->add_discount( $coupon_code );
break; // Stop the loop
}
}
}
Generation ID:cmpl-6FF4zsQ1nShUgd8DCHuirUSy8OIm2
Prompt: make a shortcode that displays the total number of sales from completed orders that a WooCommerce product with the category of "donation" has
add_shortcode('total_donations', 'total_donations');
function total_donations() {
$args = array(
'post_type' => 'product',
'product_cat' => 'donation',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
$total = 0;
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$total += $product->get_total_sales();
endwhile;
wp_reset_query();
return $total;
}
Generation ID:cmpl-6FZ3XSTkaScbrTcv9oARLMeLNPvXi
Prompt: if 1 product is added to the cart, add a matching quantity of another and apply a coupon
add_action( 'woocommerce_before_calculate_totals', 'add_free_addon_to_cart', 10, 1 );
function add_free_addon_to_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$free_addon_id = 15;
$free_addon_coupon = 'free_addon';
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == 11 ) {
$free_addon_qty = $cart_item['quantity'];
$free_addon_key = $cart->generate_cart_id( $free_addon_id );
// Check if free addon is already in cart
if ( $cart->find_product_in_cart( $free_addon_key ) ) {
// Update free addon quantity
$cart_item['quantity'] = $free_addon_qty;
} else {
// Add free addon
$cart->add_to_cart( $free_addon_id, $free_addon_qty );
}
// Check if free addon coupon is already applied
if ( ! $cart->has_discount( $free_addon_coupon ) ) {
// Apply free addon coupon
$cart->add_discount( $free_addon_coupon );
}
}
}
}
Generation ID:cmpl-6FwshdsBsbUrSfMdNbt8E7icvWI1n
Prompt: get total purchase of a specific product from woocommerce order
$product_id = '123';
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_customer_user',
'value' => $user_id,
'compare' => '='
)
)
);
$orders = get_posts($args);
$total_purchase = 0;
foreach ($orders as $order) {
$order_id = $order->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item['product_id'];
$product_name = $item['name'];
$product_quantity = $item['qty'];
$product_total = $item['line_total'];
if ($product_id == '123') {
$total_purchase += $product_total;
}
}
}
echo $total_purchase;
Generation ID:cmpl-6FxpLOjHcsNvjcq3jP59plVsXEDR4
Prompt: only allow WooCommerce coupon code "demo_coupon" to be applied if all products in the cart have the tag "demo"
add_filter( 'woocommerce_coupon_is_valid', 'woocommerce_coupon_is_valid_for_demo_products', 10, 2 );
function woocommerce_coupon_is_valid_for_demo_products( $valid, $coupon ) {
if ( ! $valid ) return $valid;
if ( $coupon->code != 'demo_coupon' ) return $valid;
$cart_items = WC()->cart->get_cart();
foreach ( $cart_items as $cart_item ) {
if ( ! has_term( 'demo', 'product_tag', $cart_item['product_id'] ) ) {
$valid = false;
wc_add_notice( __( '"demo_coupon" can only be added when all products in the cart have the tag "demo".', 'woocommerce' ), 'error' );
break;
}
}
return $valid;
}
Generation ID:cmpl-6FYvI2GRVfZisWW0NOXLg8gcQeB6L