在WooCommerce中,我添加了一个定制产品类型来销售Google Meet网络研讨会.

  • 首先,我已经添加了一个领域的管理产品页面,以放置谷歌会议链接(工程).
  • 然后,当用户购买产品时,网络研讨会链接将显示在WooCommerce订单详细信息页面和感谢页面上(部分有效).
  • 最后,一旦用户购买了产品,我将添加到购物车的按钮替换为一个自定义按钮,以"加入网络研讨会"链接到网络研讨会(不起作用).

我一直在努力修它,但是我没有修好它!

我还有以下几个问题:

  1. "谢谢"页面实际上正在崩溃.
  2. 购买产品后,添加到购物车按钮不会被"加入网络研讨会"文本所取代.

如有任何帮助,我们不胜感激.

到目前为止我的代码如下:

// 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);

推荐答案

我几乎完全重温了您的代码,并设法修复了您的问题.

对于用户购买的产品的添加到购物车按钮,它现在被一个类似的自定义按钮"加入网络研讨会"所取代,该按钮链接到谷歌网络研讨会.

重新访问的代码(updated):

// Admin Product - Add Google Meet link field
add_action('woocommerce_product_options_general_product_data', 'add_google_meet_link_custom_field');
function add_google_meet_link_custom_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>';
}

// Admin Product - Save Google Meet link field data
add_action('woocommerce_admin_process_product_object', 'save_google_meet_link_custom_field');
function save_google_meet_link_custom_field( $product ) {
    if ( isset($_POST['google_meet_link']) ) {
        $product->update_meta_data( 'google_meet_link', sanitize_url($_POST['google_meet_link']) );
    } 
}

// Display Google Meet link on order details and thank you page (clickable)
add_action('woocommerce_order_details_after_order_table', 'display_google_meet_link_custom_field_value', 10, 1);
function display_google_meet_link_custom_field_value( $order ) {
    $has_link = false;
    $html     = '';

    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        $google_meet_link = $product->get_meta('google_meet_link');

        if ( $google_meet_link ) {
            $html .= '<p><a href="' . esc_url($google_meet_link) . '">' . esc_html($google_meet_link) . '</a></p>';
            $has_link = true;
        }
    }
    
    if ( $has_link ) {
        echo '<h3>' . __('Google Meet Link:', 'text-domain') . '</h3>';
        echo $html;
    }
}

// Function that check if the webinar link exist or return webinar link
function get_webinar_link( $product, $conditional = false ) {
    $link = $product->get_meta('google_meet_link');

    if ( $conditional ) {
        return ! $link ? false : true;
    } else {
        return $link;
    }
}

// The custom replacement button function
function join_webinar_button( $product = null ){
    if ( ! $product ) global $product;

    // HERE your custom button text and link
    $button_text = __('Join Webinar', 'woocommerce');
    $button_link = get_webinar_link( $product );
    
    // Display button
    echo '<a class="button webinar-on" href="'.$button_link.'">' . $button_text . '</a>';
}


// Function that check if the current product has been purchased by the customer
function has_purchased_product( $product_id = null, $user_id = null ) {
    global $wpdb;

    $product_id = ! $product_id ? get_the_ID() : $product_id;
    $user_id = ! $user_id ? get_current_user_id() : $user_id;

    // Returns an array of product Ids reviewed by the customer
    return $wpdb->get_var( "
        SELECT  COUNT( opl.order_item_id )
        FROM {$wpdb->prefix}wc_order_product_lookup as opl
        INNER JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = opl.order_id 
        WHERE pm.meta_key = '_customer_user' AND pm.meta_value = '{$user_id}' 
            AND opl.product_id = '{$product_id}'
    " );
}

// Replacing single product button add to cart by a custom button
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    global $product;

    // Targeting our product type and checking that the product is purchased by user
    if ( $product->is_type( 'simple' ) && get_webinar_link( $product, true ) && // 'google_meet_webinar'
        has_purchased_product( $product->get_id() ) ) 
    {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
         add_action( 'woocommerce_single_product_summary', 'join_webinar_button', 30 );
    }
}

// Shop and archives pages: Replacing add to cart button by a link to the product
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Targeting our product type and checking that the product is purchased by user
    if ( $product->is_type( 'simple' ) && get_webinar_link( $product, true ) && // 'google_meet_webinar'
        has_purchased_product( $product->get_id() ) ) 
    {
        $button = join_webinar_button( $product );
    }
    return $button;
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

Php相关问答推荐

在AJX请求中使用简写后,FormData出现非法调用错误

wp_enqueue_scripts not loading(基于类的插件)

创建一个新的可变产品及其属性以用于WooCommerce中的变体

将购买限制在WooCommerce中具有相同产品属性的项目

添加登录:需要app.yaml仍然允许所有拥有Google帐户的人访问

在Laravel中为表添加前缀,以将它们逻辑地分组

将客户重定向到自定义感谢页后的空白页

Symfony 6.2 IsGraned具有多个角色

如果为布尔值,则 for each 循环重新启动

表单提交返回空白页

在WooCommerce存档页面中显示可变产品的库存变化属性

Font Awesome 加载图标未显示在 WooCommerce 管理员列表中

如何使用 AJAX、PHP 和 MYSQL 在 Select 项目时在文本框中填充数据

在WooCommerce购物车和 checkout 页面中更改总计文本

如何在自定义消息中包含 WooCommerce 选定的产品变体详细信息

PHP 日期格式:ISO8601 与 ISO8601_EXPANDED

Laravel 模型将日期保存为空

从 CSV 文件 seeder 数据库时的额外行

在 DomPDF 和 Laravel 中使用内联 css

WordPress 函数 get_post(post_id) 是否查询数据库?