简体   繁体   English

Woocommerce:禁用预约取消 URL 地址的登录要求 [wp nonce]

[英]Woocommerce: Disable login requirement for appointment cancelation URL adress [wp nonce]

I am using woocommerce + plugin for appointments calendar.我正在使用 woocommerce + 插件作为约会日历。 People can book their appointment right from the product and create a purchase.人们可以直接从产品中预约并创建购买。

If they want to cancel this appointment they can do it from my-account area by clicking at cancel button.如果他们想取消这次约会,他们可以通过单击取消按钮从我的帐户区域进行。

Cancelation URL adress slug is generated by system and it looks like this:取消 URL 地址 slug 是由系统生成的,它看起来像这样:

/?cancel_appointment=true&appointment_id=442&redirect&_wpnonce=c328e0bab6

When you are logged out it does not work and when you are logged in your appointment will change status to canceled.当您注销时它不起作用,当您登录时,您的约会状态将更改为已取消。

What I need to do is to change the requirement of the URL authorization so when you are logged out it will let you cancel the appointment because here is creating of accounts really annoying for the customers.我需要做的是更改 URL 授权的要求,因此当您注销时,它会让您取消约会,因为这里创建的帐户对客户来说真的很烦人。

This is a part of the code I have found in files of the plugin.这是我在插件文件中找到的代码的一部分。 Maybe it should help.也许它应该有所帮助。

/**
 * Returns the cancel URL for an appointment
 *
 * @param string $redirect
 *
 * @return string
 */
public function get_cancel_url( $redirect = '' ) {
    $cancel_page = get_permalink( wc_get_page_id( '' ) );

    if ( ! $cancel_page ) {
        $cancel_page = home_url();
    }

    return apply_filters(
        'appointments_cancel_appointment_url',
        wp_nonce_url(
            add_query_arg(
                array(
                    'cancel_appointment' => 'true',
                    'appointment_id'     => $this->get_id(),
                    'redirect'           => $redirect,
                ),
                $cancel_page
            ),
            'woocommerce-appointments-cancel_appointment'
        ),
        $this
    );
}

Another code I have found is here我发现的另一个代码是here

<?php
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
* Handle frontend forms
*/
class WC_Appointment_Form_Handler {

/**
 * Hook in methods
 */
public static function init() {
    add_action( 'init', array( __CLASS__, 'cancel_appointment' ), 20 );
}

/**
 * Cancel an appointment.
 */
public static function cancel_appointment() {
    if ( isset( $_GET['cancel_appointment'] ) && isset( 
 $_GET['appointment_id'] ) ) {

        $appointment_id         = absint( $_GET['appointment_id'] );
        $appointment            = get_wc_appointment( $appointment_id );
        $appointment_can_cancel = $appointment->has_status( get_wc_appointment_statuses( 'cancel' ) );
        $redirect               = $_GET['redirect'];
        $is_wc_appointment      = is_a( $appointment, 'WC_Appointment' ) ? true : false;

        if ( $appointment->has_status( 'cancelled' ) ) {
            // Message: Already cancelled - take no action.
            wc_add_notice( __( 'Your appointment has already been cancelled.', 'woocommerce-appointments' ), 'notice' );

        } elseif ( $is_wc_appointment && $appointment_can_cancel && $appointment->get_id() == $appointment_id && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-appointments-cancel_appointment' ) ) {
            // Cancel the appointment
            $appointment->update_status( 'cancelled' );
            WC_Cache_Helper::get_transient_version( 'appointments', true );

            // Message.
            wc_add_notice( apply_filters( 'woocommerce_appointment_cancelled_notice', __( 'Your appointment has been cancelled.', 'woocommerce-appointments' ) ), apply_filters( 'woocommerce_appointment_cancelled_notice_type', 'notice' ) );

            do_action( 'woocommerce_appointments_cancelled_appointment', $appointment->get_id() );
        } elseif ( ! $appointment_can_cancel ) {
            wc_add_notice( __( 'Your appointment can no longer be cancelled. Please contact us if you need assistance.', 'woocommerce-appointments' ), 'error' );
        } else {
            wc_add_notice( __( 'Invalid appointment.', 'woocommerce-appointments' ), 'error' );
        }

        if ( $redirect ) {
            wp_safe_redirect( $redirect );
            exit;
        }
    }
}
}

WC_Appointment_Form_Handler::init();

If you need anything else from the code I may search for it.如果您需要代码中的任何其他内容,我可以搜索它。 Because this is the first time I am facing the problem like this.因为这是我第一次遇到这样的问题。

Thank you everyone for your help.感谢大家的帮助。

Fixed by code edit from通过代码编辑修复

elseif ( $is_wc_appointment && $appointment_can_cancel && $appointment->get_id() == $appointment_id && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-appointments-cancel_appointment' ) ) {

to

elseif ( $is_wc_appointment && $appointment_can_cancel && $appointment->get_id() == $appointment_id) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM