BuddyDev

Search

Replies

  • Participant
    Level: Initiated
    Posts: 2
    Owen Barder on #43047

    In case anyone is looking for the answer, I solved this problem with the following code (add it to your theme functions.php file). This code loads the relevant JS and CSS scripts on the view-subscriptions page:

     
    add_action( 'wp_enqueue_scripts', 'sb_enqueue_scripts' );
    function sb_enqueue_scripts( $hook ) {
      	
    	if (strpos($_SERVER['REQUEST_URI'], "/view-subscription/") !== false)
    	{
    		enqueue_wc_subscriptions_frontend_scripts();
    	}
    }
    
    function enqueue_wc_subscriptions_frontend_scripts() {
    		
    	$dependencies = array( 'jquery' );
    
    			global $wp;
    	
    			$subscription   = wcs_get_subscription( $wp->query_vars['view-subscription'] );
    
    			if ( $subscription && current_user_can( 'view_order', $subscription->get_id() ) ) {
    			
    				$dependencies[] = 'jquery-blockui';
    				$script_params  = array(
    					'ajax_url'               => esc_url( WC()->ajax_url() ),
    					'subscription_id'        => $subscription->get_id(),
    					'add_payment_method_msg' => __( 'To enable automatic renewals for this subscription, you will first need to add a payment method.', 'woocommerce-subscriptions' ) . "\n\n" . __( 'Would you like to add a payment method now?', 'woocommerce-subscriptions' ),
    					'auto_renew_nonce'       => WCS_My_Account_Auto_Renew_Toggle::can_user_toggle_auto_renewal( $subscription ) ? wp_create_nonce( "toggle-auto-renew-{$subscription->get_id()}" ) : false,
    					'add_payment_method_url' => esc_url( $subscription->get_change_payment_method_url() ),
    					'has_payment_gateway'    => $subscription->has_payment_gateway() && wc_get_payment_gateway_by_order( $subscription )->supports( 'subscriptions' ),
    				);
    				
    				wp_enqueue_script( 'wcs-view-subscription', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js', $dependencies, WC_Subscriptions::$version, true );
    				
    				wp_localize_script( 'wcs-view-subscription', 'WCSViewSubscription', apply_filters( 'woocommerce_subscriptions_frontend_view_subscription_script_parameters', $script_params ) );
    				
    			 }
    } // end function 
    
  • Participant
    Level: Initiated
    Posts: 2
    Owen Barder on #42996

    Thanks Brajesh.

    Yes, it is the WooCommerce subscription plugin. This includes an ajax-enabled toggle to automatically renew (or not) a subscription.

    On the front end, the plugin adds a subscription tab, which shows a table of subscriptions. If you click to see the details, you get taken to a page (‘view-subscription’) which includes this toggle.

    If this page is loaded without the relevant scripts, then the toggle doesn’t work.

    I believe the relevant scripts are here:
    https://github.com/wp-premium/woocommerce-subscriptions/blob/master/woocommerce-subscriptions.php#L369-L400

    As you can see from the extract below, this loads some front-end scripts that I think are not loaded by the BuddyCommerce plugin.

    Is there an easy way to add these scripts to the plugin? If you point me in the right direction I can see if I can work it out. Alternatively I’m happy to give you full access to our development site if you want to take a look or experiment.

    Here is the code fromm Woocommerce subscriptions that I think is relevant:

     
    public static function enqueue_frontend_scripts() {
    		$dependencies = array( 'jquery' );
    
    		if ( is_cart() || is_checkout() ) {
    			wp_enqueue_script( 'wcs-cart', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'assets/js/frontend/wcs-cart.js', $dependencies, WC_Subscriptions::$version, true );
    		} elseif ( is_product() ) {
    			wp_enqueue_script( 'wcs-single-product', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'assets/js/frontend/single-product.js', $dependencies, WC_Subscriptions::$version, true );
    		} elseif ( wcs_is_view_subscription_page() ) {
    			global $wp;
    			$subscription   = wcs_get_subscription( $wp->query_vars['view-subscription'] );
    
    			if ( $subscription && current_user_can( 'view_order', $subscription->get_id() ) ) {
    				$dependencies[] = 'jquery-blockui';
    				$script_params  = array(
    					'ajax_url'               => esc_url( WC()->ajax_url() ),
    					'subscription_id'        => $subscription->get_id(),
    					'add_payment_method_msg' => __( 'To enable automatic renewals for this subscription, you will first need to add a payment method.', 'woocommerce-subscriptions' ) . "\n\n" . __( 'Would you like to add a payment method now?', 'woocommerce-subscriptions' ),
    					'auto_renew_nonce'       => WCS_My_Account_Auto_Renew_Toggle::can_user_toggle_auto_renewal( $subscription ) ? wp_create_nonce( "toggle-auto-renew-{$subscription->get_id()}" ) : false,
    					'add_payment_method_url' => esc_url( $subscription->get_change_payment_method_url() ),
    					'has_payment_gateway'    => $subscription->has_payment_gateway() && wc_get_payment_gateway_by_order( $subscription )->supports( 'subscriptions' ),
    				);
    				wp_enqueue_script( 'wcs-view-subscription', plugin_dir_url( WC_Subscriptions::$plugin_file ) . 'assets/js/frontend/view-subscription.js', $dependencies, WC_Subscriptions::$version, true );
    				wp_localize_script( 'wcs-view-subscription', 'WCSViewSubscription', apply_filters( 'woocommerce_subscriptions_frontend_view_subscription_script_parameters', $script_params ) );
    			}
    		}
    	}