/** * Update product stats when a purchase log containing it changes status * * @since 3.8.13 * * @param int $log_id Purchase Log ID * @param int $new_status New status * @param int $old_status Old status * @param WPSC_Purchase_Log $log Purchase Log */ function _wpsc_action_update_product_stats($log_id, $new_status, $old_status, $log) { $cart_contents = $log->get_cart_contents(); $new_status_completed = $log->is_transaction_completed(); $old_status_completed = WPSC_Purchase_Log::is_order_status_completed($old_status); if ($new_status_completed && !$old_status_completed) { // if the order went through without any trouble, then it's a positive thing! $yay_or_boo = 1; } elseif (!$new_status_completed && $old_status_completed) { // if the order is declined or invalid, sad face :( $yay_or_boo = -1; } else { // Not one of the above options then we will be indifferent $yay_or_boo = 0; } // this dramatic mood swing affects the stats of each products in the cart foreach ($cart_contents as $cart_item) { $product = new WPSC_Product($cart_item->prodid); if ($product->exists()) { $diff_sales = $yay_or_boo * (int) $cart_item->quantity; $diff_earnings = $yay_or_boo * (int) $cart_item->price * (int) $cart_item->quantity; $product->sales += $diff_sales; $product->earnings += $diff_earnings; // if this product has parent, make the same changes to the parent if ($product->post->post_parent) { $parent = WPSC_Product::get_instance($product->post->post_parent); $parent->sales += $diff_sales; $parent->earnings += $diff_earnings; } } } }
/** * Sets attendee data on order posts * * @since 4.1 * * @param ShoppPurchase $purchase ShoppPurchase object */ public function save_attendee_meta_to_order($data) { $purchase_log = new WPSC_Purchase_Log($data['purchase_log_id']); $order_items = $purchase_log->get_cart_contents(); // Bail if the order is empty if (empty($order_items)) { return; } $product_ids = array(); // gather product ids foreach ((array) $order_items as $item) { if (empty($item->prodid)) { continue; } $product_ids[] = $item->prodid; } $meta_object = Tribe__Tickets_Plus__Main::instance()->meta(); // build the custom meta data that will be stored in the order meta if (!($order_meta = $meta_object->build_order_meta($product_ids))) { return; } // store the custom meta on the order $result = wpsc_add_purchase_meta($purchase_log->get('id'), Tribe__Tickets_Plus__Meta::META_KEY, $order_meta, true); // clear out product custom meta data cookies foreach ($product_ids as $product_id) { $meta_object->clear_meta_cookie_data($product_id); } }
function eCommerceThankYou($order) { global $tcm; $orderId = intval($order['purchase_id']); $tcm->Log->debug('Ecommerce: ECOMMERCE THANKYOU'); $tcm->Log->debug('Ecommerce: NEW ECOMMERCE ORDERID=%s', $orderId); $order = new WPSC_Purchase_Log($orderId); $items = $order->get_cart_contents(); $productsIds = array(); foreach ($items as $v) { if (isset($v->prodid)) { $k = intval($v->prodid); if ($k) { $v = $v->name; $productsIds[] = $k; $tcm->Log->debug('Ecommerce: ITEM %s=%s IN CART', $k, $v); } } } $args = array('pluginId' => TCM_PLUGINS_WP_ECOMMERCE, 'productsIds' => $productsIds, 'categoriesIds' => array(), 'tagsIds' => array()); $tcm->Options->pushConversionSnippets($args); return ''; }
/** * Pushes sales data back to Baikonur * * Only pushes once. Accounts for annoying potential edge case of status-switching admins * * @param WPSC_Purchase_Log object $purchase_log Purchase Log object * @return void */ public static function push_sales_data($purchase_log_id, $current_status, $old_status, $purchase_log) { $purchase_log = new WPSC_Purchase_Log($purchase_log_id); $id = absint($purchase_log->get('id')); //Also checking is_order_received, as that's what Manual Payments do. if ($purchase_log->is_transaction_completed() || $purchase_log->is_order_received()) { $pushed_to_sass = wpsc_get_meta($id, '_pushed_to_wpeconomy', 'purchase_log'); if (empty($pushed_to_saas)) { $data = $purchase_log->get_data(); $cart_contents = $purchase_log->get_cart_contents(); //We want to push sales data - but naturally, IDs will differ, even names could potentially. //So we add the slug to the object we POST foreach ($cart_contents as $key => $cart_item) { $slug = get_post_field('post_name', $cart_item->prodid); $cart_contents[$key]->slug = $slug; } $args = array('body' => array('data' => json_encode($data), 'cart_contents' => json_encode($cart_contents))); $request = wp_remote_post('http://www.wpeconomy.org/?sales_data=true', $args); $response = wp_remote_retrieve_response_code($request); //For some reason, if the site is down, we want the ability to ensure we can grab the sale later. $success = 200 === $response; wpsc_update_meta($id, '_pushed_to_wpeconomy', $success, 'purchase_log'); } } }
public function add_pushes($session_id) { $purchase = new WPSC_Purchase_Log($session_id, 'sessionid'); $purchase_id = $purchase->get('id'); $data = new WPSC_Checkout_Form_Data($purchase_id); $output = ''; $city = $data->get('billingcity'); $state = $data->get('billingstate'); $country = $data->get('billingcountry'); $state = !empty($state) ? wpsc_get_state_by_id($state, 'name') : ''; $cart_items = $purchase->get_cart_contents(); $total_shipping = wpsc_get_total_shipping($purchase_id); $total_tax = $total_price = 0; foreach ($cart_items as $item) { /* For backwards compatibility, convert objects to arrays */ $item = (array) $item; $total_tax += $item['tax_charged']; $total_price += absint($item['quantity']) * $item['price']; } if ($this->is_theme_tracking || $this->advanced_code) { $output .= "<script type='text/javascript'>\n\r"; } add_filter('wpsc_toggle_display_currency_code', array($this, 'remove_currency_and_html')); if ($this->use_universal_analytics()) { // Yoast GA Plugin switched to it's own object name __gaTracker - assign it to our ga object if it exists $output .= "var ga = typeof ga === 'undefined' && typeof __gaTracker !== 'undefined' ? __gaTracker : ga;"; $output .= "ga('require', 'ecommerce');\n\r"; $output .= "ga('ecommerce:addTransaction', {\n\t\t\t\t'id': '" . $purchase_id . "', // Transaction ID. Required.\n\t\t\t\t'affiliation': '" . wp_specialchars_decode($this->get_site_name()) . "', // Affiliation or store name.\n\t\t\t\t'revenue': '" . number_format($total_price, 2, '.', '') . "', // Grand Total.\n\t\t\t\t'shipping': '" . wpsc_currency_display($total_shipping) . "', // Shipping.\n\t\t\t\t'tax': '" . wpsc_currency_display($total_tax) . "' // Tax.\n\t\t\t});\n\r"; } else { $output .= "\n\t\t\t\t_gaq.push(['_addTrans',\n\t\t\t\t'" . $purchase_id . "', // order ID - required\n\t\t\t\t'" . wp_specialchars_decode($this->get_site_name()) . "', // affiliation or store name\n\t\t\t\t'" . number_format($total_price, 2, '.', '') . "', // total - required\n\t\t\t\t'" . wpsc_currency_display($total_tax) . "', // tax\n\t\t\t\t'" . wpsc_currency_display($total_shipping) . "', // shipping\n\t\t\t\t'" . wp_specialchars_decode($city) . "', // city\n\t\t\t\t'" . wp_specialchars_decode($state) . "', // state or province\n\t\t\t\t'" . wp_specialchars_decode($country) . "' // country\n\t\t\t]);\n\r"; } remove_filter('wpsc_toggle_display_currency_code', array($this, 'remove_currency_and_html')); foreach ($cart_items as $item) { /* For backwards compatibility, convert objects to arrays */ $item = (array) $item; $category = wp_get_object_terms($item['prodid'], 'wpsc_product_category', array('orderby' => 'count', 'order' => 'DESC', 'fields' => 'all_with_object_id')); $item['sku'] = get_post_meta($item['prodid'], '_wpsc_sku', true); if (empty($item['sku'])) { $item['sku'] = $item['prodid']; } if ($category) { $item['category'] = $category[0]->name; } else { $item['category'] = ''; } $item = apply_filters('wpsc_google_analytics_pushed_product', array_map('wp_specialchars_decode', $item), $item, $this); if ($this->use_universal_analytics()) { $output .= "ga('ecommerce:addItem', {" . "'id': '" . $purchase_id . "'," . "'name': '" . $item['name'] . "'," . "'sku': '" . $item['sku'] . "'," . "'category': '" . $item['category'] . "'," . "'price': '" . $item['price'] . "'," . "'quantity': '" . $item['quantity'] . "'" . "});\n\r"; } else { $output .= "_gaq.push(['_addItem'," . "'" . $purchase_id . "'," . "'" . $item['sku'] . "'," . "'" . $item['name'] . "'," . "'" . $item['category'] . "'," . "'" . $item['price'] . "'," . "'" . $item['quantity'] . "']);\n\r"; // Item Quantity } } if ($this->use_universal_analytics()) { $output .= "ga('ecommerce:send');\n\r"; } else { $output .= "_gaq.push(['_trackTrans']);\n\r"; } if ($this->is_theme_tracking || $this->advanced_code) { $output .= "</script>\n\r"; } return $output; }
/** * Adds product properties to analytics.track() when the order is completed successfully. * * @since 1.0.0 * @access public * * @uses func_get_args() Because our abstract class doesn't know how many parameters are passed to each hook * for each different platform, we use func_get_args(). * * @return array Filtered array of name and properties for analytics.track(). */ public function completed_order() { $args = func_get_args(); $track = $args[0]; if (did_action('wpsc_transaction_results_shutdown') && isset($_GET['sessionid'])) { $log = new WPSC_Purchase_Log($_GET['sessionid'], 'sessionid'); /* We like checking is_order_received(), as that's what the manual payment gateway uses. */ if ($log->is_transaction_completed() || $log->is_order_received()) { $gateway_data = $log->get_gateway_data(); $items = $log->get_cart_contents(); $products = array(); foreach ($items as $item) { $product = array('id' => $item->prodid, 'sku' => wpsc_product_sku($item->prodid), 'name' => $item->name, 'price' => $item->price, 'quantity' => $item->quantity, 'category' => implode(', ', wp_list_pluck(wpsc_get_product_terms($item->prodid, 'wpsc_product_category'), 'name'))); $products[] = $product; } $track = array('event' => __('Completed Order', 'segment'), 'properties' => array('id' => $log->get('id'), 'total' => $log->get('totalprice'), 'revenue' => $gateway_data['subtotal'], 'shipping' => $gateway_data['shipping'], 'tax' => $gateway_data['tax'], 'products' => $products)); } } return $track; }
public function type_of_goods($log_id) { $digital = 0; $log = new WPSC_Purchase_Log($log_id); $cart = $log->get_cart_contents(); foreach ($cart as $cartitem) { $product_meta = get_post_meta($cartitem->prodid, '_wpsc_product_metadata'); if (isset($product_meta[0]['no_shipping']) && $product_meta[0]['no_shipping'] == 1) { $digital++; } } return $digital == count($cart) ? 'DIGITAL' : 'PHYSICAL'; }
function do_export_file() { global $wpdb; $trnsid = 1; $gateway_accounts = get_option('pbci_gateway_accounts', array()); $export_accounts = get_option('pbci_export_accounts', array('sales_revenue' => 'Product Revenue', 'shipping' => 'Shipping', 'sales_tax_account' => 'Sales Tax Payable', 'sales_tax_payee' => 'Sales Tax')); $cust = array('NAME' => '', 'FIRSTNAME' => '', 'LASTNAME' => '', 'EMAIL' => '', 'PHONE1' => '', 'BADDR1' => '', 'BADDR2' => '', 'BADDR3' => '', 'BADDR4' => '', 'SADDR1' => '', 'SADDR2' => '', 'SADDR3' => '', 'SADDR4' => ''); $trans = array('TRNSID' => '', 'TRNSTYPE' => '', 'DATE' => '', 'ACCNT' => '', 'AMOUNT' => '', 'NAME' => '', 'MEMO' => '', 'PAYMETH' => ''); $spl = array('SPLID' => '', 'TRNSTYPE' => '', 'DATE' => '', 'ACCNT' => '', 'AMOUNT' => '', 'NAME' => '', 'MEMO' => '', 'INVITEM' => '', 'PRICE' => '', 'EXTRA' => ''); $content = ""; $cust_content = ""; $headers = ""; $headers .= "!CUST\tNAME\t\n"; $headers .= "!ACCNT\tNAME\tACCNTTYPE\tEXTRA\tACCNUM\n"; $content .= "ACCNT\t" . $export_accounts['sales_revenue'] . "\tINC\t\n"; $content .= "ACCNT\t" . $export_accounts['shipping'] . "\tINC\t\n"; $content .= "ACCNT\t" . $export_accounts['sales_tax_account'] . "\tOCLIAB\tSALESTAX\t2201\n"; foreach ($gateway_accounts as $gateway => $account_name) { if (!empty($account_name)) { $content .= "ACCNT\t" . $account_name . "\tBANK\t\n"; } } // !TRNS line $headers .= '!TRNS'; foreach ($trans as $key => $value) { $headers .= "\t" . $key; } $headers .= "\n"; // !SPL line $headers .= '!SPL'; foreach ($spl as $key => $value) { $headers .= "\t" . $key; } $headers .= "\n"; // !CUST line $headers .= '!CUST'; foreach ($cust as $key => $value) { $headers .= "\t" . $key; } $headers .= "\n"; // !ENDTRNS line //$content .= '!ENDTRNS' . "\n"; $export_dates = array_keys($_POST['period']); foreach ($export_dates as $export_date) { $a = explode('-', $export_date); $year = $a[0]; $month = $a[1]; $sql = "SELECT ID FROM " . WPSC_TABLE_PURCHASE_LOGS . ' WHERE MONTH( FROM_UNIXTIME( date ) ) = ' . $month . ' AND YEAR( FROM_UNIXTIME( DATE ) ) = ' . $year . ' ORDER by date DESC'; $result = $wpdb->get_col($sql, 0); $purchase_log_ids = array_map('intval', $result); $max_rows = 1; foreach ($purchase_log_ids as $purchase_log_id) { $purchase_log = new WPSC_Purchase_Log($purchase_log_id); $gateway_id = $purchase_log->get('gateway'); $data = $purchase_log->get_data(); if (empty($gateway_accounts[$gateway_id])) { continue; } // reset the transaction array back to empty foreach ($trans as $key => $value) { $trans[$key] = ''; } // reset the customer array back to empty foreach ($cust as $key => $value) { $cust[$key] = ''; } if ($purchase_log->get('processed') != WPSC_Purchase_Log::ACCEPTED_PAYMENT && $purchase_log->get('processed') != WPSC_Purchase_Log::CLOSED_ORDER) { continue; } $checkout_form_data = new WPSC_Checkout_Form_Data($purchase_log_id); $checkout = $checkout_form_data->get_data(); if (!isset($checkout['billingstate'])) { $checkout['billingstate'] = ''; } if (!isset($checkout['shippingstate'])) { $checkout['shippingstate'] = ''; } $timestamp = $purchase_log->get('date'); $thedate = date('m/d/Y', $timestamp); foreach ($trans as $key => $value) { switch ($key) { case 'TRNSID': $trans[$key] = $trnsid++; break; case 'TIMESTAMP': $trans[$key] = $purchase_log->get('date'); break; case 'TRNSTYPE': $trans[$key] = 'CASH SALE'; break; case 'DATE': $trans[$key] = $thedate; break; case 'ACCNT': $trans[$key] = $gateway_accounts[$gateway_id]; break; case 'NAME': $trans[$key] = $checkout['billingfirstname'] . ' ' . $checkout['billinglastname']; break; case 'AMOUNT': $trans[$key] = $purchase_log->get('totalprice'); break; case 'CLEAR': $trans[$key] = 'N'; break; case 'SHIPDATE': $trans[$key] = $thedate; break; case 'PAYMETH': $trans[$key] = $purchase_log->get('gateway_name'); break; case 'DOCNUM': $trans[$key] = $purchase_log_id; break; case 'MEMO': $trans[$key] = 'sparkle-gear.com purchase #' . $purchase_log_id; break; case 'ADDR1': $trans[$key] = $checkout['billingfirstname'] . ' ' . $checkout['billinglastname']; break; case 'ADDR2': $trans[$key] = $checkout['billingaddress']; break; case 'ADDR3': $trans[$key] = $checkout['billingcity'] . ', ' . $checkout['billingstate'] . ' ' . $checkout['billingpostcode']; break; case 'ADDR4': $trans[$key] = $checkout['billingcountry']; break; case 'SHIPVIA': $trans[$key] = $purchase_log->get('shipping_method_name'); break; case 'INVTITLE': $trans[$key] = 'Sparkle Gear Web Store'; break; case 'SADDR1': $trans[$key] = $checkout['shippingfirstname'] . ' ' . $checkout['shippinglastname']; break; case 'SADDR2': $trans[$key] = $checkout['shippingaddress']; break; case 'SADDR3': $trans[$key] = $checkout['shippingcity'] . ', ' . $checkout['shippingstate'] . ' ' . $checkout['shippingpostcode']; break; case 'SADDR4': $trans[$key] = $checkout['billingcountry']; break; } } foreach ($cust as $key => $value) { switch ($key) { case 'NAME': $cust[$key] = $checkout['billingfirstname'] . ' ' . $checkout['billinglastname']; break; case 'FIRSTNAME': $cust[$key] = $checkout['billingfirstname']; break; case 'LASTNAME': $cust[$key] = $checkout['billinglastname']; break; case 'EMAIL': $cust[$key] = $checkout['billingemail']; break; case 'PHONE1': $cust[$key] = $checkout['billingphone']; break; case 'BADDR1': $cust[$key] = $checkout['billingfirstname'] . ' ' . $checkout['billinglastname']; break; case 'BADDR2': $cust[$key] = $checkout['billingaddress']; break; case 'BADDR3': $cust[$key] = $checkout['billingcity'] . ', ' . $checkout['billingstate'] . ' ' . $checkout['billingpostcode']; break; case 'BADDR4': $cust[$key] = $checkout['billingcountry']; break; case 'SADDR1': $cust[$key] = $checkout['shippingfirstname'] . ' ' . $checkout['shippinglastname']; break; case 'SADDR2': $cust[$key] = $checkout['shippingaddress']; break; case 'SADDR3': $cust[$key] = $checkout['shippingcity'] . ', ' . $checkout['shippingstate'] . ' ' . $checkout['shippingpostcode']; break; case 'SADDR4': $cust[$key] = $checkout['billingcountry']; break; } } foreach ($trans as $key => $value) { $trans[$key] = trim(preg_replace('/\\s+/', ' ', $value)); } foreach ($cust as $key => $value) { $cust[$key] = trim(preg_replace('/\\s+/', ' ', $value)); } $splid = 1; // TRNS line $content .= 'TRNS'; foreach ($trans as $key => $value) { $content .= "\t" . $value; } $content .= "\n"; $cart_contents = $purchase_log->get_cart_contents(); foreach ($cart_contents as $cart_item) { $product_id = $cart_item->prodid; if ($parent_product = get_post_field('post_parent', $product_id)) { $product_id = $parent_product; } $terms = wp_get_post_terms($product_id, 'wpsc_product_category'); if (!empty($terms)) { foreach ($terms as $term) { $invitem = $term->name; if ($term->parent != 0) { break; } } } else { $invitem = ''; } /* $item_name = ''; $article = new Bling_Article( $cart_item->prodid ); if ( $article->check() ) { $item_name = $article->name(); } */ $spl_product = array('SPLID' => $trnsid++, 'TRNSTYPE' => 'PAYMENT', 'DATE' => $trans['DATE'], 'ACCNT' => $export_accounts['sales_revenue'], 'AMOUNT' => -($cart_item->price * $cart_item->quantity), 'QNTY' => -$cart_item->quantity, 'PRICE' => $cart_item->price, 'NAME' => '', 'DOCNUM' => $purchase_log_id, 'MEMO' => $cart_item->name); // SPL line $content .= 'SPL'; foreach ($spl as $key => $value) { $content .= "\t"; if (!empty($spl_product[$key])) { $content .= $spl_product[$key]; } } $content .= "\n"; } $spl_shipping = array('SPLID' => $trnsid++, 'TRNSTYPE' => 'PAYMENT', 'DATE' => $trans['DATE'], 'ACCNT' => $export_accounts['shipping'], 'AMOUNT' => -$purchase_log->get('total_shipping'), 'PRICE' => $purchase_log->get('total_shipping'), 'NAME' => '', 'DOCNUM' => $purchase_log_id, 'MEMO' => 'customer paid shipping', 'EXTRA' => '', 'QNTY' => ''); $splid = 2; $spl_discount = array('SPLID' => $trnsid++, 'TRNSTYPE' => 'PAYMENT', 'DATE' => $trans['DATE'], 'ACCNT' => $export_accounts['sales_revenue'], 'AMOUNT' => $purchase_log->get('discount_value'), 'PRICE' => -$purchase_log->get('discount_value'), 'NAME' => '', 'DOCNUM' => $purchase_log_id, 'MEMO' => 'discount', 'EXTRA' => '', 'QNTY' => ''); $spl_tax = array('SPLID' => $trnsid++, 'TRNSTYPE' => 'PAYMENT', 'DATE' => $trans['DATE'], 'ACCNT' => $export_accounts['sales_tax_account'], 'AMOUNT' => -$purchase_log->get('wpec_taxes_total'), 'PRICE' => "6.25%", 'NAME' => $export_accounts['sales_tax_payee'], 'DOCNUM' => $purchase_log_id, 'MEMO' => 'sales tax', 'EXTRA' => 'AUTOSTAX', 'QNTY' => '', 'INVITEM' => 'MA Sales/Use Tax'); $spl_end = array('SPLID' => $trnsid++, 'EXTRA' => 'ENDGRP'); // SPL line $content .= 'SPL'; foreach ($spl as $key => $value) { $content .= "\t"; if (!empty($spl_shipping[$key])) { $content .= $spl_shipping[$key]; } } $content .= "\n"; // SPL line $content .= 'SPL'; foreach ($spl as $key => $value) { $content .= "\t"; if (!empty($spl_tax[$key])) { $content .= $spl_tax[$key]; } } $content .= "\n"; // SPL line $content .= 'SPL'; foreach ($spl as $key => $value) { $content .= "\t"; if (!empty($spl_discount[$key])) { $content .= $spl_discount[$key]; } } $content .= "\n"; $content .= 'SPL'; foreach ($spl as $key => $value) { $content .= "\t"; if (!empty($spl_end[$key])) { $content .= $spl_end[$key]; } } $content .= "\n"; $splid = 3; $content .= 'ENDTRNS'; $content .= "\n"; //if ( --$max_rows == 0 ) // break; $cust_content .= 'CUST'; foreach ($cust as $key => $value) { $cust_content .= "\t" . $value; } $cust_content .= "\n"; } } $file_name = 'download.iif'; header('Content-Type: text/csv'); header('Content-Disposition: inline; filename="' . $file_name . '"'); echo $headers; echo $cust_content; echo $content; exit; }