function execute($process, $event) { $ini = eZINI::instance('workflow.ini'); $cost = $ini->variable("SimpleShippingWorkflow", "ShippingCost"); $description = $ini->variable("SimpleShippingWorkflow", "ShippingDescription"); $parameters = $process->attribute('parameter_list'); if (isset($parameters['order_id'])) { $orderID = $parameters['order_id']; $order = eZOrder::fetch($orderID); $orderItems = $order->attribute('order_items'); $addShipping = true; foreach ($orderItems as $orderItem) { if ($orderItem->attribute('type') == 'ezsimpleshipping') { $addShipping = false; break; } } if ($addShipping) { $productCollection = $order->attribute('productcollection'); $orderCurrency = $productCollection->attribute('currency_code'); $cost = eZShopFunctions::convertAdditionalPrice($orderCurrency, $cost); $orderItem = new eZOrderItem(array('order_id' => $orderID, 'description' => $description, 'price' => $cost, 'type' => 'ezsimpleshipping')); $orderItem->store(); } } return eZWorkflowType::STATUS_ACCEPTED; }
function addToBasket($objectID, $optionList, $quantity) { $object = eZContentObject::fetch($objectID); $nodeID = $object->attribute('main_node_id'); $price = 0.0; $isVATIncluded = true; $attributes = $object->contentObjectAttributes(); $priceFound = false; foreach ($attributes as $attribute) { $dataType = $attribute->dataType(); if (eZShopFunctions::isProductDatatype($dataType->isA())) { $priceObj = $attribute->content(); $price += $priceObj->attribute('price'); $priceFound = true; } } if (!$priceFound) { eZDebug::writeError('Attempted to add object without price to basket.'); return array('status' => eZModuleOperationInfo::STATUS_CANCELLED); } $currency = $priceObj->attribute('currency'); // Check for 'option sets' in option list. // If found each 'option set' will be added as a separate product purchase. $hasOptionSet = false; foreach (array_keys($optionList) as $optionKey) { if (substr($optionKey, 0, 4) == 'set_') { $returnStatus = eZShopOperationCollection::addToBasket($objectID, $optionList[$optionKey]); // If adding one 'option set' fails we should stop immediately if ($returnStatus['status'] == eZModuleOperationInfo::STATUS_CANCELLED) { return $returnStatus; } $hasOptionSet = true; } } if ($hasOptionSet) { return $returnStatus; } $unvalidatedAttributes = array(); foreach ($attributes as $attribute) { $dataType = $attribute->dataType(); if ($dataType->isAddToBasketValidationRequired()) { $errors = array(); if ($attribute->validateAddToBasket($optionList[$attribute->attribute('id')], $errors) !== eZInputValidator::STATE_ACCEPTED) { $description = $errors; $contentClassAttribute = $attribute->contentClassAttribute(); $attributeName = $contentClassAttribute->attribute('name'); $unvalidatedAttributes[] = array("name" => $attributeName, "description" => $description); } } } if (count($unvalidatedAttributes) > 0) { return array('status' => eZModuleOperationInfo::STATUS_CANCELLED, 'reason' => 'validation', 'error_data' => $unvalidatedAttributes); } $basket = eZBasket::currentBasket(); /* Check if the item with the same options is not already in the basket: */ $itemID = false; $collection = $basket->attribute('productcollection'); if (!$collection) { eZDebug::writeError('Unable to find product collection.'); return array('status' => eZModuleOperationInfo::STATUS_CANCELLED); } else { $collection->setAttribute('currency_code', $currency); $collection->store(); $count = 0; /* Calculate number of options passed via the HTTP variable: */ foreach (array_keys($optionList) as $key) { if (is_array($optionList[$key])) { $count += count($optionList[$key]); } else { $count++; } } $collectionItems = $collection->itemList(false); foreach ($collectionItems as $item) { /* For all items in the basket which have the same object_id: */ if ($item['contentobject_id'] == $objectID) { $options = eZProductCollectionItemOption::fetchList($item['id'], false); /* If the number of option for this item is not the same as in the HTTP variable: */ if (count($options) != $count) { break; } $theSame = true; foreach ($options as $option) { /* If any option differs, go away: */ if (is_array($optionList[$option['object_attribute_id']]) && !in_array($option['option_item_id'], $optionList[$option['object_attribute_id']]) || !is_array($optionList[$option['object_attribute_id']]) && $option['option_item_id'] != $optionList[$option['object_attribute_id']]) { $theSame = false; break; } } if ($theSame) { $itemID = $item['id']; break; } } } if ($itemID) { /* If found in the basket, just increment number of that items: */ $item = eZProductCollectionItem::fetch($itemID); $item->setAttribute('item_count', $quantity + $item->attribute('item_count')); $item->store(); } else { $item = eZProductCollectionItem::create($basket->attribute("productcollection_id")); $item->setAttribute('name', $object->attribute('name')); $item->setAttribute("contentobject_id", $objectID); $item->setAttribute("item_count", $quantity); $item->setAttribute("price", $price); if ($priceObj->attribute('is_vat_included')) { $item->setAttribute("is_vat_inc", '1'); } else { $item->setAttribute("is_vat_inc", '0'); } $item->setAttribute("vat_value", $priceObj->attribute('vat_percent')); $item->setAttribute("discount", $priceObj->attribute('discount_percent')); $item->store(); $priceWithoutOptions = $price; $optionIDList = array(); foreach (array_keys($optionList) as $key) { $attributeID = $key; $optionString = $optionList[$key]; if (is_array($optionString)) { foreach ($optionString as $optionID) { $optionIDList[] = array('attribute_id' => $attributeID, 'option_string' => $optionID); } } else { $optionIDList[] = array('attribute_id' => $attributeID, 'option_string' => $optionString); } } $db = eZDB::instance(); $db->begin(); foreach ($optionIDList as $optionIDItem) { $attributeID = $optionIDItem['attribute_id']; $optionString = $optionIDItem['option_string']; $attribute = eZContentObjectAttribute::fetch($attributeID, $object->attribute('current_version')); $dataType = $attribute->dataType(); $optionData = $dataType->productOptionInformation($attribute, $optionString, $item); if ($optionData) { $optionData['additional_price'] = eZShopFunctions::convertAdditionalPrice($currency, $optionData['additional_price']); $optionItem = eZProductCollectionItemOption::create($item->attribute('id'), $optionData['id'], $optionData['name'], $optionData['value'], $optionData['additional_price'], $attributeID); $optionItem->store(); $price += $optionData['additional_price']; } } if ($price != $priceWithoutOptions) { $item->setAttribute("price", $price); $item->store(); } $db->commit(); } } return array('status' => eZModuleOperationInfo::STATUS_CONTINUE); }
function calculatePriceWithOptions($currency = false) { $optionList = eZProductCollectionItemOption::fetchList($this->attribute('id')); $contentObject = $this->contentObject(); $contentObjectVersion = $contentObject->attribute('current_version'); $optionsPrice = 0.0; if (count($optionList) > 0) { $db = eZDB::instance(); $db->begin(); foreach ($optionList as $option) { $objectAttribute = eZContentObjectAttribute::fetch($option->attribute('object_attribute_id'), $contentObjectVersion); if ($objectAttribute == null) { $optionsPrice += 0.0; continue; } $dataType = $objectAttribute->dataType(); $optionData = $dataType->productOptionInformation($objectAttribute, $option->attribute('option_item_id'), $this); if ($optionData) { $optionData['additional_price'] = eZShopFunctions::convertAdditionalPrice($currency, $optionData['additional_price']); $option->setAttribute('price', $optionData['additional_price']); $option->store(); $optionsPrice += $optionData['additional_price']; } } $db->commit(); } return $optionsPrice; }