Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created February 13, 2020 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/62f1cc61462594aabc059e104d167c7a to your computer and use it in GitHub Desktop.
Save helgatheviking/62f1cc61462594aabc059e104d167c7a to your computer and use it in GitHub Desktop.
Add Name Your Price support to Braintree Subscription product types

Name Your Price Compatibility for Braintree Subscriptions

Contributors: Kathy Darling
Requires at least: 5.4.0
Tested up to: 5.4.0
Stable tag: 1.0.0 License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
WC requires at least: 3.9.0
WC tested up to: 3.9.0

Add Name Your Price support to Braintree Subscription product types

Description

The WooCommerce Name Your Price extension lets you be flexible in what price you are willing to accept for selected products.

The Braintree for WooCommerce gateway supports subscriptions set up as plans in your Braintree account. The Braintree subscription product types are not officially supported by Name Your Price, but the two plugins can be made compatible with this plugin.

Support

This compatibility plugin does not receive official, priority support as is provided as is.

<?php
/**
* Plugin Name: Name Your Price Compatibility for Braintree Subscriptions
* Plugin URI: https://woocommerce.com/products/name-your-price/?aff=5151&cid=4951026
* Description: Add Name Your Price support to Braintree Subscription product types
* Version: 1.0.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com/
* Requires at least: 5.4.0
* Tested up to: 5.3.0
* WC requires at least: 3.9.0
* WC tested up to: 3.9.0
*
* Copyright: © 2020 Kathy Darling
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add NYP supported "simple" types.
*
* @param array
* @return array
*/
function kia_add_braintree_simple_supported_type( $types ) {
$types[] = 'braintree-subscription-variation';
$types[] = 'braintree-subscription';
return $types; }
add_filter( 'wc_nyp_simple_supported_types', 'kia_add_braintree_simple_supported_type' );
/**
* Add NYP supported "variable" types.
* @param array
* @return array
*/
function kia_add_braintree_variable_supported_type( $types ) {
$types[] = 'braintree-variable-subscription';
return $types;
}
add_filter( 'wc_nyp_variable_supported_types', 'kia_add_braintree_variable_supported_type' );
/**
* Adjust the product based on cart session data.
*
* @param array $cart_item $cart_item['data'] is product object in session
* @param array $values cart item array
* @return array
*/
function kia_add_braintree_subscription_price_prop( $cart_item, $values ) {
if ( $cart_item['data']->is_type( array( 'braintree-subscription', braintree-subscription-variation' ) ) && isset( $values['nyp'] ) && is_callable( array( $cart_item['data'], 'set_subscription_price' ) ) ) {
$cart_item['data']->set_subscription_price( $values['nyp'] );
}
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'kia_add_braintree_subscription_price_prop', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment