/** * Get standard product data that applies to every product type * * @since 2.1 * @param WC_Product $product * @return array */ private function get_product_data($product) { return array('title' => $product->get_name(), 'id' => $product->get_id(), 'created_at' => $this->server->format_datetime($product->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($product->get_date_modified(), false, true), 'type' => $product->get_type(), 'status' => $product->get_status(), 'downloadable' => $product->is_downloadable(), 'virtual' => $product->is_virtual(), 'permalink' => $product->get_permalink(), 'sku' => $product->get_sku(), 'price' => wc_format_decimal($product->get_price(), 2), 'regular_price' => wc_format_decimal($product->get_regular_price(), 2), 'sale_price' => $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : null, 'price_html' => $product->get_price_html(), 'taxable' => $product->is_taxable(), 'tax_status' => $product->get_tax_status(), 'tax_class' => $product->get_tax_class(), 'managing_stock' => $product->managing_stock(), 'stock_quantity' => $product->get_stock_quantity(), 'in_stock' => $product->is_in_stock(), 'backorders_allowed' => $product->backorders_allowed(), 'backordered' => $product->is_on_backorder(), 'sold_individually' => $product->is_sold_individually(), 'purchaseable' => $product->is_purchasable(), 'featured' => $product->is_featured(), 'visible' => $product->is_visible(), 'catalog_visibility' => $product->get_catalog_visibility(), 'on_sale' => $product->is_on_sale(), 'weight' => $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : null, 'dimensions' => array('length' => $product->get_length(), 'width' => $product->get_width(), 'height' => $product->get_height(), 'unit' => get_option('woocommerce_dimension_unit')), 'shipping_required' => $product->needs_shipping(), 'shipping_taxable' => $product->is_shipping_taxable(), 'shipping_class' => $product->get_shipping_class(), 'shipping_class_id' => 0 !== $product->get_shipping_class_id() ? $product->get_shipping_class_id() : null, 'description' => apply_filters('the_content', $product->get_description()), 'short_description' => apply_filters('woocommerce_short_description', $product->get_short_description()), 'reviews_allowed' => $product->get_reviews_allowed(), 'average_rating' => wc_format_decimal($product->get_average_rating(), 2), 'rating_count' => $product->get_rating_count(), 'related_ids' => array_map('absint', array_values(wc_get_related_products($product->get_id()))), 'upsell_ids' => array_map('absint', $product->get_upsell_ids()), 'cross_sell_ids' => array_map('absint', $product->get_cross_sell_ids()), 'categories' => wc_get_object_terms($product->get_id(), 'product_cat', 'name'), 'tags' => wc_get_object_terms($product->get_id(), 'product_tag', 'name'), 'images' => $this->get_images($product), 'featured_src' => wp_get_attachment_url(get_post_thumbnail_id($product->get_id())), 'attributes' => $this->get_attributes($product), 'downloads' => $this->get_downloads($product), 'download_limit' => $product->get_download_limit(), 'download_expiry' => $product->get_download_expiry(), 'download_type' => 'standard', 'purchase_note' => apply_filters('the_content', $product->get_purchase_note()), 'total_sales' => $product->get_total_sales(), 'variations' => array(), 'parent' => array()); }
/** * Get standard product data that applies to every product type * * @since 2.1 * @param WC_Product $product * @return WC_Product */ private function get_product_data($product) { return array('title' => $product->get_title(), 'id' => (int) $product->is_type('variation') ? $product->get_variation_id() : $product->id, 'created_at' => $this->server->format_datetime($product->get_post_data()->post_date_gmt), 'updated_at' => $this->server->format_datetime($product->get_post_data()->post_modified_gmt), 'type' => $product->product_type, 'status' => $product->get_post_data()->post_status, 'downloadable' => $product->is_downloadable(), 'virtual' => $product->is_virtual(), 'permalink' => $product->get_permalink(), 'sku' => $product->get_sku(), 'price' => $product->get_price(), 'regular_price' => $product->get_regular_price(), 'sale_price' => $product->get_sale_price() ? $product->get_sale_price() : null, 'price_html' => $product->get_price_html(), 'taxable' => $product->is_taxable(), 'tax_status' => $product->get_tax_status(), 'tax_class' => $product->get_tax_class(), 'managing_stock' => $product->managing_stock(), 'stock_quantity' => $product->get_stock_quantity(), 'in_stock' => $product->is_in_stock(), 'backorders_allowed' => $product->backorders_allowed(), 'backordered' => $product->is_on_backorder(), 'sold_individually' => $product->is_sold_individually(), 'purchaseable' => $product->is_purchasable(), 'featured' => $product->is_featured(), 'visible' => $product->is_visible(), 'catalog_visibility' => $product->visibility, 'on_sale' => $product->is_on_sale(), 'product_url' => $product->is_type('external') ? $product->get_product_url() : '', 'button_text' => $product->is_type('external') ? $product->get_button_text() : '', 'weight' => $product->get_weight() ? $product->get_weight() : null, 'dimensions' => array('length' => $product->length, 'width' => $product->width, 'height' => $product->height, 'unit' => get_option('woocommerce_dimension_unit')), 'shipping_required' => $product->needs_shipping(), 'shipping_taxable' => $product->is_shipping_taxable(), 'shipping_class' => $product->get_shipping_class(), 'shipping_class_id' => 0 !== $product->get_shipping_class_id() ? $product->get_shipping_class_id() : null, 'description' => wpautop(do_shortcode($product->get_post_data()->post_content)), 'short_description' => apply_filters('woocommerce_short_description', $product->get_post_data()->post_excerpt), 'reviews_allowed' => 'open' === $product->get_post_data()->comment_status, 'average_rating' => wc_format_decimal($product->get_average_rating(), 2), 'rating_count' => (int) $product->get_rating_count(), 'related_ids' => array_map('absint', array_values($product->get_related())), 'upsell_ids' => array_map('absint', $product->get_upsells()), 'cross_sell_ids' => array_map('absint', $product->get_cross_sells()), 'parent_id' => $product->post->post_parent, 'categories' => wp_get_post_terms($product->id, 'product_cat', array('fields' => 'names')), 'tags' => wp_get_post_terms($product->id, 'product_tag', array('fields' => 'names')), 'images' => $this->get_images($product), 'featured_src' => (string) wp_get_attachment_url(get_post_thumbnail_id($product->is_type('variation') ? $product->variation_id : $product->id)), 'attributes' => $this->get_attributes($product), 'downloads' => $this->get_downloads($product), 'download_limit' => (int) $product->download_limit, 'download_expiry' => (int) $product->download_expiry, 'download_type' => $product->download_type, 'purchase_note' => wpautop(do_shortcode(wp_kses_post($product->purchase_note))), 'total_sales' => metadata_exists('post', $product->id, 'total_sales') ? (int) get_post_meta($product->id, 'total_sales', true) : 0, 'variations' => array(), 'parent' => array(), 'grouped_products' => array()); }
/** * Get standard product data that applies to every product type * * @since 2.1 * @param WC_Product $product * @return WC_Product */ private function get_product_data($product, $fields = null) { if ($fields) { $field_list = explode(',', $fields); } $product_data = array(); if (!$fields || $fields && in_array('title', $field_list)) { $product_data['title'] = $product->get_title(); } if (!$fields || $fields && in_array('id', $field_list)) { $product_data['id'] = (int) $product->is_type('variation') ? $product->get_variation_id() : $product->id; } if (!$fields || $fields && in_array('created_at', $field_list)) { $product_data['created_at'] = $this->server->format_datetime($product->get_post_data()->post_date_gmt); } if (!$fields || $fields && in_array('updated_at', $field_list)) { $product_data['updated_at'] = $this->server->format_datetime($product->get_post_data()->post_modified_gmt); } if (!$fields || $fields && in_array('type', $field_list)) { $product_data['type'] = $product->product_type; } if (!$fields || $fields && in_array('status', $field_list)) { $product_data['status'] = $product->get_post_data()->post_status; } if (!$fields || $fields && in_array('downloadable', $field_list)) { $product_data['downloadable'] = $product->is_downloadable(); } if (!$fields || $fields && in_array('virtual', $field_list)) { $product_data['virtual'] = $product->is_virtual(); } if (!$fields || $fields && in_array('permalink', $field_list)) { $product_data['permalink'] = $product->get_permalink(); } if (!$fields || $fields && in_array('sku', $field_list)) { $product_data['sku'] = $product->get_sku(); } if (!$fields || $fields && in_array('price', $field_list)) { $product_data['price'] = $product->get_price(); } if (!$fields || $fields && in_array('regular_price', $field_list)) { $product_data['regular_price'] = $product->get_regular_price(); } if (!$fields || $fields && in_array('sale_price', $field_list)) { $product_data['sale_price'] = $product->get_sale_price() ? $product->get_sale_price() : null; } if (!$fields || $fields && in_array('price_html', $field_list)) { $product_data['price_html'] = $product->get_price_html(); } if (!$fields || $fields && in_array('taxable', $field_list)) { $product_data['taxable'] = $product->is_taxable(); } if (!$fields || $fields && in_array('tax_status', $field_list)) { $product_data['tax_status'] = $product->get_tax_status(); } if (!$fields || $fields && in_array('tax_class', $field_list)) { $product_data['tax_class'] = $product->get_tax_class(); } if (!$fields || $fields && in_array('managing_stock', $field_list)) { $product_data['managing_stock'] = $product->managing_stock(); } if (!$fields || $fields && in_array('stock_quantity', $field_list)) { $product_data['stock_quantity'] = $product->get_stock_quantity(); } if (!$fields || $fields && in_array('in_stock', $field_list)) { $product_data['in_stock'] = $product->is_in_stock(); } if (!$fields || $fields && in_array('backorders_allowed', $field_list)) { $product_data['backorders_allowed'] = $product->backorders_allowed(); } if (!$fields || $fields && in_array('backordered', $field_list)) { $product_data['backordered'] = $product->is_on_backorder(); } if (!$fields || $fields && in_array('sold_individually', $field_list)) { $product_data['sold_individually'] = $product->is_sold_individually(); } if (!$fields || $fields && in_array('purchaseable', $field_list)) { $product_data['purchaseable'] = $product->is_purchasable(); } if (!$fields || $fields && in_array('featured', $field_list)) { $product_data['featured'] = $product->is_featured(); } if (!$fields || $fields && in_array('visible', $field_list)) { $product_data['visible'] = $product->is_visible(); } if (!$fields || $fields && in_array('catalog_visibility', $field_list)) { $product_data['catalog_visibility'] = $product->visibility; } if (!$fields || $fields && in_array('on_sale', $field_list)) { $product_data['on_sale'] = $product->is_on_sale(); } if (!$fields || $fields && in_array('product_url', $field_list)) { $product_data['product_url'] = $product->is_type('external') ? $product->get_product_url() : ''; } if (!$fields || $fields && in_array('button_text', $field_list)) { $product_data['button_text'] = $product->is_type('external') ? $product->get_button_text() : ''; } if (!$fields || $fields && in_array('weight', $field_list)) { $product_data['weight'] = $product->get_weight() ? $product->get_weight() : null; } if (!$fields || $fields && in_array('dimensions', $field_list)) { $product_data['dimensions'] = array('length' => $product->length, 'width' => $product->width, 'height' => $product->height, 'unit' => get_option('woocommerce_dimension_unit')); } if (!$fields || $fields && in_array('shipping_required', $field_list)) { $product_data['shipping_required'] = $product->needs_shipping(); } if (!$fields || $fields && in_array('shipping_taxable', $field_list)) { $product_data['shipping_taxable'] = $product->is_shipping_taxable(); } if (!$fields || $fields && in_array('shipping_class', $field_list)) { $product_data['shipping_class'] = $product->get_shipping_class(); } if (!$fields || $fields && in_array('shipping_class_id', $field_list)) { $product_data['shipping_class_id'] = 0 !== $product->get_shipping_class_id() ? $product->get_shipping_class_id() : null; } if (!$fields || $fields && in_array('description', $field_list)) { $product_data['description'] = wpautop(do_shortcode($product->get_post_data()->post_content)); } if (!$fields || $fields && in_array('short_description', $field_list)) { $product_data['short_description'] = apply_filters('woocommerce_short_description', $product->get_post_data()->post_excerpt); } if (!$fields || $fields && in_array('reviews_allowed', $field_list)) { $product_data['reviews_allowed'] = 'open' === $product->get_post_data()->comment_status; } if (!$fields || $fields && in_array('average_rating', $field_list)) { $product_data['average_rating'] = wc_format_decimal($product->get_average_rating(), 2); } if (!$fields || $fields && in_array('rating_count', $field_list)) { $product_data['rating_count'] = (int) $product->get_rating_count(); } if (!$fields || $fields && in_array('related_ids', $field_list)) { $product_data['related_ids'] = array_map('absint', array_values($product->get_related())); } if (!$fields || $fields && in_array('upsell_ids', $field_list)) { $product_data['upsell_ids'] = array_map('absint', $product->get_upsells()); } if (!$fields || $fields && in_array('cross_sell_ids', $field_list)) { $product_data['cross_sell_ids'] = array_map('absint', $product->get_cross_sells()); } if (!$fields || $fields && in_array('parent_id', $field_list)) { $product_data['parent_id'] = $product->post->post_parent; } if (!$fields || $fields && in_array('categories', $field_list)) { $product_data['categories'] = wp_get_post_terms($product->id, 'product_cat', array('fields' => 'names')); } if (!$fields || $fields && in_array('tags', $field_list)) { $product_data['tags'] = wp_get_post_terms($product->id, 'product_tag', array('fields' => 'names')); } if (!$fields || $fields && in_array('images', $field_list)) { $product_data['images'] = $this->get_images($product); } if (!$fields || $fields && in_array('featured_src', $field_list)) { $product_data['featured_src'] = (string) wp_get_attachment_url(get_post_thumbnail_id($product->is_type('variation') ? $product->variation_id : $product->id)); } if (!$fields || $fields && in_array('attributes', $field_list)) { $product_data['attributes'] = $this->get_attributes($product); } if (!$fields || $fields && in_array('downloads', $field_list)) { $product_data['downloads'] = $this->get_downloads($product); } if (!$fields || $fields && in_array('download_limit', $field_list)) { $product_data['download_limit'] = (int) $product->download_limit; } if (!$fields || $fields && in_array('download_expiry', $field_list)) { $product_data['download_expiry'] = (int) $product->download_expiry; } if (!$fields || $fields && in_array('download_type', $field_list)) { $product_data['download_type'] = $product->download_type; } if (!$fields || $fields && in_array('purchase_note', $field_list)) { $product_data['purchase_note'] = wpautop(do_shortcode(wp_kses_post($product->purchase_note))); } if (!$fields || $fields && in_array('total_sales', $field_list)) { $product_data['total_sales'] = metadata_exists('post', $product->id, 'total_sales') ? (int) get_post_meta($product->id, 'total_sales', true) : 0; } if (!$fields || $fields && in_array('variations', $field_list)) { $product_data['variations'] = array(); } if (!$fields || $fields && in_array('parent', $field_list)) { $product_data['parent'] = array(); } if (!$fields || $fields && in_array('grouped_products', $field_list)) { $product_data['grouped_products'] = array(); } if (!$fields || $fields && in_array('menu_order', $field_list)) { $product_data['menu_order'] = $product->post->menu_order; } return $product_data; }
?> <?php foreach ($order_summary as $order) { $order = new WC_Order($order->order_id); $valid_items = WCV_Queries::get_products_for_order($order->id); $valid = array(); $needs_shipping = false; $items = $order->get_items(); foreach ($items as $key => $value) { if (in_array($value['variation_id'], $valid_items) || in_array($value['product_id'], $valid_items)) { $valid[] = $value; } // See if product needs shipping $product = new WC_Product($value['product_id']); $needs_shipping = !$product->needs_shipping() || $product->is_downloadable('yes') ? false : true; } $shippers = (array) get_post_meta($order->id, 'wc_pv_shipped', true); $shipped = in_array($user_id, $shippers); ?> <tr> <td><?php echo $order->get_order_number(); ?> </td> <td><?php echo apply_filters('wcvendors_dashboard_google_maps_link', '<a target="_blank" href="' . esc_url('http://maps.google.com/maps?&q=' . urlencode(esc_html(preg_replace('#<br\\s*/?>#i', ', ', $order->get_formatted_shipping_address()))) . '&z=16') . '">' . esc_html(preg_replace('#<br\\s*/?>#i', ', ', $order->get_formatted_shipping_address())) . '</a>'); ?> </td> <td><?php
/** * Prepare recurring amounts, taxes etc for subscription items * * @access public * @param int $order_id * @return void */ public function new_order($order_id) { global $woocommerce; // Iterate over real cart and work with subscription products (if any) foreach ($woocommerce->cart->cart_contents as $cart_item_key => $cart_item) { $id = !empty($cart_item['variation_id']) ? $cart_item['variation_id'] : $cart_item['product_id']; if (Subscriptio_Subscription_Product::is_subscription($id)) { $product = new WC_Product($id); // Store all required renewal order fields here $renewal_order = array( 'taxes' => array(), 'shipping' => array(), ); // Create fake cart to mimic renewal order $renewal_cart = new WC_Cart(); // Add product to cart $renewal_cart->add_to_cart( $cart_item['product_id'], $cart_item['quantity'], (isset($cart_item['variation_id']) ? $cart_item['variation_id'] : ''), (isset($cart_item['variation']) ? $cart_item['variation'] : '') ); // Get fake cart item key $renewal_cart_item_keys = array_keys($renewal_cart->cart_contents); $renewal_cart_item_key = array_shift($renewal_cart_item_keys); // Set renewal price $renewal_cart->cart_contents[$renewal_cart_item_key]['data']->price = Subscriptio_Subscription_Product::get_recurring_price($id); // Add shipping if ($product->needs_shipping() && $renewal_cart->needs_shipping()) { // Get instance of checkout object to retrieve shipping options $wc_checkout = WC_Checkout::instance(); // Iterate over shipping packages foreach ($woocommerce->shipping->get_packages() as $package_key => $package) { // Check if this rate was selected if (isset($package['rates'][$wc_checkout->shipping_methods[$package_key]])) { // Check if it contains current subscription if (isset($package['contents'][$cart_item_key])) { // Save shipping details for further calculation $shipping_details = array( 'shipping_method' => $wc_checkout->shipping_methods[$package_key], 'destination' => $package['destination'], ); // Save shipping address $renewal_order['shipping_address'] = array( // First three lines may need to be changed to make this compatible with shipping extensions that allow multiple shipping addresses '_shipping_first_name' => $wc_checkout->posted['ship_to_different_address'] ? $wc_checkout->posted['shipping_first_name'] : $wc_checkout->posted['billing_first_name'], '_shipping_last_name' => $wc_checkout->posted['ship_to_different_address'] ? $wc_checkout->posted['shipping_last_name'] : $wc_checkout->posted['billing_last_name'], '_shipping_company' => $wc_checkout->posted['ship_to_different_address'] ? $wc_checkout->posted['shipping_company'] : $wc_checkout->posted['billing_company'], '_shipping_address_1' => $shipping_details['destination']['address'], '_shipping_address_2' => $shipping_details['destination']['address_2'], '_shipping_city' => $shipping_details['destination']['city'], '_shipping_state' => $shipping_details['destination']['state'], '_shipping_postcode' => $shipping_details['destination']['postcode'], '_shipping_country' => $shipping_details['destination']['country'], ); break; } } } // Got the shipping method and address for the package that contains current subscription? if (!isset($shipping_details)) { continue; } // Get packages based on renewal order details $packages = apply_filters('woocommerce_cart_shipping_packages', array( 0 => array( 'contents' => $renewal_cart->get_cart(), 'contents_cost' => isset($renewal_cart->cart_contents[$renewal_cart_item_key]['line_total']) ? $renewal_cart->cart_contents[$renewal_cart_item_key]['line_total'] : 0, 'applied_coupons' => $renewal_cart->applied_coupons, 'destination' => $shipping_details['destination'], ), )); // Now we need to calculate shipping costs but this requires overwriting session variables // In order not to affect real cart, we will overwrite them but then set them back to original values $original_session = array( 'chosen_shipping_methods' => $woocommerce->session->get('chosen_shipping_methods'), 'shipping_method_counts' => $woocommerce->session->get('shipping_method_counts'), ); // Set fake renewal values $woocommerce->session->set('chosen_shipping_methods', array($shipping_details['shipping_method'])); $woocommerce->session->set('shipping_method_counts', array(1)); // Override chosen shipping method in case there's a mismatch in shipping_method_counts (more than one available) add_filter('woocommerce_shipping_chosen_method', array($this, 'set_shipping_chosen_method')); $this->temp_shipping_chosen_method = $shipping_details['shipping_method']; // Calculate shipping for fake renewal order now $woocommerce->shipping->calculate_shipping($packages); // Remove filter remove_filter('woocommerce_shipping_chosen_method', array($this, 'set_shipping_chosen_method')); $this->temp_shipping_chosen_method = null; } // Recalculate totals $renewal_cart->calculate_totals(); // Get taxes foreach ($renewal_cart->get_tax_totals() as $rate_key => $rate) { $renewal_order['taxes'][] = array( 'name' => $rate_key, 'rate_id' => $rate->tax_rate_id, 'label' => $rate->label, 'compound' => absint($rate->is_compound ? 1 : 0), 'tax_amount' => wc_format_decimal(isset($renewal_cart->taxes[$rate->tax_rate_id]) ? $renewal_cart->taxes[$rate->tax_rate_id] : 0), 'shipping_tax_amount' => wc_format_decimal(isset($renewal_cart->shipping_taxes[$rate->tax_rate_id]) ? $renewal_cart->shipping_taxes[$rate->tax_rate_id] : 0), ); } // Get renewal_order_shipping $renewal_order['renewal_order_shipping'] = wc_format_decimal($renewal_cart->shipping_total); // Get renewal_order_shipping_tax $renewal_order['renewal_order_shipping_tax'] = wc_format_decimal($renewal_cart->shipping_tax_total); // Get renewal_cart_discount $renewal_order['renewal_cart_discount'] = wc_format_decimal($renewal_cart->get_cart_discount_total()); // Get renewal_order_discount $renewal_order['renewal_order_discount'] = wc_format_decimal($renewal_cart->get_order_discount_total()); // Get renewal_order_tax $renewal_order['renewal_order_tax'] = wc_format_decimal($renewal_cart->tax_total); // Get renewal_order_total $renewal_order['renewal_order_total'] = wc_format_decimal($renewal_cart->total, get_option('woocommerce_price_num_decimals')); // Get renewal_line_subtotal $renewal_order['renewal_line_subtotal'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_subtotal']); // Get renewal_line_subtotal_tax $renewal_order['renewal_line_subtotal_tax'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_subtotal_tax']); // Get renewal_line_total $renewal_order['renewal_line_total'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_total']); // Get renewal_line_tax $renewal_order['renewal_line_tax'] = wc_format_decimal($renewal_cart->cart_contents[$renewal_cart_item_key]['line_tax']); // Get shipping details if ($product->needs_shipping()) { if (isset($woocommerce->shipping->packages[0]['rates'][$shipping_details['shipping_method']])) { $method = $woocommerce->shipping->packages[0]['rates'][$shipping_details['shipping_method']]; $renewal_order['shipping'] = array( 'name' => $method->label, 'method_id' => $method->id, 'cost' => wc_format_decimal( $method->cost ), ); } // Set session variables to original values and recalculate shipping for original order which is being processed now $woocommerce->session->set('chosen_shipping_methods', $original_session['chosen_shipping_methods']); $woocommerce->session->set('shipping_method_counts', $original_session['shipping_method_counts']); $woocommerce->shipping->calculate_shipping($packages); } // Save to object property so it can be accessed from another method $this->renewal_orders['by_cart_item_key'][$cart_item_key] = $renewal_order; } } }