post_type!=='product' && $post->post_type!=='product_variation')) : $post_id = $wpdb->get_var($wpdb->prepare(" SELECT post_id FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE $wpdb->postmeta.meta_key = 'SKU' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'shop_product' AND $wpdb->postmeta.meta_value = '".$item_to_add."' LIMIT 1 ")); $post = get_post( $post_id ); endif; if (!$post || ($post->post_type!=='product' && $post->post_type!=='product_variation')) : die(); endif; if ($post->post_type=="product") : $_product = new jigoshop_product( $post->ID ); else : $_product = new jigoshop_product_variation( $post->ID ); endif; $loop = 0; ?>
'.__('Sorry, your session has expired.', 'jigoshop').' '.__('Return to homepage →', 'jigoshop').'
'; exit; endif; do_action('jigoshop_checkout_update_order_review', $_POST['post_data']); if (isset($_POST['shipping_method'])) : $shipping_method = explode(":", $_POST['shipping_method']); jigoshop_session::instance()->chosen_shipping_method_id = $shipping_method[0]; if (is_numeric($shipping_method[2])) : jigoshop_session::instance()->selected_rate_id = $shipping_method[2]; endif; endif; if (isset($_POST['country'])) jigoshop_customer::set_country( $_POST['country'] ); if (isset($_POST['state'])) jigoshop_customer::set_state( $_POST['state'] ); if (isset($_POST['postcode'])) jigoshop_customer::set_postcode( $_POST['postcode'] ); if (isset($_POST['s_country'])) jigoshop_customer::set_shipping_country( $_POST['s_country'] ); if (isset($_POST['s_state'])) jigoshop_customer::set_shipping_state( $_POST['s_state'] ); if (isset($_POST['s_postcode'])) jigoshop_customer::set_shipping_postcode( $_POST['s_postcode'] ); jigoshop_cart::calculate_totals(); do_action('jigoshop_checkout_order_review'); die(); } add_action('wp_ajax_jigoshop_update_order_review', 'jigoshop_ajax_update_order_review'); add_action('wp_ajax_nopriv_jigoshop_update_order_review', 'jigoshop_ajax_update_order_review'); /** * Clear cart **/ add_action( 'wp_header', 'jigoshop_clear_cart_on_return' ); function jigoshop_clear_cart_on_return() { if (is_page(jigoshop_get_page_id('thanks'))) : 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 jigoshop_order( $order_id ); if ($order->order_key == $order_key) : jigoshop_cart::empty_cart(); endif; endif; endif; } /** * Clear the cart after payment - order will be processing or complete **/ add_action( 'init', 'jigoshop_clear_cart_after_payment' ); function jigoshop_clear_cart_after_payment( $url = false ) { if (isset( jigoshop_session::instance()->order_awaiting_payment ) && jigoshop_session::instance()->order_awaiting_payment > 0) : $order = new jigoshop_order( jigoshop_session::instance()->order_awaiting_payment ); if ($order->id > 0 && ($order->status=='completed' || $order->status=='processing')) : jigoshop_cart::empty_cart(); unset( jigoshop_session::instance()->order_awaiting_payment ); endif; endif; } /** * Process the login form **/ add_action('init', 'jigoshop_process_login'); function jigoshop_process_login() { if (isset($_POST['login']) && $_POST['login']) : jigoshop::verify_nonce('login'); if ( !isset($_POST['username']) || empty($_POST['username']) ) jigoshop::add_error( __('Username is required.', 'jigoshop') ); if ( !isset($_POST['password']) || empty($_POST['password']) ) jigoshop::add_error( __('Password is required.', 'jigoshop') ); if (jigoshop::error_count()==0) : $creds = array(); $creds['user_login'] = $_POST['username']; $creds['user_password'] = $_POST['password']; $creds['remember'] = true; $secure_cookie = is_ssl() ? true : false; $user = wp_signon( $creds, $secure_cookie ); if ( is_wp_error($user) ) : jigoshop::add_error( $user->get_error_message() ); else : if ( isset($_SERVER['HTTP_REFERER'])) { wp_safe_redirect($_SERVER['HTTP_REFERER']); exit; } wp_redirect(apply_filters('jigoshop_get_myaccount_page_id', get_permalink(jigoshop_get_page_id('myaccount')))); exit; endif; endif; endif; } /** * Process ajax checkout form */ add_action('wp_ajax_jigoshop-checkout', 'jigoshop_process_checkout'); add_action('wp_ajax_nopriv_jigoshop-checkout', 'jigoshop_process_checkout'); function jigoshop_process_checkout () { include_once jigoshop::plugin_path() . '/classes/jigoshop_checkout.class.php'; jigoshop_checkout::instance()->process_checkout(); die(0); } /** * Cancel a pending order - hook into init function **/ add_action('init', 'jigoshop_cancel_order'); function jigoshop_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 jigoshop_order( $order_id ); if ($order->id == $order_id && $order->order_key == $order_key && $order->status=='pending' && jigoshop::verify_nonce('cancel_order', '_GET')) : // Cancel the order + restore stock $order->cancel_order( __('Order cancelled by customer.', 'jigoshop') ); // Message jigoshop::add_message( __('Your order was cancelled.', 'jigoshop') ); elseif ($order->status!='pending') : jigoshop::add_error( __('Your order is no longer pending and could not be cancelled. Please contact us if you need assistance.', 'jigoshop') ); else : jigoshop::add_error( __('Invalid order.', 'jigoshop') ); endif; wp_safe_redirect(jigoshop_cart::get_cart_url()); exit; endif; } /** * Download a file - hook into init function **/ add_action('init', 'jigoshop_download_product'); function jigoshop_download_product() { if ( isset($_GET['download_file']) && isset($_GET['order']) && isset($_GET['email']) ) : global $wpdb; $download_file = (int) urldecode($_GET['download_file']); $order = urldecode( $_GET['order'] ); $email = urldecode( $_GET['email'] ); if (!is_email($email)) : wp_die( __('Invalid email address.', 'jigoshop') . ' ' . __('Go to homepage →', 'jigoshop') . '' ); endif; $download_result = $wpdb->get_row( $wpdb->prepare(" SELECT downloads_remaining FROM ".$wpdb->prefix."jigoshop_downloadable_product_permissions WHERE user_email = %s AND order_key = %s AND product_id = %s ;", $email, $order, $download_file ) ); if (!$download_result) : wp_die( __('Invalid download.', 'jigoshop') . ' ' . __('Go to homepage →', 'jigoshop') . '' ); exit; endif; $order_id = $download_result->order_id; $downloads_remaining = $download_result->downloads_remaining; if ($order_id) : $order = new jigoshop_order( $order_id ); if ($order->status!='completed' && $order->status!='processing' && $order->status!='publish') : wp_die( __('Invalid order.', 'jigoshop') . ' ' . __('Go to homepage →', 'jigoshop') . '' ); exit; endif; endif; if ($downloads_remaining == '0') : wp_die( sprintf(__('Sorry, you have reached your download limit for this file. Go to homepage →', 'jigoshop'), home_url()) ); else : if ($downloads_remaining>0) : $wpdb->update( $wpdb->prefix . "jigoshop_downloadable_product_permissions", array( 'downloads_remaining' => $downloads_remaining - 1, ), array( 'user_email' => $email, 'order_key' => $order, 'product_id' => $download_file ), array( '%d' ), array( '%s', '%s', '%d' ) ); endif; $file_path = get_post_meta($download_file, 'file_path', true); if (!$file_path) wp_die( sprintf(__('File not found. Go to homepage →', 'jigoshop'), home_url()) ); // Get URLS with https $site_url = site_url(); $network_url = network_admin_url(); if (is_ssl()) : $site_url = str_replace('https:', 'http:', $site_url); $network_url = str_replace('https:', 'http:', $network_url); endif; if (!is_multisite()) : $file_path = str_replace(trailingslashit($site_url), ABSPATH, $file_path); else : $upload_dir = wp_upload_dir(); // Try to replace network url $file_path = str_replace(trailingslashit($network_url), ABSPATH, $file_path); // Now try to replace upload URL $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_path); endif; // See if its local or remote if (strstr($file_path, 'http:') || strstr($file_path, 'https:') || strstr($file_path, 'ftp:')) : $remote_file = true; else : $remote_file = false; $file_path = realpath($file_path); endif; // Download the file $file_extension = strtolower(substr(strrchr($file_path,"."),1)); switch ($file_extension) : case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpe": case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; endswitch; @ini_set('zlib.output_compression', 'Off'); @set_time_limit(0); @session_start(); @session_cache_limiter('none'); @set_magic_quotes_runtime(0); @ob_end_clean(); if (ob_get_level()) @ob_end_clean(); @session_write_close(); header("Pragma: no-cache"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Robots: none"); header("Content-Type: ".$ctype.""); header("Content-Description: File Transfer"); header("Content-Transfer-Encoding: binary"); if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { // workaround for IE filename bug with multiple periods / multiple dots in filename $iefilename = preg_replace('/\./', '%2e', basename($file_path), substr_count(basename($file_path), '.') - 1); header("Content-Disposition: attachment; filename=\"".$iefilename."\";"); } else { header("Content-Disposition: attachment; filename=\"".basename($file_path)."\";"); } header("Content-Length: ".@filesize($file_path)); if ( $remote_file ) { header('Location: '.$file_path); } else { @readfile("$file_path") or wp_die( sprintf(__('File not found. Go to homepage →', 'jigoshop'), home_url()) ); } exit; endif; endif; } /** * Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER **/ add_action('order_status_completed', 'jigoshop_downloadable_product_permissions'); function jigoshop_downloadable_product_permissions( $order_id ) { global $wpdb; $order = new jigoshop_order( $order_id ); if (sizeof($order->items)>0) foreach ($order->items as $item) : // if ($item['id']>0) : // @todo: Bit of a hack could be improved as id is null/0 if ( (bool) $item['variation_id'] ) { $_product = new jigoshop_product_variation( $item['variation_id'] ); $product_id = $_product->variation_id; } else { $_product = new jigoshop_product( $item['id'] ); $product_id = $_product->ID; } if ( $_product->exists && $_product->is_type('downloadable') ) : $user_email = $order->billing_email; if ($order->user_id>0) : $user_info = get_userdata($order->user_id); if ($user_info->user_email) : $user_email = $user_info->user_email; endif; else : $order->user_id = 0; endif; $limit = trim(get_post_meta($_product->id, 'download_limit', true)); if (!empty($limit)) : $limit = (int) $limit; else : $limit = ''; endif; // Downloadable product - give access to the customer $wpdb->insert( $wpdb->prefix . 'jigoshop_downloadable_product_permissions', array( 'product_id' => $product_id, 'user_id' => $order->user_id, 'user_email' => $user_email, 'order_key' => $order->order_key, 'downloads_remaining' => $limit ), array( '%s', '%s', '%s', '%s', '%s' ) ); endif; // endif; endforeach; } /** * Displays Google Analytics tracking code in the footer * * @return void */ add_action( 'wp_footer', 'jigoshop_ga_tracking' ); function jigoshop_ga_tracking() { // If admin don't track..shouldn't require this if ( is_admin() ) return false; // Don't track the shop owners roaming if ( current_user_can('manage_options') ) return false; $tracking_id = get_option('jigoshop_ga_id'); if ( ! $tracking_id ) return false; $loggedin = (is_user_logged_in()) ? 'yes' : 'no'; if ( is_user_logged_in() ) { $user_id = get_current_user_id(); $current_user = get_user_by('id', $user_id); $username = $current_user->user_login; } else { $user_id = null; $username = __('Guest', 'jigoshop'); } ?> user_login; } else { $user_id = ''; $username = __('Guest', 'jigoshop'); } ?> query['product_cat'])) ? $wp_query->query['product_cat'] : ''; $terms = get_terms( 'product_cat', $r ); if (!$terms) return; $output = ""; echo $output; } /** * Walk the Product Categories. */ function jigoshop_walk_category_dropdown_tree() { $args = func_get_args(); // the user's options are the third parameter if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) $walker = new Jigoshop_Walker_CategoryDropdown; else $walker = $args[2]['walker']; return call_user_func_array(array( &$walker, 'walk' ), $args ); } /** * Create HTML dropdown list of Product Categories. */ class Jigoshop_Walker_CategoryDropdown extends Walker { var $tree_type = 'category'; var $db_fields = array ('parent' => 'parent', 'id' => 'term_id', 'slug' => 'slug' ); function start_el(&$output, $object, $depth, $args, $current_object_id = 0) { $pad = str_repeat(' ', $depth * 3); $cat_name = apply_filters('list_product_cats', $object->name, $object); $output .= "\t\n"; } }