In WooCommerce, I have added a custom product type to sale Google meet webinars.
- First, I have added a field to admin product pages to put the Google meet link (works).
- Then, when the user purchase the product, the webinar link will be displayed on WooCommerce order details page and thank you page (works partially).
- To finish, Once the user has purchased the product, I replace the add to cart button with a custom button to "join the webinar" linked to the webinar (doesn't work).
I have been really working hard on it, but I didn’t get it fixed!
I still have the following problems:
- The “Thank You” page is actually crashing.
- After purchasing the product, the add to cart button is not replaced by "join the webinar" text.
Any help is appreciated.
My code so far:
// Add Google Meet link field
function custom_add_google_meet_link_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'google_meet_link',
'label' => __('Google Meet Link', 'text-domain'),
'placeholder' => 'Enter Google Meet Link',
'desc_tip' => 'true',
'description' => __('Add your Google Meet link.', 'text-domain')
)
);
echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'custom_add_google_meet_link_field');
// Save Google Meet link field data
function custom_save_google_meet_link_field_data($product_id) {
$google_meet_link = isset($_POST['google_meet_link']) ? sanitize_text_field($_POST['google_meet_link']) : '';
update_post_meta($product_id, 'google_meet_link', $google_meet_link);
}
add_action('woocommerce_process_product_meta', 'custom_save_google_meet_link_field_data');
// Display Google Meet link on order details and thank you page (clickable)
function custom_display_google_meet_link($order) {
$order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item['product_id'];
$google_meet_link = get_post_meta($product_id, 'google_meet_link', true);
if (!empty($google_meet_link)) {
echo '<h3>' . __('Google Meet Link:', 'text-domain') . '</h3>';
echo '<p><a href="' . esc_url($google_meet_link) . '">' . esc_html($google_meet_link) . '</a></p>';
}
}
}
add_action('woocommerce_order_details_after_order_table', 'custom_display_google_meet_link', 10, 1);
add_action('woocommerce_thankyou', 'custom_display_google_meet_link', 10, 1);
// Change "Add to Cart" button to "Join Webinar"
function custom_change_add_to_cart_button($button_text, $product) {
if ($product->get_type() === 'google_meet_webinar' && $product->is_purchasable()) {
$button_text = __('Join Webinar', 'text-domain');
}
return $button_text;
}
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_change_add_to_cart_button', 10, 2);
add_filter('woocommerce_product_add_to_cart_text', 'custom_change_add_to_cart_button', 10, 2);
I have revisited your code nearly completely and managed to fix your issues.
For the add to cart button of the purchased product by the user, it's now replaced by a similar custom button "join the webinar" linked to the Google webinar.
The revisited code (updated):
Code goes in functions.php file of your active child theme (or active theme). Tested and works.