/**
  * Place a previous order again.
  */
 public static function order_again()
 {
     // Nothing to do
     if (!isset($_GET['order_again']) || !is_user_logged_in() || !isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'woocommerce-order_again')) {
         return;
     }
     // Clear current cart
     WC()->cart->empty_cart();
     // Load the previous order - Stop if the order does not exist
     $order = wc_get_order(absint($_GET['order_again']));
     if (!$order->get_id()) {
         return;
     }
     if (!$order->has_status(apply_filters('woocommerce_valid_order_statuses_for_order_again', array('completed')))) {
         return;
     }
     // Make sure the user is allowed to order again. By default it check if the
     // previous order belonged to the current user.
     if (!current_user_can('order_again', $order->get_id())) {
         return;
     }
     // Copy products from the order to the cart
     foreach ($order->get_items() as $item) {
         // Load all product info including variation data
         $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $item->get_product_id());
         $quantity = $item->get_quantity();
         $variation_id = $item->get_variation_id();
         $variations = array();
         $cart_item_data = apply_filters('woocommerce_order_again_cart_item_data', array(), $item, $order);
         foreach ($item->get_meta_data() as $meta) {
             if (taxonomy_is_product_attribute($meta->meta_key)) {
                 $variations[$meta->meta_key] = $meta->meta_value;
             } elseif (meta_is_product_attribute($meta->meta_key, $meta->meta_value, $product_id)) {
                 $variations[$meta->meta_key] = $meta->meta_value;
             }
         }
         // Add to cart validation
         if (!apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data)) {
             continue;
         }
         WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations, $cart_item_data);
     }
     do_action('woocommerce_ordered_again', $order->get_id());
     // Redirect to cart
     wc_add_notice(__('The cart has been filled with the items from your previous order.', 'woocommerce'));
     wp_safe_redirect(wc_get_cart_url());
     exit;
 }
Example #2
0
 /**
  * Set up cart item meta data for a to complete a subscription renewal via the cart.
  *
  * @since 2.0
  */
 protected function setup_cart($subscription, $cart_item_data)
 {
     WC()->cart->empty_cart(true);
     $success = true;
     foreach ($subscription->get_items() as $item_id => $line_item) {
         // Load all product info including variation data
         $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $line_item['product_id']);
         $quantity = (int) $line_item['qty'];
         $variation_id = (int) $line_item['variation_id'];
         $variations = array();
         foreach ($line_item['item_meta'] as $meta_name => $meta_value) {
             if (taxonomy_is_product_attribute($meta_name)) {
                 $variations[$meta_name] = $meta_value[0];
             } elseif (meta_is_product_attribute($meta_name, $meta_value[0], $product_id)) {
                 $variations[$meta_name] = $meta_value[0];
             }
         }
         $product = get_product($line_item['product_id']);
         // The notice displayed when a subscription product has been deleted and the custoemr attempts to manually renew or make a renewal payment for a failed recurring payment for that product/subscription
         // translators: placeholder is an item name
         $product_deleted_error_message = apply_filters('woocommerce_subscriptions_renew_deleted_product_error_message', __('The %s product has been deleted and can no longer be renewed. Please choose a new product or contact us for assistance.', 'woocommerce-subscriptions'));
         // Display error message for deleted products
         if (false === $product) {
             wc_add_notice(sprintf($product_deleted_error_message, $line_item['name']), 'error');
             // Make sure we don't actually need the variation ID (if the product was a variation, it will have a variation ID; however, if the product has changed from a simple subscription to a variable subscription, there will be no variation_id)
         } elseif ($product->is_type(array('variable-subscription')) && !empty($line_item['variation_id'])) {
             $variation = get_product($variation_id);
             // Display error message for deleted product variations
             if (false === $variation) {
                 wc_add_notice(sprintf($product_deleted_error_message, $line_item['name']), 'error');
             }
         }
         if (wcs_is_subscription($subscription)) {
             $cart_item_data['subscription_line_item_id'] = $item_id;
         }
         $cart_item_key = WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations, apply_filters('woocommerce_order_again_cart_item_data', array($this->cart_item_key => $cart_item_data), $line_item, $subscription));
         $success = $success && (bool) $cart_item_key;
     }
     // If a product linked to a subscription failed to be added to the cart prevent partially paying for the order by removing all cart items.
     if (!$success && wcs_is_subscription($subscription)) {
         wc_add_notice(sprintf(esc_html__('Subscription #%d has not been added to the cart.', 'woocommerce-subscriptions'), $subscription->id), 'error');
         WC()->cart->empty_cart(true);
     }
     do_action('woocommerce_setup_cart_for_' . $this->cart_item_key, $subscription, $cart_item_data);
 }