/** * Checks the cart to see if it contains a subscription product renewal for a failed renewal payment. * * @param bool | Array The cart item containing the renewal, else false. * @return string * @since 2.0 */ function wcs_cart_contains_failed_renewal_order_payment() { $contains_renewal = false; $cart_item = wcs_cart_contains_renewal(); if (false !== $cart_item && isset($cart_item['subscription_renewal']['renewal_order_id'])) { $renewal_order = wc_get_order($cart_item['subscription_renewal']['renewal_order_id']); if ($renewal_order->has_status('failed')) { $contains_renewal = $cart_item; } } return apply_filters('wcs_cart_contains_failed_renewal_order_payment', $contains_renewal); }
/** * Check is a subscription coupon is valid before applying * * @since 1.2 */ public static function validate_subscription_coupon($valid, $coupon) { self::$coupon_error = ''; // ignore non-subscription coupons if (!in_array($coupon->type, array('recurring_fee', 'sign_up_fee', 'recurring_percent', 'sign_up_fee_percent'))) { // but make sure there is actually something for the coupon to be applied to (i.e. not a free trial) if (0 == WC()->cart->subtotal) { self::$coupon_error = __('Sorry, this coupon is only valid for an initial payment and the cart does not require an initial payment.', 'woocommerce-subscriptions'); } } else { // prevent subscription coupons from being applied to renewal payments if (wcs_cart_contains_renewal()) { self::$coupon_error = __('Sorry, this coupon is only valid for new subscriptions.', 'woocommerce-subscriptions'); } // prevent subscription coupons from being applied to non-subscription products if (!wcs_cart_contains_renewal() && !WC_Subscriptions_Cart::cart_contains_subscription()) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products.', 'woocommerce-subscriptions'); } // prevent sign up fee coupons from being applied to subscriptions without a sign up fee if (0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() && in_array($coupon->type, array('sign_up_fee', 'sign_up_fee_percent'))) { self::$coupon_error = __('Sorry, this coupon is only valid for subscription products with a sign-up fee.', 'woocommerce-subscriptions'); } } if (!empty(self::$coupon_error)) { $valid = false; add_filter('woocommerce_coupon_error', __CLASS__ . '::add_coupon_error', 10); } return $valid; }
/** * Check if the cart includes a subscription that needs to be synced. * * @return bool Returns true if any item in the cart is a subscription sync request, otherwise, false. * @since 1.5 */ public static function cart_contains_synced_subscription($cart = null) { $cart = empty($cart) && isset(WC()->cart) ? WC()->cart : $cart; $contains_synced = false; if (self::is_syncing_enabled() && !empty($cart) && !wcs_cart_contains_renewal()) { foreach ($cart->cart_contents as $cart_item_key => $cart_item) { if (!is_array($cart_item['data']->subscription_payment_sync_date) && $cart_item['data']->subscription_payment_sync_date > 0 || is_array($cart_item['data']->subscription_payment_sync_date) && $cart_item['data']->subscription_payment_sync_date['day'] > 0) { $contains_synced = $cart_item; break; } } } return $contains_synced; }
/** * Force tokenization for subscriptions, this can be forced either during checkout * or when the payment method for a subscription is being changed * * @since 4.1.0 * @see SV_WC_Payment_Gateway::tokenization_forced() * @param bool $force_tokenization whether tokenization should be forced * @return bool true if tokenization should be forced, false otherwise */ public function maybe_force_tokenization($force_tokenization) { // pay page with subscription? $pay_page_subscription = false; if ($this->get_gateway()->is_pay_page_gateway()) { $order_id = $this->get_gateway()->get_checkout_pay_page_order_id(); if ($order_id) { $pay_page_subscription = wcs_order_contains_subscription($order_id); } } if (WC_Subscriptions_Cart::cart_contains_subscription() || wcs_cart_contains_renewal() || WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment || $pay_page_subscription) { $force_tokenization = true; } return $force_tokenization; }
/** * Checks the cart to see if it contains a subscription product renewal. * * Returns the cart_item containing the product renewal, else false. * * @deprecated 2.0 * @since 1.3 */ public static function cart_contains_subscription_renewal($role = '') { _deprecated_function(__METHOD__, '2.0', 'wcs_cart_contains_renewal( $role )'); return wcs_cart_contains_renewal($role); }
/** * If a product is being marked as not purchasable because it is limited and the customer has a subscription, * but the current request is to resubscribe to the subscription, then mark it as purchasable. * * @since 2.0 * @return bool */ public function is_purchasable($is_purchasable, $product) { // If the product is being set as not-purchasable by Subscriptions (due to limiting) if (false === $is_purchasable && false === WC_Subscriptions_Product::is_purchasable($is_purchasable, $product)) { // Adding to cart from the product page or paying for a renewal if (isset($_GET[$this->cart_item_key]) || isset($_GET['subscription_renewal']) || wcs_cart_contains_renewal()) { $is_purchasable = true; } else { if (WC()->session->cart) { foreach (WC()->session->cart as $cart_item_key => $cart_item) { if ($product->id == $cart_item['product_id'] && isset($cart_item['subscription_renewal'])) { $is_purchasable = true; break; } } } } } return $is_purchasable; }
/** * Checks the cart to see if it contains a subscription renewal item. * * @see wcs_cart_contains_renewal() * @return bool | Array The cart item containing the renewal, else false. * @since 2.0.10 */ protected function cart_contains() { return wcs_cart_contains_renewal(); }
/** * When a subscription is added to the cart, remove other products/subscriptions to * work with PayPal Standard, which only accept one subscription per checkout. * * If multiple purchase flag is set, allow them to be added at the same time. * * @since 1.0 */ public static function maybe_empty_cart($valid, $product_id, $quantity) { $is_subscription = WC_Subscriptions_Product::is_subscription($product_id); $cart_contains_subscription = WC_Subscriptions_Cart::cart_contains_subscription(); $multiple_subscriptions_possible = WC_Subscriptions_Payment_Gateways::one_gateway_supports('multiple_subscriptions'); $manual_renewals_enabled = 'yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals', 'no') ? true : false; if ($is_subscription && 'yes' != get_option(WC_Subscriptions_Admin::$option_prefix . '_multiple_purchase', 'no')) { WC()->cart->empty_cart(); } elseif ($is_subscription && wcs_cart_contains_renewal() && !$multiple_subscriptions_possible && !$manual_renewals_enabled) { self::remove_subscriptions_from_cart(); self::add_notice(__('A subscription renewal has been removed from your cart. Multiple subscriptions can not be purchased at the same time.', 'woocommerce-subscriptions'), 'notice'); } elseif ($is_subscription && $cart_contains_subscription && !$multiple_subscriptions_possible && !$manual_renewals_enabled) { self::remove_subscriptions_from_cart(); self::add_notice(__('A subscription has been removed from your cart. Due to payment gateway restrictions, different subscription products can not be purchased at the same time.', 'woocommerce-subscriptions'), 'notice'); } elseif ($cart_contains_subscription && 'yes' != get_option(WC_Subscriptions_Admin::$option_prefix . '_multiple_purchase', 'no')) { self::remove_subscriptions_from_cart(); self::add_notice(__('A subscription has been removed from your cart. Products and subscriptions can not be purchased at the same time.', 'woocommerce-subscriptions'), 'notice'); // Redirect to cart page to remove subscription & notify shopper add_filter('add_to_cart_fragments', __CLASS__ . '::redirect_ajax_add_to_cart'); } return $valid; }
/** * When completing checkout for a subscription renewal, update the address on the subscription to use * the shipping/billing address entered in case it has changed since the subscription was first created. * * @since 2.0 */ public function maybe_update_subscription_customer_data($update_customer_data, $checkout_object) { $cart_renewal_item = wcs_cart_contains_renewal(); if (false !== $cart_renewal_item) { $subscription = wcs_get_subscription($cart_renewal_item[$this->cart_item_key]['subscription_id']); $billing_address = array(); if ($checkout_object->checkout_fields['billing']) { foreach (array_keys($checkout_object->checkout_fields['billing']) as $field) { $field_name = str_replace('billing_', '', $field); $billing_address[$field_name] = $checkout_object->get_posted_address_data($field_name); } } $shipping_address = array(); if ($checkout_object->checkout_fields['shipping']) { foreach (array_keys($checkout_object->checkout_fields['shipping']) as $field) { $field_name = str_replace('shipping_', '', $field); $shipping_address[$field_name] = $checkout_object->get_posted_address_data($field_name, 'shipping'); } } $subscription->set_address($billing_address, 'billing'); $subscription->set_address($shipping_address, 'shipping'); } return $update_customer_data; }
/** * Add scripts */ public function scripts() { $enqueue_scripts = is_cart() || is_checkout() || is_checkout_pay_page(); if (!apply_filters('woocommerce_amazon_pa_enqueue_scripts', $enqueue_scripts)) { return; } $type = 'yes' == $this->settings['enable_login_app'] ? 'app' : 'standard'; wp_enqueue_style('amazon_payments_advanced', plugins_url('assets/css/style.css', __FILE__)); wp_enqueue_script('amazon_payments_advanced_widgets', WC_Amazon_Payments_Advanced_API::get_widgets_url(), array(), '1.0', true); wp_enqueue_script('amazon_payments_advanced', plugins_url('assets/js/amazon-' . $type . '-widgets.js', __FILE__), array(), '1.0', true); $redirect_page = is_cart() ? add_query_arg('amazon_payments_advanced', 'true', get_permalink(woocommerce_get_page_id('checkout'))) : add_query_arg('amazon_payments_advanced', 'true'); $params = array('seller_id' => $this->settings['seller_id'], 'reference_id' => $this->reference_id, 'redirect' => esc_url_raw($redirect_page), 'is_checkout_pay_page' => is_checkout_pay_page(), 'is_checkout' => is_checkout(), 'access_token' => $this->access_token); if ('yes' == $this->settings['enable_login_app']) { $params['button_type'] = 'LwA'; $params['button_color'] = 'Gold'; $params['button_size'] = 'small'; $params['checkout_url'] = esc_url_raw(get_permalink(woocommerce_get_page_id('checkout'))); } if (class_exists('WC_Subscriptions_Cart')) { $cart_contains_subscription = WC_Subscriptions_Cart::cart_contains_subscription() || wcs_cart_contains_renewal(); $change_payment_for_subscription = isset($_GET['change_payment_method']) && wcs_is_subscription(absint($_GET['change_payment_method'])); $params['is_recurring'] = $cart_contains_subscription || $change_payment_for_subscription; } $params = array_map('esc_js', apply_filters('woocommerce_amazon_pa_widgets_params', $params)); wp_localize_script('amazon_payments_advanced', 'amazon_payments_advanced_params', $params); }