public function simple_product_xml($merchantID, $awsAccessKeyID) { global $wpdb; $client_id = ''; $prefix = $wpdb->prefix; $table = $prefix . "pwa_before_cart_save"; $sess_data = new WC_Session_Handler(); $sess_detail = $sess_data->get_session_cookie(); $sess_cust_id = $sess_detail[0]; if (is_user_logged_in()) { $user_ID = get_current_user_id(); } else { $user_ID = 0; } if (UPDATE_ODER_FROM == 'woocart') { $query = 'SELECT id FROM ' . $table . ' where user_id = "' . $user_ID . '" ORDER BY id DESC LIMIT 1'; $result = $wpdb->get_results($query); $client_id = $result[0]->id; } else { $client_id = $user_ID; } $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $root = $doc->createElement('Order'); $attribute = $doc->createAttribute('xmlns'); $attribute->value = 'http://payments.amazon.com/checkout/2009-05-15/'; $root_attr = $root->appendChild($attribute); $root = $doc->appendChild($root); $ClientRequestId = $doc->createElement('ClientRequestId', $client_id); $ClientRequestId = $root->appendChild($ClientRequestId); $Cart = $doc->createElement('Cart'); $Cart = $root->appendChild($Cart); $Items = $doc->createElement('Items'); $Items = $Cart->appendChild($Items); $CartPromotionId = $doc->createElement('CartPromotionId', 'Total_Discount'); $CartPromotionId = $Cart->appendChild($CartPromotionId); $Promotions = $doc->createElement('Promotions'); $Promotions = $root->appendChild($Promotions); $cartObj = WC()->cart; $all_fees = $cartObj->get_fees(); $real_cart = WC()->cart->get_cart(); $_pf = new WC_Product_Factory(); $currency = get_woocommerce_currency(); $cart_total = 0; $total_fees = 0; $cart_total_wocom = WC()->cart->total; $cart_subtotal = $cartObj->subtotal; $amazon_total_disc = 0; $cart_discount_amount = $cartObj->get_cart_discount_total(); $cart_coupon_code = ''; if (!empty($cartObj->coupon_discount_tax_amounts)) { foreach ($cartObj->coupon_discount_tax_amounts as $key => $coupon) { $cart_coupon_code .= $key . ","; $cart_discount_amount += $coupon; } } else { $cart_discount_amount = (double) $cartObj->discount_cart + (double) $cartObj->discount_total; } $neg_fees_Amout = 0; foreach ($all_fees as $fee) { $fees_Amout = $fee->amount + $fee->tax; if ($fees_Amout < 0) { $neg_fees_Amout = abs($fees_Amout); } } $cart_discount_amount += $neg_fees_Amout; $cart_discount_amount = (double) $cart_discount_amount; if ($cart_discount_amount) { $Promotion = $doc->createElement('Promotion'); $Promotion = $Promotions->appendChild($Promotion); $Promotion_pro_id = $doc->createElement('PromotionId', 'Total_Discount'); $Promotion_pro_id = $Promotion->appendChild($Promotion_pro_id); $Promotion_pro_desc = $doc->createElement('Description', 'desc'); $Promotion_pro_desc = $Promotion->appendChild($Promotion_pro_desc); $Promotion_pro_benf = $doc->createElement('Benefit'); $Promotion_pro_benf = $Promotion->appendChild($Promotion_pro_benf); $Promotion_pro_benf_fad = $doc->createElement('FixedAmountDiscount'); $Promotion_pro_benf_fad = $Promotion_pro_benf->appendChild($Promotion_pro_benf_fad); $Promotion_pro_benf_fad_amount = $doc->createElement('Amount', $cart_discount_amount); $Promotion_pro_benf_fad_amount = $Promotion_pro_benf_fad->appendChild($Promotion_pro_benf_fad_amount); $Promotion_pro_benf_fad_currency = $doc->createElement('CurrencyCode', $currency); $Promotion_pro_benf_fad_currency = $Promotion_pro_benf_fad->appendChild($Promotion_pro_benf_fad_currency); } foreach ($real_cart as $key => $item) { $product_id = isset($item['variation_id']) && $item['variation_id'] != '' ? $item['variation_id'] : $item['product_id']; $variation_id = $item['variation_id']; $productObj = new WC_Product($product_id); $product = $_pf->get_product($product_id); $sku = $product->get_sku(); $sku = substr($sku, 0, 40); $title = $product->get_title(); if (!$title) { $title = 'Title'; } $title = substr($title, 0, 80); $description = $product->get_post_data()->post_content; $description = substr($description, 0, 1900); $quantity = $item['quantity']; $weight = $product->get_weight(); $weight_unit = get_option('woocommerce_weight_unit'); if ($weight_unit != 'kg' && $weight > 0) { $weight = $weight / 1000; $weight_unit = 'kg'; } $variations = isset($item['variation']) && $item['variation'] != '' ? $item['variation'] : array(); if ($cartObj->prices_include_tax) { $product_price = ($item['line_subtotal'] + $item['line_subtotal_tax']) / $quantity; } else { $product_price = $item['line_subtotal'] / $quantity; } $amazon_product_price = ($item['line_subtotal'] + $item['line_subtotal_tax']) / $quantity; $cart_total += $amazon_end_line_subtotal; /** create xml of the basic product **/ $Item = $doc->createElement('Item'); $Item = $Items->appendChild($Item); $sku = $this->replace_char($sku); $sku = htmlentities($sku, ENT_QUOTES, 'UTF-8'); $SKU = $doc->createElement('SKU', $sku); $SKU = $Item->appendChild($SKU); $MerchantId = $doc->createElement('MerchantId', $merchantID); $MerchantId = $Item->appendChild($MerchantId); $title = $this->replace_char($title); $title = htmlentities($title, ENT_QUOTES, 'UTF-8'); $Title = $doc->createElement('Title', $title); $Title = $Item->appendChild($Title); $description = $this->replace_char($description); $description = htmlentities($description, ENT_QUOTES, 'UTF-8'); $Description = $doc->createElement('Description', $description); $Description = $Item->appendChild($Description); $Price = $doc->createElement('Price'); $Price = $Item->appendChild($Price); $Amount = $doc->createElement('Amount', $amazon_product_price); $Amount = $Price->appendChild($Amount); $CurrencyCode = $doc->createElement('CurrencyCode', $currency); $CurrencyCode = $Price->appendChild($CurrencyCode); $Quantity = $doc->createElement('Quantity', $quantity); $Quantity = $Item->appendChild($Quantity); if ($weight) { $Weight = $doc->createElement('Weight'); $Weight = $Item->appendChild($Weight); $Amount_wt = $doc->createElement('Amount', $weight); $Amount_wt = $Weight->appendChild($Amount_wt); $Wt_unit = $doc->createElement('Unit', $weight_unit); $Wt_unit = $Weight->appendChild($Wt_unit); } $ItemCustomData = $doc->createElement('ItemCustomData'); $ItemCustomData = $Item->appendChild($ItemCustomData); $Product_id = $doc->createElement('Product_id', $item['product_id']); $Product_id = $ItemCustomData->appendChild($Product_id); $Variation_id = $doc->createElement('Variation_id', $variation_id); $Variation_id = $ItemCustomData->appendChild($Variation_id); foreach ($variations as $key => $variant) { $Item_attribute = $doc->createElement('Item_attribute'); $Item_attribute = $ItemCustomData->appendChild($Item_attribute); $key = substr($key, 0, 40); $key = $this->replace_char($key); $key = htmlentities($key, ENT_QUOTES, 'UTF-8'); $Attribute_name = $doc->createElement('Attribute_name', $key); $Attribute_name = $Item_attribute->appendChild($Attribute_name); $variant = substr($variant, 0, 40); $variant = $this->replace_char($variant); $variant = htmlentities($variant, ENT_QUOTES, 'UTF-8'); $Attribute_val = $doc->createElement('Attribute_val', $variant); $Attribute_val = $Item_attribute->appendChild($Attribute_val); } } foreach ($all_fees as $fee) { $fees_Amout = $fee->amount + $fee->tax; if ($fees_Amout > 0) { $Item = $doc->createElement('Item'); $Item = $Items->appendChild($Item); $fee_id = substr($fee->id, 0, 40); $fee_id = $this->replace_char($fee_id); $fee_id = htmlentities($fee_id, ENT_QUOTES, 'UTF-8'); $SKU = $doc->createElement('SKU', $fee_id); $SKU = $Item->appendChild($SKU); $MerchantId = $doc->createElement('MerchantId', $merchantID); $MerchantId = $Item->appendChild($MerchantId); $fee_name = substr($fee->name, 0, 80); if (!$fee_name) { $fee_name = 'Order Fees'; } $fee_name = $this->replace_char($fee_name); $fee_name = htmlentities($fee_name, ENT_QUOTES, 'UTF-8'); $Title = $doc->createElement('Title', $fee_name); $Title = $Item->appendChild($Title); $Description = $doc->createElement('Description', 'extra fees'); $Description = $Item->appendChild($Description); $Price = $doc->createElement('Price'); $Price = $Item->appendChild($Price); $Amount = $doc->createElement('Amount', $fees_Amout); $Amount = $Price->appendChild($Amount); $CurrencyCode = $doc->createElement('CurrencyCode', $currency); $CurrencyCode = $Price->appendChild($CurrencyCode); $Quantity = $doc->createElement('Quantity', 1); $Quantity = $Item->appendChild($Quantity); } } return $doc->saveXML(); }
public static function getActiveCartData() { /* * 1. Check in woocommerce session i.e cookies. * 2. Check in woocommerce session in database. * */ $details = array('cart_data_alone' => array(), 'wc_session_data_serialized' => '', 'cart_items_n_quantity_hash' => array()); $wcSession = new WC_Session_Handler(); $customerId = $wcSession->get_customer_id(); $userId = is_user_logged_in() ? get_current_user_id() : 0; $wcCustId = !empty($customerId) ? $customerId : $userId; if (!empty($wcCustId)) { $wcSessionData = get_option('_wc_session_' . $wcCustId); self::debugLog(json_encode($wcSessionData)); if (!empty($wcSessionData)) { $details['cart_data_alone'] = unserialize($wcSessionData['cart']); $details['wc_session_data_serialized'] = serialize($wcSessionData); $details['cart_items_n_quantity_hash'] = WpsWcAFR::getItemsNQuantityHashFromWcSession($wcSessionData); } } return $details; }
public function add_cart_data_temp() { if (UPDATE_ODER_FROM == 'woocart') { global $wpdb; $prefix = $wpdb->prefix; $table = $prefix . "pwa_before_cart_save"; $data = maybe_serialize(WC()->cart); $sess_data = new WC_Session_Handler(); $sess_detail = $sess_data->get_session_cookie(); $sess_cust_id = $sess_detail[0]; if (is_user_logged_in()) { $user_ID = get_current_user_id(); $query = 'SELECT id FROM ' . $table . ' where user_id = "' . $sess_cust_id . '"'; $result = $wpdb->get_results($query); $ids = ''; $id = $result[0]->id; if ($id) { foreach ($result as $rid) { $ids .= $rid->id . ','; } $ids = substr($ids, 0, -1); $qry = "UPDATE " . $table . " SET user_id = '" . $user_ID . "' WHERE id in (" . $ids . ")"; $wpdb->query($qry); } } else { $user_ID = $sess_cust_id; } if ($user_ID) { $qry = "INSERT INTO " . $table . " (user_id, cart_data, status) VALUES ('" . $user_ID . "', '" . addslashes($data) . "',0)"; $wpdb->query($qry); } } }