/** * Returns the current product list link. * @param bool url Optional, whether to return a link or url. Defaults to show link. * @param string link_text Optional, text to show in link. */ function mp_products_link_sc($atts) { extract(shortcode_atts(array('url' => false, 'link_text' => ''), $atts)); return mp_products_link(false, $url, $link_text); }
/** * Echos the current store navigation links. * * @param bool $echo Optional, whether to echo. Defaults to true */ function mp_store_navigation($echo = true) { global $mp; //navigation if (!$mp->get_setting('disable_cart')) { $nav = '<ul class="mp_store_navigation"><li class="page_item"><a href="' . mp_products_link(false, true) . '" title="' . __('Products', 'mp') . '">' . __('Products', 'mp') . '</a></li>'; $nav .= '<li class="page_item"><a href="' . mp_cart_link(false, true) . '" title="' . __('Shopping Cart', 'mp') . '">' . __('Shopping Cart', 'mp') . '</a></li>'; $nav .= '<li class="page_item"><a href="' . mp_orderstatus_link(false, true) . '" title="' . __('Order Status', 'mp') . '">' . __('Order Status', 'mp') . '</a></li> </ul>'; } else { $nav = '<ul class="mp_store_navigation"> <li class="page_item"><a href="' . mp_products_link(false, true) . '" title="' . __('Products', 'mp') . '">' . __('Products', 'mp') . '</a></li> </ul>'; } $nav = apply_filters('mp_store_navigation', $nav); if ($echo) { echo $nav; } else { return $nav; } }
/** * Echos the current store navigation links. * * @param bool $echo Optional, whether to echo. Defaults to true */ function mp_store_navigation($echo = true) { $settings = get_option('mp_settings'); //navigation if (!$settings['disable_cart']) { $nav = '<ul class="mp_store_navigation"><li class="page_item"><a href="' . mp_products_link(false, true) . '" title="' . __('Products', 'mp') . '">' . __('Products', 'mp') . '</a></li>'; $nav .= '<li class="page_item"><a href="' . mp_cart_link(false, true) . '" title="' . __('Shopping Cart', 'mp') . '">' . __('Shopping Cart', 'mp') . '</a></li>'; $nav .= '<li class="page_item"><a href="' . mp_orderstatus_link(false, true) . '" title="' . __('Order Status', 'mp') . '">' . __('Order Status', 'mp') . '</a></li> </ul>'; } else { $nav = '<ul class="mp_store_navigation"> <li class="page_item"><a href="' . mp_products_link(false, true) . '" title="' . __('Products', 'mp') . '">' . __('Products', 'mp') . '</a></li> </ul>'; } if ($echo) { echo $nav; } else { return $nav; } }
function update_cart() { global $blog_id, $mp_gateway_active_plugins; $blog_id = is_multisite() ? $blog_id : 1; $current_blog_id = $blog_id; $cart = $this->get_cart_cookie(); if (isset($_POST['empty_cart'])) { //empty cart contents //clear all blog products only if global checkout enabled if ($this->global_cart) { $this->set_global_cart_cookie(array()); } else { $this->set_cart_cookie(array()); } if (defined('DOING_AJAX') && DOING_AJAX) { ?> <div class="mp_cart_empty"> <?php _e('There are no items in your cart.', 'mp'); ?> </div> <div id="mp_cart_actions_widget"> <a class="mp_store_link" href="<?php mp_products_link(true, true); ?> "><?php _e('Browse Products »', 'mp'); ?> </a> </div> <?php exit; } } else { if (isset($_POST['product_id'])) { //add a product to cart //if not valid product_id return $product_id = apply_filters('mp_product_id_add_to_cart', intval($_POST['product_id'])); $product = get_post($product_id); if (!$product || $product->post_type != 'product' || $product->post_status != 'publish') { return false; } //get quantity $quantity = isset($_POST['quantity']) ? intval(abs($_POST['quantity'])) : 1; //get variation $variation = isset($_POST['variation']) ? intval(abs($_POST['variation'])) : 0; //check max stores if ($this->global_cart && count($global_cart = $this->get_cart_cookie(true)) >= $mp_gateway_active_plugins[0]->max_stores && !isset($global_cart[$blog_id])) { if (defined('DOING_AJAX') && DOING_AJAX) { echo 'error||' . sprintf(__("Sorry, currently it's not possible to checkout with items from more than %s stores.", 'mp'), $mp_gateway_active_plugins[0]->max_stores); exit; } else { $this->cart_checkout_error(sprintf(__("Sorry, currently it's not possible to checkout with items from more than %s stores.", 'mp'), $mp_gateway_active_plugins[0]->max_stores)); return false; } } //calculate new quantity $new_quantity = $cart[$product_id][$variation] + $quantity; //check stock if (get_post_meta($product_id, 'mp_track_inventory', true)) { $stock = maybe_unserialize(get_post_meta($product_id, 'mp_inventory', true)); if (!is_array($stock)) { $stock[0] = $stock; } if ($stock[$variation] < $new_quantity) { if (defined('DOING_AJAX') && DOING_AJAX) { echo 'error||' . sprintf(__("Sorry, we don't have enough of this item in stock. (%s remaining)", 'mp'), number_format_i18n($stock[$variation] - $cart[$product_id][$variation])); exit; } else { $this->cart_checkout_error(sprintf(__("Sorry, we don't have enough of this item in stock. (%s remaining)", 'mp'), number_format_i18n($stock[$variation] - $cart[$product_id][$variation]))); return false; } } //send ajax leftover stock if (defined('DOING_AJAX') && DOING_AJAX) { $return = array_sum($stock) - $new_quantity . '||'; } } else { //send ajax always stock if stock checking turned off if (defined('DOING_AJAX') && DOING_AJAX) { $return = 1 . '||'; } } //check limit if tracking on or downloadable if (get_post_meta($product_id, 'mp_track_limit', true) || ($file = get_post_meta($product_id, 'mp_file', true))) { $limit = empty($file) ? maybe_unserialize(get_post_meta($product_id, 'mp_limit', true)) : array($variation => 1); if ($limit[$variation] && $limit[$variation] < $new_quantity) { if (defined('DOING_AJAX') && DOING_AJAX) { echo 'error||' . sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s".', 'mp'), number_format_i18n($limit[$variation]), $product->post_title); exit; } else { $this->cart_checkout_error(sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s".', 'mp'), number_format_i18n($limit[$variation]), $product->post_title)); return false; } } } $cart[$product_id][$variation] = $new_quantity; //save items to cookie $this->set_cart_cookie($cart); //if running via ajax return updated cart and die if (defined('DOING_AJAX') && DOING_AJAX) { $return .= mp_show_cart('widget', false, false); echo $return; exit; } } else { if (isset($_POST['update_cart_submit'])) { //update cart contents $global_cart = $this->get_cart_cookie(true); //process quantity updates if (is_array($_POST['quant'])) { foreach ($_POST['quant'] as $pbid => $quant) { list($bid, $product_id, $variation) = split(':', $pbid); if (is_multisite()) { switch_to_blog($bid); } $quant = intval(abs($quant)); if ($quant) { //check stock if (get_post_meta($product_id, 'mp_track_inventory', true)) { $stock = maybe_unserialize(get_post_meta($product_id, 'mp_inventory', true)); if (!is_array($stock)) { $stock[0] = $stock; } if ($stock[$variation] < $quant) { $left = $stock[$variation] - intval($global_cart[$bid][$product_id][$variation]) < 0 ? 0 : $stock[$variation] - intval($global_cart[$bid][$product_id][$variation]); $this->cart_checkout_error(sprintf(__('Sorry, there is not enough stock for "%s". (%s remaining)', 'mp'), get_the_title($product_id), number_format_i18n($left))); continue; } } //check limit if tracking on or downloadable if (get_post_meta($product_id, 'mp_track_limit', true) || ($file = get_post_meta($product_id, 'mp_file', true))) { $limit = empty($file) ? maybe_unserialize(get_post_meta($product_id, 'mp_limit', true)) : array($variation => 1); if ($limit[$variation] && $limit[$variation] < $quant) { $this->cart_checkout_error(sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s".', 'mp'), number_format_i18n($limit[$variation]), get_the_title($product_id))); continue; } } $global_cart[$bid][$product_id][$variation] = $quant; } else { unset($global_cart[$bid][$product_id][$variation]); } } if (is_multisite()) { switch_to_blog($current_blog_id); } } //remove items if (isset($_POST['remove']) && is_array($_POST['remove'])) { foreach ($_POST['remove'] as $pbid) { list($bid, $product_id, $variation) = split(':', $pbid); unset($global_cart[$bid][$product_id][$variation]); } $this->cart_update_message(__('Item(s) Removed', 'mp')); } //save items to cookie $this->set_global_cart_cookie($global_cart); //add coupon code if (!empty($_POST['coupon_code'])) { if ($this->check_coupon($_POST['coupon_code'])) { //get coupon code if (is_multisite()) { global $blog_id; $_SESSION['mp_cart_coupon_' . $blog_id] = $_POST['coupon_code']; } else { $_SESSION['mp_cart_coupon'] = $_POST['coupon_code']; } $this->cart_update_message(__('Coupon Successfully Applied', 'mp')); } else { $this->cart_checkout_error(__('Invalid Coupon Code', 'mp')); } } } else { if (isset($_GET['remove_coupon'])) { //remove coupon code if (is_multisite()) { global $blog_id; unset($_SESSION['mp_cart_coupon_' . $blog_id]); } else { unset($_SESSION['mp_cart_coupon']); } $this->cart_update_message(__('Coupon Removed', 'mp')); } else { if (isset($_POST['mp_shipping_submit'])) { //save shipping info //check checkout info if (!is_email($_POST['email'])) { $this->cart_checkout_error(__('Please enter a valid Email Address.', 'mp'), 'email'); } //only require these fields if not a download only cart if (!$this->download_only_cart($this->get_cart_contents()) && $this->get_setting('shipping->method') != 'none') { if (empty($_POST['name'])) { $this->cart_checkout_error(__('Please enter your Full Name.', 'mp'), 'name'); } if (empty($_POST['address1'])) { $this->cart_checkout_error(__('Please enter your Street Address.', 'mp'), 'address1'); } if (empty($_POST['city'])) { $this->cart_checkout_error(__('Please enter your City.', 'mp'), 'city'); } if (($_POST['country'] == 'US' || $_POST['country'] == 'CA') && empty($_POST['state'])) { $this->cart_checkout_error(__('Please enter your State/Province/Region.', 'mp'), 'state'); } if ($_POST['country'] == 'US' && !array_key_exists(strtoupper($_POST['state']), $this->usa_states)) { $this->cart_checkout_error(__('Please enter a valid two-letter State abbreviation.', 'mp'), 'state'); } else { if ($_POST['country'] == 'CA' && !array_key_exists(strtoupper($_POST['state']), $this->canadian_provinces)) { $this->cart_checkout_error(__('Please enter a valid two-letter Canadian Province abbreviation.', 'mp'), 'state'); } else { $_POST['state'] = strtoupper($_POST['state']); } } if (empty($_POST['zip'])) { $this->cart_checkout_error(__('Please enter your Zip/Postal Code.', 'mp'), 'zip'); } if (empty($_POST['country']) || strlen($_POST['country']) != 2) { $this->cart_checkout_error(__('Please enter your Country.', 'mp'), 'country'); } if ($_POST['no_shipping_options'] == '1') { $this->cart_checkout_error(__('No valid shipping options found. Please check your address carefully.', 'mp'), 'no_shipping_options'); } } // Process Personalization if (isset($_POST['mp_custom_fields']) && count($_POST['mp_custom_fields'])) { foreach ($_POST['mp_custom_fields'] as $cf_key => $cf_items) { list($bid, $product_id, $variation) = split(':', $cf_key); if (!isset($product_id)) { continue; } if (!isset($variation)) { continue; } $mp_has_custom_field = get_post_meta(intval($product_id), 'mp_has_custom_field', true); if (isset($mp_has_custom_field) && isset($mp_has_custom_field[intval($variation)]) && $mp_has_custom_field[intval($variation)]) { $mp_custom_field_required = get_post_meta(intval($product_id), 'mp_custom_field_required', true); if (isset($mp_custom_field_required) && isset($mp_custom_field_required[intval($variation)]) && $mp_custom_field_required[intval($variation)]) { foreach ($cf_items as $idx => $cf_item) { if (empty($cf_item)) { $this->cart_checkout_error(__('Required product extra information.', 'mp'), 'custom_fields_' . $product_id . '_' . $variation); break; } else { $cf_items[$idx] = trim(strip_tags(stripslashes($cf_item))); } } $_POST['mp_custom_fields'][$cf_key] = $cf_items; } } } } //save to session global $current_user; $meta = get_user_meta($current_user->ID, 'mp_shipping_info', true); $_SESSION['mp_shipping_info']['email'] = $_POST['email'] ? trim(stripslashes($_POST['email'])) : (isset($meta['email']) ? $meta['email'] : $current_user->user_email); $_SESSION['mp_shipping_info']['name'] = $_POST['name'] ? trim(stripslashes($_POST['name'])) : (isset($meta['name']) ? $meta['name'] : $current_user->user_firstname . ' ' . $current_user->user_lastname); $_SESSION['mp_shipping_info']['address1'] = $_POST['address1'] ? trim(stripslashes($_POST['address1'])) : $meta['address1']; $_SESSION['mp_shipping_info']['address2'] = $_POST['address2'] ? trim(stripslashes($_POST['address2'])) : $meta['address2']; $_SESSION['mp_shipping_info']['city'] = $_POST['city'] ? trim(stripslashes($_POST['city'])) : $meta['city']; $_SESSION['mp_shipping_info']['state'] = $_POST['state'] ? trim(stripslashes($_POST['state'])) : $meta['state']; $_SESSION['mp_shipping_info']['zip'] = $_POST['zip'] ? trim(stripslashes($_POST['zip'])) : $meta['zip']; $_SESSION['mp_shipping_info']['country'] = $_POST['country'] ? trim($_POST['country']) : $meta['country']; $_SESSION['mp_shipping_info']['phone'] = $_POST['phone'] ? preg_replace('/[^0-9-\\(\\) ]/', '', trim($_POST['phone'])) : $meta['phone']; if (isset($_POST['special_instructions'])) { $_SESSION['mp_shipping_info']['special_instructions'] = trim(stripslashes($_POST['special_instructions'])); } //Handle and store Product Custom field data if (isset($_POST['mp_custom_fields'])) { $_SESSION['mp_shipping_info']['mp_custom_fields'] = $_POST['mp_custom_fields']; } //for checkout plugins do_action('mp_shipping_process'); //save to user meta if ($current_user->ID) { update_user_meta($current_user->ID, 'mp_shipping_info', $_SESSION['mp_shipping_info']); } //if no errors send to next checkout step if ($this->checkout_error == false) { //check for $0 checkout to skip gateways //loop through cart items $global_cart = $this->get_cart_contents(true); if (!$this->global_cart) { //get subset if needed $selected_cart[$blog_id] = $global_cart[$blog_id]; } else { $selected_cart = $global_cart; } $totals = array(); $shipping_prices = array(); $tax_prices = array(); foreach ($selected_cart as $bid => $cart) { if (is_multisite()) { switch_to_blog($bid); } foreach ($cart as $product_id => $variations) { foreach ($variations as $data) { $totals[] = $data['price'] * $data['quantity']; } } if (($shipping_price = $this->shipping_price()) !== false) { $shipping_prices[] = $shipping_price; } if (($tax_price = $this->tax_price()) !== false) { $tax_prices[] = $tax_price; } } //go back to original blog if (is_multisite()) { switch_to_blog($current_blog_id); } $total = array_sum($totals); //coupon line if ($coupon = $this->coupon_value($this->get_coupon_code(), $total)) { $total = $coupon['new_total']; } //shipping if ($shipping_price = array_sum($shipping_prices)) { $total = $total + $shipping_price; } //tax line if ($tax_price = array_sum($tax_prices)) { $total = $total + $tax_price; } if ($total > 0) { $network_settings = get_site_option('mp_network_settings'); //can we skip the payment form page? if ($this->global_cart) { $skip = apply_filters('mp_payment_form_skip_' . $network_settings['global_gateway'], false); } else { $skip = apply_filters('mp_payment_form_skip_' . $this->get_setting('gateways->allowed->0'), false); } if (!$this->global_cart && count($this->get_setting('gateways->allowed', array())) > 1 || !$skip) { wp_safe_redirect(mp_checkout_step_url('checkout')); exit; } else { if ($this->global_cart) { $_SESSION['mp_payment_method'] = $network_settings['global_gateway']; } else { $_SESSION['mp_payment_method'] = $this->get_setting('gateways->allowed->0'); } do_action('mp_payment_submit_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info']); //if no errors send to next checkout step if ($this->checkout_error == false) { wp_safe_redirect(mp_checkout_step_url('confirm-checkout')); exit; } else { wp_safe_redirect(mp_checkout_step_url('checkout')); exit; } } } else { //empty price, create order already //loop through and create orders foreach ($selected_cart as $bid => $cart) { $totals = array(); if (is_multisite()) { switch_to_blog($bid); } foreach ($cart as $product_id => $variations) { foreach ($variations as $data) { $totals[] = $data['price'] * $data['quantity']; } } $total = array_sum($totals); //coupon line if ($coupon = $this->coupon_value($this->get_coupon_code(), $total)) { $total = $coupon['new_total']; } //shipping if (($shipping_price = $this->shipping_price()) !== false) { $total = $total + $shipping_price; } //tax line if (($tax_price = $this->tax_price()) !== false) { $total = $total + $tax_price; } //setup our payment details $timestamp = time(); $payment_info['gateway_public_name'] = __('Manual Checkout', 'mp'); $payment_info['gateway_private_name'] = __('Manual Checkout', 'mp'); $payment_info['method'][] = __('N/A - Free order', 'mp'); $payment_info['transaction_id'][] = __('N/A', 'mp'); $payment_info['status'][$timestamp] = __('Completed', 'mp'); $payment_info['total'] = $total; $payment_info['currency'] = $this->get_setting('currency'); $this->create_order(false, $cart, $_SESSION['mp_shipping_info'], $payment_info, true); } //go back to original blog if (is_multisite()) { switch_to_blog($current_blog_id); } $_SESSION['mp_payment_method'] = 'manual'; //so we don't get an error message on confirmation page //redirect to final page wp_safe_redirect(mp_checkout_step_url('confirmation')); exit; } } } else { if (isset($_POST['mp_choose_gateway'])) { //check and save payment info $_SESSION['mp_payment_method'] = $_POST['mp_choose_gateway']; //processing script is only for selected gateway plugin do_action('mp_payment_submit_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info']); //if no errors send to next checkout step if ($this->checkout_error == false) { wp_safe_redirect(mp_checkout_step_url('confirm-checkout')); exit; } } else { if (isset($_POST['mp_payment_confirm'])) { //create order and process payment do_action('mp_payment_confirm_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info']); //if no errors send to next checkout step if ($this->checkout_error == false) { wp_safe_redirect(mp_checkout_step_url('confirmation')); exit; } } } } } } } } }
/** * Echos the current shopping cart contents. Use in the cart template. * * @param string $context Optional. Possible values: widget, checkout * @param string $checkoutstep Optional. Possible values: checkout-edit, shipping, checkout, confirm-checkout, confirmation * @param bool $echo Optional. default true */ function flexmarket_show_cart($context = '', $checkoutstep = null, $echo = true, $btnclass = '') { global $mp, $blog_id; $content = ''; if ($checkoutstep == null) { $checkoutstep = get_query_var('checkoutstep'); } if (mp_items_in_cart() || $checkoutstep == 'confirmation') { // type = widget if ($context == 'widget') { $content .= _flexmarket_cart_table('widget', false, $btnclass); $content .= '<div class="mp_cart_actions_widget">'; $content .= '<a class="mp_empty_cart" href="' . mp_cart_link(false, true) . '?empty-cart=1" title="' . __('Empty your shopping cart', 'flexmarket') . '">' . __('Empty Cart', 'flexmarket') . '</a>'; $content .= '<a class="mp_checkout_link" href="' . mp_cart_link(false, true) . '" title="' . __('Go To Checkout Page', 'flexmarket') . '">' . __('Checkout »', 'flexmarket') . '</a>'; $content .= '<div class="clear"></div>'; $content .= '</div>'; } else { if ($context == 'checkout') { //generic error message context for plugins to hook into $content .= apply_filters('mp_checkout_error_checkout', ''); if ($mp->get_setting('show_purchase_breadcrumbs') == 1) { $content .= flexmarket_cart_breadcrumbs($checkoutstep); } //handle checkout steps switch ($checkoutstep) { case 'shipping': $content .= do_shortcode($mp->get_setting('msg->shipping')); $content .= _flexmarket_cart_shipping(true, false, $btnclass); break; case 'checkout': $content .= do_shortcode($mp->get_setting('msg->checkout')); $content .= _flexmarket_cart_payment('form', false, $btnclass); break; case 'confirm-checkout': $content .= do_shortcode($mp->get_setting('msg->confirm_checkout')); $content .= _flexmarket_cart_table('checkout', false, $btnclass); $content .= _flexmarket_cart_shipping(false, false, $btnclass); $content .= _flexmarket_cart_payment('confirm', false, $btnclass); break; case 'confirmation': $content .= do_shortcode($mp->get_setting('msg->success')); $content .= _flexmarket_cart_payment('confirmation', false, $btnclass); break; default: $content .= do_shortcode($mp->get_setting('msg->cart')); $content .= _flexmarket_cart_table('checkout-edit', false, $btnclass); $content .= '<div class="clear padding20"></div>'; $content .= _flexmarket_cart_login(false, $btnclass); break; } } else { $content .= _flexmarket_cart_table('checkout', false, $btnclass); $content .= '<div class="mp_cart_actions">'; $content .= '<a class="mp_empty_cart btn' . $btnclass . '" href="' . mp_cart_link(false, true) . '?empty-cart=1" title="' . __('Empty your shopping cart', 'flexmarket') . '">' . __('Empty Cart', 'flexmarket') . '</a>'; $content .= '<a class="mp_checkout_link btn' . $btnclass . '" href="' . mp_cart_link(false, true) . '" title="' . __('Go To Checkout Page', 'flexmarket') . '">' . __('Checkout »', 'flexmarket') . '</a>'; $content .= '</div>'; } } } else { if ($context != 'widget') { $content .= do_shortcode($mp->get_setting('msg->cart')); } $content .= '<div class="mp_cart_empty"><p>' . __('There are no items in your cart.', 'flexmarket') . '</p></div>'; $content .= '<div id="mp_cart_actions_widget"><a class="mp_store_link btn' . $btnclass . '" href="' . mp_products_link(false, true) . '">' . __('Browse Products »', 'flexmarket') . '</a></div>'; } if ($echo) { echo $content; } else { return $content; } }