Example #1
0
/**
 * shopp_product_variant_to_item
 *
 * Convert a variant Price object to an Item object
 *
 * @api
 * @since 1.2
 *
 * @param ShoppPrice $Variant a product or variant Price object to create the item from.
 * @param int $quantity (optional default:1) quantity of the variant the Item object will represent
 * @return Item|bool Item object on success, false on failure
 **/
function shopp_product_variant_to_item($Variant, $quantity = 1)
{
    $quantity = (int) $quantity;
    if (!$quantity) {
        $quantity = 1;
    }
    if (is_object($Variant) && is_a($Variant, 'ShoppPrice') && $Variant->product && $Variant->id && in_array($Variant->context, array('product', 'variation'))) {
        $Product = shopp_product($Variant->product);
        $Item = new Item($Product, $Variant->id);
        $Item->quantity($quantity);
        return $Item;
    }
    shopp_debug(__FUNCTION__ . " failed: Variant object missing or invalid.");
    return false;
}
 /**
  * order()
  * Handles new order notifications from Google */
 function order($XML)
 {
     global $Shopp;
     $db = DB::get();
     // Check if this is a Shopp order or not
     $origin = $XML->getElementContent('shopping-cart-agent');
     if (empty($origin) || substr($origin, 0, strpos("/", SHOPP_GATEWAY_USERAGENT)) == SHOPP_GATEWAY_USERAGENT) {
         return true;
     }
     $buyer = $XML->getElement('buyer-billing-address');
     $buyer = $buyer['CHILDREN'];
     $Customer = new Customer();
     $name = $XML->getElement('structured-name');
     $Customer->firstname = $buyer['structured-name']['CHILDREN']['first-name']['CONTENT'];
     $Customer->lastname = $buyer['structured-name']['CHILDREN']['last-name']['CONTENT'];
     if (empty($name)) {
         $name = $buyer['contact-name']['CONTENT'];
         $names = explode(" ", $name);
         $Customer->firstname = $names[0];
         $Customer->lastname = $names[count($names) - 1];
     }
     $Customer->email = $buyer['email']['CONTENT'];
     $Customer->phone = $buyer['phone']['CONTENT'];
     $Customer->save();
     $Billing = new Billing();
     $Billing->customer = $Customer->id;
     $Billing->address = $buyer['address1']['CONTENT'];
     $Billing->xaddress = $buyer['address2']['CONTENT'];
     $Billing->city = $buyer['city']['CONTENT'];
     $Billing->state = $buyer['region']['CONTENT'];
     $Billing->country = $buyer['country-code']['CONTENT'];
     $Billing->postcode = $buyer['postal-code']['CONTENT'];
     $Billing->save();
     $shipto = $XML->getElement('buyer-shipping-address');
     $shipto = $shipto['CHILDREN'];
     $Shipping = new Shipping();
     $Shipping->customer = $Customer->id;
     $Shipping->address = $shipto['address1']['CONTENT'];
     $Shipping->xaddress = $shipto['address2']['CONTENT'];
     $Shipping->city = $shipto['city']['CONTENT'];
     $Shipping->state = $shipto['region']['CONTENT'];
     $Shipping->country = $shipto['country-code']['CONTENT'];
     $Shipping->postcode = $shipto['postal-code']['CONTENT'];
     $Shipping->save();
     $Purchase = new Purchase();
     $Purchase->customer = $Customer->id;
     $Purchase->billing = $Billing->id;
     $Purchase->shipping = $Shipping->id;
     $Purchase->copydata($Customer);
     $Purchase->copydata($Billing);
     $Purchase->copydata($Shipping, 'ship');
     $Purchase->freight = $XML->getElementContent('shipping-cost');
     $Purchase->tax = $XML->getElementContent('total-tax');
     $Purchase->total = $XML->getElementContent('order-total');
     $Purchase->subtotal = $Purchase->total - $Purchase->frieght - $Purchase->tax;
     $Purchase->gateway = "Google Checkout";
     $Purchase->transactionid = $XML->getElementContent('google-order-number');
     $Purchase->transtatus = $XML->getElementContent('financial-order-state');
     $Purchase->ip = $XML->getElementContent('customer-ip');
     $orderdata = $XML->getElement('shopp-order-data');
     $data = array();
     if (is_array($orderdata) && count($orderdata) > 0) {
         foreach ($orderdata as $input) {
             $data[$input['ATTRS']['name']] = $input['CONTENT'];
         }
     }
     $Purchase->data = $data;
     $Purchase->save();
     $items = $XML->getElement('item');
     if (key($items) === "CHILDREN") {
         $items = array($items);
     }
     foreach ($items as $item) {
         $xml = $item['CHILDREN'];
         $itemdata = $xml['merchant-private-item-data']['CHILDREN'];
         $inputdata = $itemdata['shopp-item-data-list']['CHILDREN']['shopp-item-data'];
         $data = array();
         if (is_array($inputdata) && count($inputdata) > 0) {
             foreach ($inputdata as $input) {
                 $data[$input['ATTRS']['name']] = $input['CONTENT'];
             }
         }
         $Product = new Product($itemdata['shopp-product-id']['CONTENT']);
         $Item = new Item($Product, $itemdata['shopp-price-id']['CONTENT'], false, $data);
         $Item->quantity($xml['quantity']['CONTENT']);
         $Purchased = new Purchased();
         $Purchased->copydata($Item);
         $Purchased->purchase = $Purchase->id;
         if (!empty($Purchased->download)) {
             $Purchased->keygen();
         }
         $Purchased->save();
         if ($Item->inventory) {
             $Item->unstock();
         }
     }
 }