function fflcommerce_cart($atts) { unset(fflcommerce_session::instance()->selected_rate_id); // Process Discount Codes if (isset($_POST['apply_coupon']) && $_POST['apply_coupon'] && fflcommerce::verify_nonce('cart')) { $coupon_code = sanitize_title($_POST['coupon_code']); fflcommerce_cart::add_discount($coupon_code); } elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && fflcommerce::verify_nonce('cart')) { // Update Shipping unset(fflcommerce_session::instance()->chosen_shipping_method_id); $country = $_POST['calc_shipping_country']; $state = $_POST['calc_shipping_state']; $postcode = $_POST['calc_shipping_postcode']; if ($postcode && !fflcommerce_validation::is_postcode($postcode, $country)) { fflcommerce::add_error(__('Please enter a valid postcode/ZIP.', 'fflcommerce')); $postcode = ''; } elseif ($postcode) { $postcode = fflcommerce_validation::format_postcode($postcode, $country); } if ($country) { // Update customer location fflcommerce_customer::set_location($country, $state, $postcode); fflcommerce_customer::set_shipping_location($country, $state, $postcode); fflcommerce::add_message(__('Shipping costs updated.', 'fflcommerce')); } else { fflcommerce_customer::set_shipping_location('', '', ''); fflcommerce::add_message(__('Shipping costs updated.', 'fflcommerce')); } } elseif (isset($_POST['shipping_rates'])) { $rates_params = explode(":", $_POST['shipping_rates']); $available_methods = fflcommerce_shipping::get_available_shipping_methods(); $shipping_method = $available_methods[$rates_params[0]]; if ($rates_params[1] != null) { fflcommerce_session::instance()->selected_rate_id = $rates_params[1]; } $shipping_method->choose(); // chooses the method selected by user. } // Re-Calc prices. This needs to happen every time the cart page is loaded and after checking post results. fflcommerce_cart::calculate_totals(); $result = fflcommerce_cart::check_cart_item_stock(); if (is_wp_error($result)) { fflcommerce::add_error($result->get_error_message()); } fflcommerce_render('shortcode/cart', array('cart' => fflcommerce_cart::get_cart(), 'coupons' => fflcommerce_cart::get_coupons())); }
function fflcommerce_edit_address() { $account_url = get_permalink(fflcommerce_get_page_id(FFLCOMMERCE_MY_ACCOUNT)); $user_id = get_current_user_id(); $load_address = fflcommerce_get_address_to_edit(); $address = fflcommerce_get_address_fields($load_address, $user_id); if (isset($_POST['save_address']) && fflcommerce::verify_nonce(FFLCOMMERCE_EDIT_ADDRESS)) { if ($user_id > 0) { foreach ($address as &$field) { if (isset($_POST[$field['name']])) { $field['value'] = fflcommerce_clean($_POST[$field['name']]); update_user_meta($user_id, $field['name'], $field['value']); } } do_action('fflcommerce_user_edit_address', $user_id, $address); } } fflcommerce_render('shortcode/my_account/edit_address', array('url' => add_query_arg('address', $load_address, apply_filters('fflcommerce_get_edit_address_page_id', get_permalink(fflcommerce_get_page_id(FFLCOMMERCE_EDIT_ADDRESS)))), 'account_url' => $account_url, 'load_address' => $load_address, 'address' => $address)); }
function fflcommerce_pay_action() { if (!is_fflcommerce_single_page(FFLCOMMERCE_PAY)) { return; } if (isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id'])) { // Pay for existing order $order_key = urldecode($_GET['order']); $order_id = (int) $_GET['order_id']; $order = new fflcommerce_order($order_id); if ($order->id == $order_id && $order->order_key == $order_key && $order->status == 'pending') { // Set customer location to order location if ($order->billing_country) { fflcommerce_customer::set_country($order->billing_country); } if ($order->billing_state) { fflcommerce_customer::set_state($order->billing_state); } if ($order->billing_postcode) { fflcommerce_customer::set_postcode($order->billing_postcode); } // Pay form was posted - process payment if (isset($_POST['pay']) && fflcommerce::verify_nonce('pay')) { // Update payment method if ($order->order_total > 0) { $payment_method = fflcommerce_clean($_POST['payment_method']); $data = (array) maybe_unserialize(get_post_meta($order_id, 'order_data', true)); $data['payment_method'] = $payment_method; update_post_meta($order_id, 'order_data', $data); $available_gateways = fflcommerce_payment_gateways::get_available_payment_gateways(); $result = $available_gateways[$payment_method]->process_payment($order_id); // Redirect to success/confirmation/payment page if ($result['result'] == 'success') { wp_safe_redirect($result['redirect']); exit; } } else { // No payment was required for order $order->payment_complete(); // filter redirect page $checkout_redirect = apply_filters('fflcommerce_get_checkout_redirect_page_id', fflcommerce_get_page_id('thanks')); wp_safe_redirect(get_permalink($checkout_redirect)); exit; } } } elseif ($order->status != 'pending') { fflcommerce::add_error(__('Your order has already been paid for. Please contact us if you need assistance.', 'fflcommerce')); } else { fflcommerce::add_error(__('Invalid order.', 'fflcommerce')); } } else { // Pay for order after checkout step if (isset($_GET['order'])) { $order_id = $_GET['order']; } else { $order_id = 0; } if (isset($_GET['key'])) { $order_key = $_GET['key']; } else { $order_key = ''; } if ($order_id > 0) { $order = new fflcommerce_order($order_id); if ($order->order_key != $order_key || $order->status != 'pending') { wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id('myaccount')))); exit; } } else { wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id('myaccount')))); exit; } } }
/** * Process the checkout after the confirm order button is pressed */ public function process_checkout() { if (!defined('FFLCOMMERCE_CHECKOUT')) { define('FFLCOMMERCE_CHECKOUT', true); } // Initialize cart fflcommerce_cart::get_cart(); fflcommerce_cart::calculate_totals(); if (isset($_POST) && $_POST && !isset($_POST['login'])) { fflcommerce::verify_nonce('process_checkout'); // this will fill in our $posted array with validated data self::validate_checkout(); $gateway = fflcommerce_payment_gateways::get_gateway($this->posted['payment_method']); if (self::process_gateway($gateway)) { $gateway->validate_fields(); } do_action('fflcommerce_after_checkout_validation', $this->posted, $_POST, sizeof(fflcommerce::$errors)); if (fflcommerce::has_errors()) { return false; } if (!isset($_POST['update_totals'])) { $user_id = get_current_user_id(); // Create customer account and log them in if ($this->show_signup && !$user_id && $this->posted['create_account']) { $user_id = $this->create_user_account(); if ($user_id === 0) { return false; } } $billing = array('first_name' => $this->posted['billing_first_name'], 'last_name' => $this->posted['billing_last_name'], 'company' => $this->posted['billing_company'], 'address_1' => $this->posted['billing_address_1'], 'address_2' => $this->posted['billing_address_2'], 'city' => $this->posted['billing_city'], 'state' => $this->posted['billing_state'], 'postcode' => $this->posted['billing_postcode'], 'country' => $this->posted['billing_country'], 'phone' => $this->posted['billing_phone'], 'email' => $this->posted['billing_email']); fflcommerce_customer::set_country($billing['country']); fflcommerce_customer::set_state($billing['state']); fflcommerce_customer::set_postcode($billing['postcode']); if (isset($this->posted['billing_euvatno']) && $this->valid_euvatno) { $billing['euvatno'] = $this->posted['billing_euvatno']; $billing['euvatno'] = str_replace(' ', '', $billing['euvatno']); // If country code is not provided - add one. if (strpos($billing['euvatno'], $billing['country']) === false) { $billing['euvatno'] = $billing['country'] . $billing['euvatno']; } } // Get shipping/billing if (!empty($this->posted['shiptobilling'])) { $shipping = $billing; unset($shipping['phone'], $shipping['email']); } elseif (fflcommerce_shipping::is_enabled()) { $shipping = array('first_name' => $this->posted['shipping_first_name'], 'last_name' => $this->posted['shipping_last_name'], 'company' => $this->posted['shipping_company'], 'address_1' => $this->posted['shipping_address_1'], 'address_2' => $this->posted['shipping_address_2'], 'city' => $this->posted['shipping_city'], 'state' => $this->posted['shipping_state'], 'postcode' => $this->posted['shipping_postcode'], 'country' => $this->posted['shipping_country']); } fflcommerce_customer::set_shipping_country($shipping['country']); fflcommerce_customer::set_shipping_state($shipping['state']); fflcommerce_customer::set_shipping_postcode($shipping['postcode']); // Update totals based on processed customer address fflcommerce_cart::calculate_totals(); // Save billing/shipping to user meta fields if ($user_id > 0) { foreach ($billing as $field => $value) { update_user_meta($user_id, 'billing_' . $field, $value); } if (isset($shipping)) { foreach ($shipping as $field => $value) { update_user_meta($user_id, 'shipping_' . $field, $value); } } } if (!isset($_POST['submit_action']) || $_POST['submit_action'] != 'place_order') { $result = fflcommerce::redirect(fflcommerce_get_page_id(FFLCOMMERCE_CHECKOUT)); return array('result' => 'redirect', 'redirect' => $result); } // Order meta data $data = array(); $applied_coupons = array_map(function ($coupon) { return JS_Coupons::get_coupon($coupon); }, fflcommerce_cart::get_coupons()); do_action('fflcommerce_checkout_update_order_total', $this->posted); foreach ($billing as $field => $value) { $data['billing_' . $field] = $value; } if (isset($shipping)) { foreach ($shipping as $field => $value) { $data['shipping_' . $field] = $value; } } $data['order_discount_coupons'] = $applied_coupons; $data['shipping_method'] = $this->posted['shipping_method']; $data['shipping_service'] = $this->posted['shipping_service']; $data['payment_method'] = $this->posted['payment_method']; $data['payment_method_title'] = $gateway->title; $data['order_subtotal'] = fflcommerce_cart::get_subtotal(); $data['order_discount_subtotal'] = fflcommerce_cart::get_discount_subtotal(); $data['order_shipping'] = fflcommerce_cart::get_shipping_total(); $data['order_discount'] = fflcommerce_cart::get_total_discount(false); $data['order_tax'] = fflcommerce_cart::get_taxes_as_string(); $data['order_tax_no_shipping_tax'] = fflcommerce_cart::get_total_cart_tax_without_shipping_tax(); $data['order_tax_divisor'] = fflcommerce_cart::get_tax_divisor(); $data['order_shipping_tax'] = fflcommerce_cart::get_shipping_tax(); $data['order_total'] = fflcommerce_cart::get_total(false); $data['order_total_prices_per_tax_class_ex_tax'] = fflcommerce_cart::get_price_per_tax_class_ex_tax(); if ($this->valid_euvatno) { $data['order_tax'] = ''; $temp = fflcommerce_cart::get_total_cart_tax_without_shipping_tax(); $data['order_total'] -= $data['order_shipping_tax'] + $temp; $data['order_shipping_tax'] = 0; } // Cart items $order_items = array(); foreach (fflcommerce_cart::get_cart() as $values) { /** @var fflcommerce_product $product */ $product = $values['data']; // Check stock levels if (!$product->has_enough_stock($values['quantity'])) { fflcommerce::add_error(sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.', 'fflcommerce'), $product->get_title())); if (self::get_options()->get('fflcommerce_show_stock') == 'yes') { fflcommerce::add_error(sprintf(__('We have only %d available at this time.', 'fflcommerce'), $product->get_stock())); } break; } // Calc item tax to store $rates = $product->get_tax_destination_rate(); $rates = current($rates); if (isset($rates['rate'])) { $rate = $rates['rate']; } else { $rate = 0.0; } if ($this->valid_euvatno) { $rate = 0.0; } $price_inc_tax = $product->get_price_with_tax(); if (!empty($values['variation_id'])) { $product_id = $values['variation_id']; } else { $product_id = $values['product_id']; } $custom_products = (array) fflcommerce_session::instance()->customized_products; $custom = isset($custom_products[$product_id]) ? $custom_products[$product_id] : ''; if (!empty($custom)) { unset($custom_products[$product_id]); fflcommerce_session::instance()->customized_products = $custom_products; } $order_items[] = apply_filters('new_order_item', array('id' => $values['product_id'], 'variation_id' => $values['variation_id'], 'variation' => $values['variation'], 'customization' => $custom, 'name' => $product->get_title(), 'qty' => (int) $values['quantity'], 'cost' => $product->get_price_excluding_tax(), 'cost_inc_tax' => $price_inc_tax, 'taxrate' => $rate), $values); } if (fflcommerce::has_errors()) { return false; } // Insert or update the post data $create_new_order = true; $order_data = array('post_type' => 'shop_order', 'post_title' => 'Order – ' . date('F j, Y @ h:i A'), 'post_status' => 'publish', 'post_excerpt' => $this->posted['order_comments'], 'post_author' => 1); $order_id = 0; if (isset(fflcommerce_session::instance()->order_awaiting_payment) && fflcommerce_session::instance()->order_awaiting_payment > 0) { $order_id = absint(fflcommerce_session::instance()->order_awaiting_payment); $terms = wp_get_object_terms($order_id, 'shop_order_status', array('fields' => 'slugs')); $order_status = isset($terms[0]) ? $terms[0] : 'pending'; // Resume the unpaid order if its pending if ($order_status == 'pending' || $order_status == 'failed') { $create_new_order = false; $order_data['ID'] = $order_id; wp_update_post($order_data); } } if ($create_new_order) { $order_id = wp_insert_post($order_data); } if (is_wp_error($order_id) || $order_id === 0) { fflcommerce::add_error(__('Error: Unable to create order. Please try again.', 'fflcommerce')); return false; } // Update post meta update_post_meta($order_id, 'order_data', $data); update_post_meta($order_id, 'order_key', uniqid('order_')); update_post_meta($order_id, 'customer_user', (int) $user_id); update_post_meta($order_id, 'order_items', $order_items); wp_set_object_terms($order_id, 'pending', 'shop_order_status'); $order = new fflcommerce_order($order_id); /* Coupon usage limit */ foreach ($data['order_discount_coupons'] as $coupon) { $coupon_id = JS_Coupons::get_coupon_post_id($coupon['code']); if ($coupon_id !== false) { $usage_count = get_post_meta($coupon_id, 'usage', true); $usage_count = empty($usage_count) ? 1 : $usage_count + 1; update_post_meta($coupon_id, 'usage', $usage_count); } } if ($create_new_order) { do_action('fflcommerce_new_order', $order_id); } else { do_action('fflcommerce_resume_order', $order_id); } do_action('fflcommerce_checkout_update_order_meta', $order_id, $this->posted); // can't just simply check needs_payment() here, as paypal may have force payment set to true if (self::process_gateway($gateway)) { // Store Order ID in session so it can be re-used after payment failure fflcommerce_session::instance()->order_awaiting_payment = $order_id; // Process Payment $result = $gateway->process_payment($order_id); // Redirect to success/confirmation/payment page if ($result['result'] == 'success') { return $result; } return false; } else { // No payment was required for order $order->payment_complete(); // Empty the Cart fflcommerce_cart::empty_cart(); // Redirect to success/confirmation/payment page $checkout_redirect = apply_filters('fflcommerce_get_checkout_redirect_page_id', fflcommerce_get_page_id('thanks')); return array('result' => 'redirect', 'redirect' => $checkout_redirect); } } } return true; }
function fflcommerce_order_tracking($atts) { extract(shortcode_atts(array(), $atts)); global $post; $fflcommerce_options = FFLCommerce_Base::get_options(); if ($_POST) { $order = new fflcommerce_order(); $order->id = !empty($_POST['orderid']) ? $_POST['orderid'] : 0; if (isset($_POST['order_email']) && $_POST['order_email']) { $order_email = trim($_POST['order_email']); } else { $order_email = ''; } if (!fflcommerce::verify_nonce('order_tracking')) { echo '<p>' . __('You have taken too long. Please refresh the page and retry.', 'fflcommerce') . '</p>'; } elseif ($order->id && $order_email && $order->get_order(apply_filters('fflcommerce_shortcode_order_tracking_order_id', $order->id))) { if ($order->billing_email == $order_email) { echo '<p>' . sprintf(__('Order %s which was made %s ago and has the status "%s"', 'fflcommerce'), $order->get_order_number(), human_time_diff(strtotime($order->order_date), current_time('timestamp')), __($order->status, 'fflcommerce')); if ($order->status == 'completed') { $completed = (array) get_post_meta($order->id, '_js_completed_date', true); if (!empty($completed)) { $completed = $completed[0]; } else { $completed = ''; } // shouldn't happen, reset to be sure echo sprintf(__(' was completed %s ago', 'fflcommerce'), human_time_diff(strtotime($completed), current_time('timestamp'))); } echo '.</p>'; do_action('fflcommerce_tracking_details_info', $order); ?> <?php do_action('fflcommerce_before_track_order_details', $order->id); ?> <h2><?php _e('Order Details', 'fflcommerce'); ?> </h2> <table class="shop_table"> <thead> <tr> <th><?php _e('ID/SKU', 'fflcommerce'); ?> </th> <th><?php _e('Title', 'fflcommerce'); ?> </th> <th><?php _e('Price', 'fflcommerce'); ?> </th> <th><?php _e('Quantity', 'fflcommerce'); ?> </th> </tr> </thead> <tfoot> <tr> <?php if ($fflcommerce_options->get('fflcommerce_calc_taxes') == 'yes' && $order->has_compound_tax() || $fflcommerce_options->get('fflcommerce_tax_after_coupon') == 'yes' && $order->order_discount > 0) { ?> <td colspan="3"><?php _e('Retail Price', 'fflcommerce'); ?> </td> <?php } else { ?> <td colspan="3"><?php _e('Subtotal', 'fflcommerce'); ?> </td> <?php } ?> <td><?php echo $order->get_subtotal_to_display(); ?> </td> </tr> <?php if ($order->order_shipping > 0) { ?> <tr> <td colspan="3"><?php _e('Shipping', 'fflcommerce'); ?> </td> <td><?php echo $order->get_shipping_to_display(); ?> </td> </tr> <?php } do_action('fflcommerce_processing_fee_after_shipping'); if ($fflcommerce_options->get('fflcommerce_tax_after_coupon') == 'yes' && $order->order_discount > 0) { ?> <tr class="discount"> <td colspan="3"><?php _e('Discount', 'fflcommerce'); ?> </td> <td>-<?php echo fflcommerce_price($order->order_discount); ?> </td> </tr> <?php } if ($fflcommerce_options->get('fflcommerce_calc_taxes') == 'yes' && $order->has_compound_tax() || $fflcommerce_options->get('fflcommerce_tax_after_coupon') == 'yes' && $order->order_discount > 0) { ?> <tr> <td colspan="3"><?php _e('Subtotal', 'fflcommerce'); ?> </td> <td><?php echo fflcommerce_price($order->order_discount_subtotal); ?> </td> </tr> <?php } if ($fflcommerce_options->get('fflcommerce_calc_taxes') == 'yes') { foreach ($order->get_tax_classes() as $tax_class) { if ($order->show_tax_entry($tax_class)) { ?> <tr> <td colspan="3"><?php echo $order->get_tax_class_for_display($tax_class) . ' (' . (double) $order->get_tax_rate($tax_class) . '%):'; ?> </td> <td><?php echo $order->get_tax_amount($tax_class); ?> </td> </tr> <?php } } } ?> <?php if ($fflcommerce_options->get('fflcommerce_tax_after_coupon') == 'no' && $order->order_discount > 0) { ?> <tr class="discount"> <td colspan="3"><?php _e('Discount', 'fflcommerce'); ?> </td> <td>-<?php echo fflcommerce_price($order->order_discount); ?> </td> </tr><?php } ?> <tr> <td colspan="3"><strong><?php _e('Grand Total', 'fflcommerce'); ?> </strong></td> <td><strong><?php echo fflcommerce_price($order->order_total); ?> </strong></td> </tr> </tfoot> <tbody> <?php foreach ($order->items as $order_item) { if (isset($order_item['variation_id']) && $order_item['variation_id'] > 0) { $_product = new fflcommerce_product_variation($order_item['variation_id']); } else { $_product = new fflcommerce_product($order_item['id']); } echo '<tr>'; echo '<td>' . $_product->sku . '</td>'; echo '<td class="product-name">' . $_product->get_title(); if ($_product instanceof fflcommerce_product_variation) { echo fflcommerce_get_formatted_variation($_product, $order_item['variation']); } do_action('fflcommerce_display_item_meta_data', $order_item); echo '</td>'; echo '<td>' . fflcommerce_price($order_item['cost']) . '</td>'; echo '<td>' . $order_item['qty'] . '</td>'; echo '</tr>'; } ?> </tbody> </table> <?php do_action('fflcommerce_after_track_order_details', $order->id); ?> <div style="width: 49%; float:left;"> <h2><?php _e('Billing Address', 'fflcommerce'); ?> </h2> <p><?php $address = $order->billing_first_name . ' ' . $order->billing_last_name . '<br/>'; if ($order->billing_company) { $address .= $order->billing_company . '<br/>'; } $address .= $order->formatted_billing_address; echo $address; ?> </p> </div> <div style="width: 49%; float:right;"> <h2><?php _e('Shipping Address', 'fflcommerce'); ?> </h2> <p><?php $address = $order->shipping_first_name . ' ' . $order->shipping_last_name . '<br/>'; if ($order->shipping_company) { $address .= $order->shipping_company . '<br/>'; } $address .= $order->formatted_shipping_address; echo $address; ?> </p> </div> <div class="clear"></div> <?php } else { echo '<p>' . __('Sorry, we could not find that order id in our database. <a href="' . get_permalink($post->ID) . '">Want to retry?</a>', 'fflcommerce') . '</p>'; } } else { echo '<p>' . sprintf(__('Sorry, we could not find that order id in our database. <a href="%s">Want to retry?</a></p>', 'fflcommerce'), get_permalink($post->ID)); } } else { ?> <form action="<?php echo esc_url(get_permalink($post->ID)); ?> " method="post" class="track_order"> <p><?php _e('To track your order please enter your Order ID and email address in the boxes below and press return. This was given to you on your receipt and in the confirmation email you should have received.', 'fflcommerce'); ?> </p> <p class="form-row form-row-first"><label for="orderid"><?php _e('Order ID', 'fflcommerce'); ?> </label> <input class="input-text" type="text" name="orderid" id="orderid" placeholder="<?php _e('Found in your order confirmation email.', 'fflcommerce'); ?> " /></p> <p class="form-row form-row-last"><label for="order_email"><?php _e('Billing Email', 'fflcommerce'); ?> </label> <input class="input-text" type="text" name="order_email" id="order_email" placeholder="<?php _e('Email you used during checkout.', 'fflcommerce'); ?> " /></p> <div class="clear"></div> <p class="form-row"><input type="submit" class="button" name="track" value="<?php _e('Track"', 'fflcommerce'); ?> " /></p> <?php fflcommerce::nonce_field('order_tracking'); ?> </form> <?php } }
function fflcommerce_cancel_order() { if (isset($_GET['cancel_order']) && isset($_GET['order']) && isset($_GET['order_id'])) { $order_key = urldecode($_GET['order']); $order_id = (int) $_GET['order_id']; $order = new fflcommerce_order($order_id); if ($order->id == $order_id && $order->order_key == $order_key && $order->status == 'pending' && fflcommerce::verify_nonce('cancel_order')) { // Cancel the order + restore stock $order->cancel_order(__('Order cancelled by customer.', 'fflcommerce')); // Message fflcommerce::add_message(__('Your order was cancelled.', 'fflcommerce')); } elseif ($order->status != 'pending') { fflcommerce::add_error(__('Your order is no longer pending and could not be cancelled. Please contact us if you need assistance.', 'fflcommerce')); } else { fflcommerce::add_error(__('Invalid order.', 'fflcommerce')); } wp_safe_redirect(fflcommerce_cart::get_cart_url()); exit; } }
function fflcommerce_check_comment_rating($comment_data) { // If posting a comment (not trackback etc) and not logged in if (isset($_POST['rating']) && !fflcommerce::verify_nonce('comment_rating')) { wp_die(__('You have taken too long. Please go back and refresh the page.', 'fflcommerce')); } else { if (isset($_POST['rating']) && empty($_POST['rating']) && $comment_data['comment_type'] == '') { wp_die(__('Please rate the product.', "fflcommerce")); exit; } } return $comment_data; }