• If you want to use discount coupons in a specific event, or restricted, this code helps you.

    This code creates a product in woocommerce when you create an event. This will make it easier for you to define specific discount codes

    Add it to child-theme/functions.php

    function create_woocommerce_product_from_event($post_id, $post, $update) {
    // Verify that it is an event from The Events Calendar
    if ('tribe_events' !== $post->post_type) {
    return;
    }

    // Avoid infinite loops
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return;
    }

    // Extract event data
    $event_title = $post->post_title;
    $event_description = $post->post_content;
    $start_date = get_post_meta($post_id, '_EventStartDate', true);
    $event_price = get_post_meta($post_id, '_EventCost', true); // Assuming the event has a cost.

    // Create or update a product in WooCommerce
    $product_id = get_post_meta($post_id, '_woocommerce_product', true);

    $product_args = array(
    'post_title' => $event_title,
    'post_content' => $event_description,
    'post_status' => 'publish',
    'post_type' => 'product',
    );

    if ($product_id) {
    // Update the product if it already exists
    $product_args['ID'] = $product_id;
    wp_update_post($product_args);
    } else {
    // Create a new product
    $product_id = wp_insert_post($product_args);
    update_post_meta($post_id, '_woocommerce_product', $product_id);
    }

    // Configure product metadata
    update_post_meta($product_id, '_regular_price', $event_price ? $event_price : '0'); // Product price
    update_post_meta($product_id, '_price', $event_price ? $event_price : '0');
    update_post_meta($product_id, '_stock_status', 'instock');
    update_post_meta($product_id, '_virtual', 'no');
    update_post_meta($product_id, '_tribe_event_id', $post_id); // Relationship with the event
    }
    add_action('save_post', 'create_woocommerce_product_from_event', 10, 3);
  • You must be logged in to reply to this topic.