Diff
checker
텍스트
텍스트
이미지
문서
Excel
폴더
Legal
Enterprise
데스크톱
요금제
로그인
데스크톱 앱 다운로드
텍스트 비교
두 텍스트 파일의 차이점을 찾아보세요
도구
기록
실시간 편집
공백 변경 숨기기
변경 없는 행 숨기기
줄바꿈 비활성화
레이아웃
나란히 보기
합쳐 보기
비교 단위
스마트
단어
글자
텍스트 스타일
모양 변경
구문 강조
언어 선택
제외
텍스트 변환
첫 변경으로
수정
Diffchecker Desktop
가장 안전하게 Diffchecker를 사용하는 방법. 데스크톱 앱을 사용하면 비교 데이터가 외부로 전송되지 않습니다!
데스크톱 앱 받기
Untitled diff
생성일
11년 전
비교 결과 만료 없음
초기화
내보내기
공유
설명
30 삭제
행
총
삭제
글자
총
삭제
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
281 행
복사
9 추가
행
총
추가
글자
총
추가
이 기능을 계속 사용하려면 업그레이드해 주세요
Diff
checker
Pro
요금제 보기
260 행
복사
<?php
<?php
복사
복사됨
복사
복사됨
class
Pricebinc
_App_Model_OrderCreate extends Mage_Core_Model_Abstract
class
NameApp
_App_Model_OrderCreate extends Mage_Core_Model_Abstract
{
{
const CUSTOMER_RANDOM = null;
const CUSTOMER_RANDOM = null;
protected $_shippingMethod = 'freeshipping_freeshipping';
protected $_shippingMethod = 'freeshipping_freeshipping';
protected $_paymentMethod = 'cashondelivery';
protected $_paymentMethod = 'cashondelivery';
복사
복사됨
복사
복사됨
protected $_customer = self::CUSTOMER_RANDOM;
protected $_subTotal = 0;
protected $_subTotal = 0;
protected $_order;
protected $_order;
복사
복사됨
복사
복사됨
protected $_storeId
;
protected $_storeId
= '0';
public function _construct()
{
parent::_construct();
$this->_init('app/ordercreate');
}
public function setShippingMethod($methodName)
public function setShippingMethod($methodName)
{
{
$this->_shippingMethod = $methodName;
$this->_shippingMethod = $methodName;
}
}
복사
복사됨
복사
복사됨
public function setPaymentMethod($methodName)
public function setPaymentMethod($methodName)
{
{
$this->_paymentMethod = $methodName;
$this->_paymentMethod = $methodName;
}
}
복사
복사됨
복사
복사됨
public function setCustomer($customer)
{
if ($customer instanceof Mage_Customer_Model_Customer) {
$this->_customer = $customer;
}
if (is_numeric($customer)) {
$this->_customer = Mage::getModel('customer/customer')->load($customer);
} else if ($customer === self::CUSTOMER_RANDOM) {
$customers = Mage::getResourceModel('customer/customer_collection');
$customers
->getSelect()
->limit(1)
->order('RAND()');
$id = $customers->getFirstItem()->getId();
$this->_customer = Mage::getModel('customer/customer')->load($id);
}
}
public function createOrder($products)
public function createOrder($products)
{
{
복사
복사됨
복사
복사됨
if (!($this->_customer instanceof Mage_Customer_Model_Customer)) {
$this->setCustomer(self::CUSTOMER_RANDOM);
}
$transaction = Mage::getModel('core/resource_transaction');
$transaction = Mage::getModel('core/resource_transaction');
$this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
복사
복사됨
복사
복사됨
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
$reservedOrderId = Mage::getSingleton('eav/config')
$reservedOrderId = Mage::getSingleton('eav/config')
->getEntityType('order')
->getEntityType('order')
->fetchNewIncrementId($this->_storeId);
->fetchNewIncrementId($this->_storeId);
$currencyCode = Mage::app()->getBaseCurrencyCode();
$currencyCode = Mage::app()->getBaseCurrencyCode();
$this->_order = Mage::getModel('sales/order')
$this->_order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setIncrementId($reservedOrderId)
->setStoreId($this->_storeId)
->setStoreId($this->_storeId)
->setQuoteId(0)
->setQuoteId(0)
->setGlobalCurrencyCode($currencyCode)
->setGlobalCurrencyCode($currencyCode)
->setBaseCurrencyCode($currencyCode)
->setBaseCurrencyCode($currencyCode)
->setStoreCurrencyCode($currencyCode)
->setStoreCurrencyCode($currencyCode)
->setOrderCurrencyCode($currencyCode);
->setOrderCurrencyCode($currencyCode);
$this->_order->setCustomerEmail($this->_customer->getEmail())
$this->_order->setCustomerEmail($this->_customer->getEmail())
->setCustomerFirstname($this->_customer->getFirstname())
->setCustomerFirstname($this->_customer->getFirstname())
->setCustomerLastname($this->_customer->getLastname())
->setCustomerLastname($this->_customer->getLastname())
->setCustomerGroupId($this->_customer->getGroupId())
->setCustomerGroupId($this->_customer->getGroupId())
->setCustomerIsGuest(0)
->setCustomerIsGuest(0)
->setCustomer($this->_customer);
->setCustomer($this->_customer);
$billing = $this->_customer->getDefaultBillingAddress();
$billing = $this->_customer->getDefaultBillingAddress();
$billingAddress = Mage::getModel('sales/order_address')
$billingAddress = Mage::getModel('sales/order_address')
->setStoreId($this->_storeId)
->setStoreId($this->_storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setCustomerId($this->_customer->getId())
->setCustomerId($this->_customer->getId())
->setCustomerAddressId($this->_customer->getDefaultBilling())
->setCustomerAddressId($this->_customer->getDefaultBilling())
->setCustomerAddress_id($billing->getEntityId())
->setCustomerAddress_id($billing->getEntityId())
->setPrefix($billing->getPrefix())
->setPrefix($billing->getPrefix())
->setFirstname($billing->getFirstname())
->setFirstname($billing->getFirstname())
->setMiddlename($billing->getMiddlename())
->setMiddlename($billing->getMiddlename())
->setLastname($billing->getLastname())
->setLastname($billing->getLastname())
->setSuffix($billing->getSuffix())
->setSuffix($billing->getSuffix())
->setCompany($billing->getCompany())
->setCompany($billing->getCompany())
->setStreet($billing->getStreet())
->setStreet($billing->getStreet())
->setCity($billing->getCity())
->setCity($billing->getCity())
->setCountry_id($billing->getCountryId())
->setCountry_id($billing->getCountryId())
->setRegion($billing->getRegion())
->setRegion($billing->getRegion())
->setRegion_id($billing->getRegionId())
->setRegion_id($billing->getRegionId())
->setPostcode($billing->getPostcode())
->setPostcode($billing->getPostcode())
->setTelephone($billing->getTelephone())
->setTelephone($billing->getTelephone())
->setFax($billing->getFax());
->setFax($billing->getFax());
$this->_order->setBillingAddress($billingAddress);
$this->_order->setBillingAddress($billingAddress);
$shipping = $this->_customer->getDefaultShippingAddress();
$shipping = $this->_customer->getDefaultShippingAddress();
$shippingAddress = Mage::getModel('sales/order_address')
$shippingAddress = Mage::getModel('sales/order_address')
->setStoreId($this->_storeId)
->setStoreId($this->_storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setCustomerId($this->_customer->getId())
->setCustomerId($this->_customer->getId())
->setCustomerAddressId($this->_customer->getDefaultShipping())
->setCustomerAddressId($this->_customer->getDefaultShipping())
->setCustomer_address_id($shipping->getEntityId())
->setCustomer_address_id($shipping->getEntityId())
->setPrefix($shipping->getPrefix())
->setPrefix($shipping->getPrefix())
->setFirstname($shipping->getFirstname())
->setFirstname($shipping->getFirstname())
->setMiddlename($shipping->getMiddlename())
->setMiddlename($shipping->getMiddlename())
->setLastname($shipping->getLastname())
->setLastname($shipping->getLastname())
->setSuffix($shipping->getSuffix())
->setSuffix($shipping->getSuffix())
->setCompany($shipping->getCompany())
->setCompany($shipping->getCompany())
->setStreet($shipping->getStreet())
->setStreet($shipping->getStreet())
->setCity($shipping->getCity())
->setCity($shipping->getCity())
->setCountry_id($shipping->getCountryId())
->setCountry_id($shipping->getCountryId())
->setRegion($shipping->getRegion())
->setRegion($shipping->getRegion())
->setRegion_id($shipping->getRegionId())
->setRegion_id($shipping->getRegionId())
->setPostcode($shipping->getPostcode())
->setPostcode($shipping->getPostcode())
->setTelephone($shipping->getTelephone())
->setTelephone($shipping->getTelephone())
->setFax($shipping->getFax());
->setFax($shipping->getFax());
$this->_order->setShippingAddress($shippingAddress)
$this->_order->setShippingAddress($shippingAddress)
->setShippingMethod($this->_shippingMethod);
->setShippingMethod($this->_shippingMethod);
$orderPayment = Mage::getModel('sales/order_payment')
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($this->_storeId)
->setStoreId($this->_storeId)
->setCustomerPaymentId(0)
->setCustomerPaymentId(0)
->setMethod($this->_paymentMethod)
->setMethod($this->_paymentMethod)
->setPoNumber(' – ');
->setPoNumber(' – ');
$this->_order->setPayment($orderPayment);
$this->_order->setPayment($orderPayment);
$this->_addProducts($products);
$this->_addProducts($products);
$this->_order->setSubtotal($this->_subTotal)
$this->_order->setSubtotal($this->_subTotal)
->setBaseSubtotal($this->_subTotal)
->setBaseSubtotal($this->_subTotal)
->setGrandTotal($this->_subTotal)
->setGrandTotal($this->_subTotal)
->setBaseGrandTotal($this->_subTotal);
->setBaseGrandTotal($this->_subTotal);
$transaction->addObject($this->_order);
$transaction->addObject($this->_order);
$transaction->addCommitCallback(array($this->_order, 'place'));
$transaction->addCommitCallback(array($this->_order, 'place'));
$transaction->addCommitCallback(array($this->_order, 'save'));
$transaction->addCommitCallback(array($this->_order, 'save'));
$transaction->save();
$transaction->save();
}
}
protected function _addProducts($products)
protected function _addProducts($products)
{
{
$this->_subTotal = 0;
$this->_subTotal = 0;
foreach ($products as $productRequest) {
foreach ($products as $productRequest) {
if ($productRequest['product'] == 'rand') {
if ($productRequest['product'] == 'rand') {
$productsCollection = Mage::getResourceModel('catalog/product_collection');
$productsCollection = Mage::getResourceModel('catalog/product_collection');
$productsCollection->addFieldToFilter('type_id', 'simple');
$productsCollection->addFieldToFilter('type_id', 'simple');
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection);
$productsCollection->getSelect()
$productsCollection->getSelect()
->order('RAND()')
->order('RAND()')
->limit(rand($productRequest['min'], $productRequest['max']));
->limit(rand($productRequest['min'], $productRequest['max']));
foreach ($productsCollection as $product) {
foreach ($productsCollection as $product) {
$this->_addProduct(array(
$this->_addProduct(array(
'product' => $product->getId(),
'product' => $product->getId(),
'qty' => rand(1, 2)
'qty' => rand(1, 2)
));
));
}
}
} else {
} else {
$this->_addProduct($productRequest);
$this->_addProduct($productRequest);
}
}
}
}
}
}
protected function _addProduct($requestData)
protected function _addProduct($requestData)
{
{
$request = new Varien_Object();
$request = new Varien_Object();
$request->setData($requestData);
$request->setData($requestData);
$product = Mage::getModel('catalog/product')->load($request['product']);
$product = Mage::getModel('catalog/product')->load($request['product']);
$cartCandidates = $product->getTypeInstance(true)
$cartCandidates = $product->getTypeInstance(true)
->prepareForCartAdvanced($request, $product);
->prepareForCartAdvanced($request, $product);
if (is_string($cartCandidates)) {
if (is_string($cartCandidates)) {
throw new Exception($cartCandidates);
throw new Exception($cartCandidates);
}
}
if (!is_array($cartCandidates)) {
if (!is_array($cartCandidates)) {
$cartCandidates = array($cartCandidates);
$cartCandidates = array($cartCandidates);
}
}
$parentItem = null;
$parentItem = null;
$errors = array();
$errors = array();
$items = array();
$items = array();
foreach ($cartCandidates as $candidate) {
foreach ($cartCandidates as $candidate) {
$item = $this->_productToOrderItem($candidate, $candidate->getCartQty());
$item = $this->_productToOrderItem($candidate, $candidate->getCartQty());
$items[] = $item;
$items[] = $item;
/**
/**
* As parent item we should always use the item of first added product
* As parent item we should always use the item of first added product
*/
*/
if (!$parentItem) {
if (!$parentItem) {
$parentItem = $item;
$parentItem = $item;
}
}
if ($parentItem && $candidate->getParentProductId()) {
if ($parentItem && $candidate->getParentProductId()) {
$item->setParentItem($parentItem);
$item->setParentItem($parentItem);
}
}
/**
/**
* We specify qty after we know about parent (for stock)
* We specify qty after we know about parent (for stock)
*/
*/
$item->setQty($item->getQty() + $candidate->getCartQty());
$item->setQty($item->getQty() + $candidate->getCartQty());
// collect errors instead of throwing first one
// collect errors instead of throwing first one
if ($item->getHasError()) {
if ($item->getHasError()) {
$message = $item->getMessage();
$message = $item->getMessage();
if (!in_array($message, $errors)) { // filter duplicate messages
if (!in_array($message, $errors)) { // filter duplicate messages
$errors[] = $message;
$errors[] = $message;
}
}
}
}
}
}
if (!empty($errors)) {
if (!empty($errors)) {
Mage::throwException(implode("\n", $errors));
Mage::throwException(implode("\n", $errors));
}
}
foreach ($items as $item) {
foreach ($items as $item) {
$this->_order->addItem($item);
$this->_order->addItem($item);
}
}
return $items;
return $items;
}
}
function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1)
function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1)
{
{
$rowTotal = $product->getFinalPrice() * $qty;
$rowTotal = $product->getFinalPrice() * $qty;
$options = $product->getCustomOptions();
$options = $product->getCustomOptions();
$optionsByCode = array();
$optionsByCode = array();
foreach ($options as $option) {
foreach ($options as $option) {
$quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData())
$quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData())
->setProduct($option->getProduct());
->setProduct($option->getProduct());
$optionsByCode[$quoteOption->getCode()] = $quoteOption;
$optionsByCode[$quoteOption->getCode()] = $quoteOption;
}
}
$product->setCustomOptions($optionsByCode);
$product->setCustomOptions($optionsByCode);
$options = $product->getTypeInstance(true)->getOrderOptions($product);
$options = $product->getTypeInstance(true)->getOrderOptions($product);
$orderItem = Mage::getModel('sales/order_item')
$orderItem = Mage::getModel('sales/order_item')
->setStoreId($this->_storeId)
->setStoreId($this->_storeId)
->setQuoteItemId(0)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setQuoteParentItemId(NULL)
->setProductId($product->getId())
->setProductId($product->getId())
->setProductType($product->getTypeId())
->setProductType($product->getTypeId())
->setQtyBackordered(NULL)
->setQtyBackordered(NULL)
->setTotalQtyOrdered($product['rqty'])
->setTotalQtyOrdered($product['rqty'])
->setQtyOrdered($product['qty'])
->setQtyOrdered($product['qty'])
->setName($product->getName())
->setName($product->getName())
->setSku($product->getSku())
->setSku($product->getSku())
->setPrice($product->getFinalPrice())
->setPrice($product->getFinalPrice())
->setBasePrice($product->getFinalPrice())
->setBasePrice($product->getFinalPrice())
->setOriginalPrice($product->getFinalPrice())
->setOriginalPrice($product->getFinalPrice())
->setRowTotal($rowTotal)
->setRowTotal($rowTotal)
->setBaseRowTotal($rowTotal)
->setBaseRowTotal($rowTotal)
->setWeeeTaxApplied(serialize(array()))
->setWeeeTaxApplied(serialize(array()))
->setBaseWeeeTaxDisposition(0)
->setBaseWeeeTaxDisposition(0)
->setWeeeTaxDisposition(0)
->setWeeeTaxDisposition(0)
->setBaseWeeeTaxRowDisposition(0)
->setBaseWeeeTaxRowDisposition(0)
->setWeeeTaxRowDisposition(0)
->setWeeeTaxRowDisposition(0)
->setBaseWeeeTaxAppliedAmount(0)
->setBaseWeeeTaxAppliedAmount(0)
->setBaseWeeeTaxAppliedRowAmount(0)
->setBaseWeeeTaxAppliedRowAmount(0)
->setWeeeTaxAppliedAmount(0)
->setWeeeTaxAppliedAmount(0)
->setWeeeTaxAppliedRowAmount(0)
->setWeeeTaxAppliedRowAmount(0)
->setProductOptions($options);
->setProductOptions($options);
$this->_subTotal += $rowTotal;
$this->_subTotal += $rowTotal;
return $orderItem;
return $orderItem;
}
}
}
}
복사
복사됨
복사
복사됨
저장된 비교 결과
원본
파일 열기
<?php class Pricebinc_App_Model_OrderCreate extends Mage_Core_Model_Abstract { const CUSTOMER_RANDOM = null; protected $_shippingMethod = 'freeshipping_freeshipping'; protected $_paymentMethod = 'cashondelivery'; protected $_customer = self::CUSTOMER_RANDOM; protected $_subTotal = 0; protected $_order; protected $_storeId; public function setShippingMethod($methodName) { $this->_shippingMethod = $methodName; } public function setPaymentMethod($methodName) { $this->_paymentMethod = $methodName; } public function setCustomer($customer) { if ($customer instanceof Mage_Customer_Model_Customer) { $this->_customer = $customer; } if (is_numeric($customer)) { $this->_customer = Mage::getModel('customer/customer')->load($customer); } else if ($customer === self::CUSTOMER_RANDOM) { $customers = Mage::getResourceModel('customer/customer_collection'); $customers ->getSelect() ->limit(1) ->order('RAND()'); $id = $customers->getFirstItem()->getId(); $this->_customer = Mage::getModel('customer/customer')->load($id); } } public function createOrder($products) { if (!($this->_customer instanceof Mage_Customer_Model_Customer)) { $this->setCustomer(self::CUSTOMER_RANDOM); } $transaction = Mage::getModel('core/resource_transaction'); $this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID); $reservedOrderId = Mage::getSingleton('eav/config') ->getEntityType('order') ->fetchNewIncrementId($this->_storeId); $currencyCode = Mage::app()->getBaseCurrencyCode(); $this->_order = Mage::getModel('sales/order') ->setIncrementId($reservedOrderId) ->setStoreId($this->_storeId) ->setQuoteId(0) ->setGlobalCurrencyCode($currencyCode) ->setBaseCurrencyCode($currencyCode) ->setStoreCurrencyCode($currencyCode) ->setOrderCurrencyCode($currencyCode); $this->_order->setCustomerEmail($this->_customer->getEmail()) ->setCustomerFirstname($this->_customer->getFirstname()) ->setCustomerLastname($this->_customer->getLastname()) ->setCustomerGroupId($this->_customer->getGroupId()) ->setCustomerIsGuest(0) ->setCustomer($this->_customer); $billing = $this->_customer->getDefaultBillingAddress(); $billingAddress = Mage::getModel('sales/order_address') ->setStoreId($this->_storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) ->setCustomerId($this->_customer->getId()) ->setCustomerAddressId($this->_customer->getDefaultBilling()) ->setCustomerAddress_id($billing->getEntityId()) ->setPrefix($billing->getPrefix()) ->setFirstname($billing->getFirstname()) ->setMiddlename($billing->getMiddlename()) ->setLastname($billing->getLastname()) ->setSuffix($billing->getSuffix()) ->setCompany($billing->getCompany()) ->setStreet($billing->getStreet()) ->setCity($billing->getCity()) ->setCountry_id($billing->getCountryId()) ->setRegion($billing->getRegion()) ->setRegion_id($billing->getRegionId()) ->setPostcode($billing->getPostcode()) ->setTelephone($billing->getTelephone()) ->setFax($billing->getFax()); $this->_order->setBillingAddress($billingAddress); $shipping = $this->_customer->getDefaultShippingAddress(); $shippingAddress = Mage::getModel('sales/order_address') ->setStoreId($this->_storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) ->setCustomerId($this->_customer->getId()) ->setCustomerAddressId($this->_customer->getDefaultShipping()) ->setCustomer_address_id($shipping->getEntityId()) ->setPrefix($shipping->getPrefix()) ->setFirstname($shipping->getFirstname()) ->setMiddlename($shipping->getMiddlename()) ->setLastname($shipping->getLastname()) ->setSuffix($shipping->getSuffix()) ->setCompany($shipping->getCompany()) ->setStreet($shipping->getStreet()) ->setCity($shipping->getCity()) ->setCountry_id($shipping->getCountryId()) ->setRegion($shipping->getRegion()) ->setRegion_id($shipping->getRegionId()) ->setPostcode($shipping->getPostcode()) ->setTelephone($shipping->getTelephone()) ->setFax($shipping->getFax()); $this->_order->setShippingAddress($shippingAddress) ->setShippingMethod($this->_shippingMethod); $orderPayment = Mage::getModel('sales/order_payment') ->setStoreId($this->_storeId) ->setCustomerPaymentId(0) ->setMethod($this->_paymentMethod) ->setPoNumber(' – '); $this->_order->setPayment($orderPayment); $this->_addProducts($products); $this->_order->setSubtotal($this->_subTotal) ->setBaseSubtotal($this->_subTotal) ->setGrandTotal($this->_subTotal) ->setBaseGrandTotal($this->_subTotal); $transaction->addObject($this->_order); $transaction->addCommitCallback(array($this->_order, 'place')); $transaction->addCommitCallback(array($this->_order, 'save')); $transaction->save(); } protected function _addProducts($products) { $this->_subTotal = 0; foreach ($products as $productRequest) { if ($productRequest['product'] == 'rand') { $productsCollection = Mage::getResourceModel('catalog/product_collection'); $productsCollection->addFieldToFilter('type_id', 'simple'); Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection); $productsCollection->getSelect() ->order('RAND()') ->limit(rand($productRequest['min'], $productRequest['max'])); foreach ($productsCollection as $product) { $this->_addProduct(array( 'product' => $product->getId(), 'qty' => rand(1, 2) )); } } else { $this->_addProduct($productRequest); } } } protected function _addProduct($requestData) { $request = new Varien_Object(); $request->setData($requestData); $product = Mage::getModel('catalog/product')->load($request['product']); $cartCandidates = $product->getTypeInstance(true) ->prepareForCartAdvanced($request, $product); if (is_string($cartCandidates)) { throw new Exception($cartCandidates); } if (!is_array($cartCandidates)) { $cartCandidates = array($cartCandidates); } $parentItem = null; $errors = array(); $items = array(); foreach ($cartCandidates as $candidate) { $item = $this->_productToOrderItem($candidate, $candidate->getCartQty()); $items[] = $item; /** * As parent item we should always use the item of first added product */ if (!$parentItem) { $parentItem = $item; } if ($parentItem && $candidate->getParentProductId()) { $item->setParentItem($parentItem); } /** * We specify qty after we know about parent (for stock) */ $item->setQty($item->getQty() + $candidate->getCartQty()); // collect errors instead of throwing first one if ($item->getHasError()) { $message = $item->getMessage(); if (!in_array($message, $errors)) { // filter duplicate messages $errors[] = $message; } } } if (!empty($errors)) { Mage::throwException(implode("\n", $errors)); } foreach ($items as $item) { $this->_order->addItem($item); } return $items; } function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1) { $rowTotal = $product->getFinalPrice() * $qty; $options = $product->getCustomOptions(); $optionsByCode = array(); foreach ($options as $option) { $quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData()) ->setProduct($option->getProduct()); $optionsByCode[$quoteOption->getCode()] = $quoteOption; } $product->setCustomOptions($optionsByCode); $options = $product->getTypeInstance(true)->getOrderOptions($product); $orderItem = Mage::getModel('sales/order_item') ->setStoreId($this->_storeId) ->setQuoteItemId(0) ->setQuoteParentItemId(NULL) ->setProductId($product->getId()) ->setProductType($product->getTypeId()) ->setQtyBackordered(NULL) ->setTotalQtyOrdered($product['rqty']) ->setQtyOrdered($product['qty']) ->setName($product->getName()) ->setSku($product->getSku()) ->setPrice($product->getFinalPrice()) ->setBasePrice($product->getFinalPrice()) ->setOriginalPrice($product->getFinalPrice()) ->setRowTotal($rowTotal) ->setBaseRowTotal($rowTotal) ->setWeeeTaxApplied(serialize(array())) ->setBaseWeeeTaxDisposition(0) ->setWeeeTaxDisposition(0) ->setBaseWeeeTaxRowDisposition(0) ->setWeeeTaxRowDisposition(0) ->setBaseWeeeTaxAppliedAmount(0) ->setBaseWeeeTaxAppliedRowAmount(0) ->setWeeeTaxAppliedAmount(0) ->setWeeeTaxAppliedRowAmount(0) ->setProductOptions($options); $this->_subTotal += $rowTotal; return $orderItem; } }
수정본
파일 열기
<?php class NameApp_App_Model_OrderCreate extends Mage_Core_Model_Abstract { const CUSTOMER_RANDOM = null; protected $_shippingMethod = 'freeshipping_freeshipping'; protected $_paymentMethod = 'cashondelivery'; protected $_subTotal = 0; protected $_order; protected $_storeId = '0'; public function _construct() { parent::_construct(); $this->_init('app/ordercreate'); } public function setShippingMethod($methodName) { $this->_shippingMethod = $methodName; } public function setPaymentMethod($methodName) { $this->_paymentMethod = $methodName; } public function createOrder($products) { $transaction = Mage::getModel('core/resource_transaction'); $this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID); $this->_customer = Mage::getSingleton('customer/session')->getCustomer(); $reservedOrderId = Mage::getSingleton('eav/config') ->getEntityType('order') ->fetchNewIncrementId($this->_storeId); $currencyCode = Mage::app()->getBaseCurrencyCode(); $this->_order = Mage::getModel('sales/order') ->setIncrementId($reservedOrderId) ->setStoreId($this->_storeId) ->setQuoteId(0) ->setGlobalCurrencyCode($currencyCode) ->setBaseCurrencyCode($currencyCode) ->setStoreCurrencyCode($currencyCode) ->setOrderCurrencyCode($currencyCode); $this->_order->setCustomerEmail($this->_customer->getEmail()) ->setCustomerFirstname($this->_customer->getFirstname()) ->setCustomerLastname($this->_customer->getLastname()) ->setCustomerGroupId($this->_customer->getGroupId()) ->setCustomerIsGuest(0) ->setCustomer($this->_customer); $billing = $this->_customer->getDefaultBillingAddress(); $billingAddress = Mage::getModel('sales/order_address') ->setStoreId($this->_storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) ->setCustomerId($this->_customer->getId()) ->setCustomerAddressId($this->_customer->getDefaultBilling()) ->setCustomerAddress_id($billing->getEntityId()) ->setPrefix($billing->getPrefix()) ->setFirstname($billing->getFirstname()) ->setMiddlename($billing->getMiddlename()) ->setLastname($billing->getLastname()) ->setSuffix($billing->getSuffix()) ->setCompany($billing->getCompany()) ->setStreet($billing->getStreet()) ->setCity($billing->getCity()) ->setCountry_id($billing->getCountryId()) ->setRegion($billing->getRegion()) ->setRegion_id($billing->getRegionId()) ->setPostcode($billing->getPostcode()) ->setTelephone($billing->getTelephone()) ->setFax($billing->getFax()); $this->_order->setBillingAddress($billingAddress); $shipping = $this->_customer->getDefaultShippingAddress(); $shippingAddress = Mage::getModel('sales/order_address') ->setStoreId($this->_storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) ->setCustomerId($this->_customer->getId()) ->setCustomerAddressId($this->_customer->getDefaultShipping()) ->setCustomer_address_id($shipping->getEntityId()) ->setPrefix($shipping->getPrefix()) ->setFirstname($shipping->getFirstname()) ->setMiddlename($shipping->getMiddlename()) ->setLastname($shipping->getLastname()) ->setSuffix($shipping->getSuffix()) ->setCompany($shipping->getCompany()) ->setStreet($shipping->getStreet()) ->setCity($shipping->getCity()) ->setCountry_id($shipping->getCountryId()) ->setRegion($shipping->getRegion()) ->setRegion_id($shipping->getRegionId()) ->setPostcode($shipping->getPostcode()) ->setTelephone($shipping->getTelephone()) ->setFax($shipping->getFax()); $this->_order->setShippingAddress($shippingAddress) ->setShippingMethod($this->_shippingMethod); $orderPayment = Mage::getModel('sales/order_payment') ->setStoreId($this->_storeId) ->setCustomerPaymentId(0) ->setMethod($this->_paymentMethod) ->setPoNumber(' – '); $this->_order->setPayment($orderPayment); $this->_addProducts($products); $this->_order->setSubtotal($this->_subTotal) ->setBaseSubtotal($this->_subTotal) ->setGrandTotal($this->_subTotal) ->setBaseGrandTotal($this->_subTotal); $transaction->addObject($this->_order); $transaction->addCommitCallback(array($this->_order, 'place')); $transaction->addCommitCallback(array($this->_order, 'save')); $transaction->save(); } protected function _addProducts($products) { $this->_subTotal = 0; foreach ($products as $productRequest) { if ($productRequest['product'] == 'rand') { $productsCollection = Mage::getResourceModel('catalog/product_collection'); $productsCollection->addFieldToFilter('type_id', 'simple'); Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection); $productsCollection->getSelect() ->order('RAND()') ->limit(rand($productRequest['min'], $productRequest['max'])); foreach ($productsCollection as $product) { $this->_addProduct(array( 'product' => $product->getId(), 'qty' => rand(1, 2) )); } } else { $this->_addProduct($productRequest); } } } protected function _addProduct($requestData) { $request = new Varien_Object(); $request->setData($requestData); $product = Mage::getModel('catalog/product')->load($request['product']); $cartCandidates = $product->getTypeInstance(true) ->prepareForCartAdvanced($request, $product); if (is_string($cartCandidates)) { throw new Exception($cartCandidates); } if (!is_array($cartCandidates)) { $cartCandidates = array($cartCandidates); } $parentItem = null; $errors = array(); $items = array(); foreach ($cartCandidates as $candidate) { $item = $this->_productToOrderItem($candidate, $candidate->getCartQty()); $items[] = $item; /** * As parent item we should always use the item of first added product */ if (!$parentItem) { $parentItem = $item; } if ($parentItem && $candidate->getParentProductId()) { $item->setParentItem($parentItem); } /** * We specify qty after we know about parent (for stock) */ $item->setQty($item->getQty() + $candidate->getCartQty()); // collect errors instead of throwing first one if ($item->getHasError()) { $message = $item->getMessage(); if (!in_array($message, $errors)) { // filter duplicate messages $errors[] = $message; } } } if (!empty($errors)) { Mage::throwException(implode("\n", $errors)); } foreach ($items as $item) { $this->_order->addItem($item); } return $items; } function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1) { $rowTotal = $product->getFinalPrice() * $qty; $options = $product->getCustomOptions(); $optionsByCode = array(); foreach ($options as $option) { $quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData()) ->setProduct($option->getProduct()); $optionsByCode[$quoteOption->getCode()] = $quoteOption; } $product->setCustomOptions($optionsByCode); $options = $product->getTypeInstance(true)->getOrderOptions($product); $orderItem = Mage::getModel('sales/order_item') ->setStoreId($this->_storeId) ->setQuoteItemId(0) ->setQuoteParentItemId(NULL) ->setProductId($product->getId()) ->setProductType($product->getTypeId()) ->setQtyBackordered(NULL) ->setTotalQtyOrdered($product['rqty']) ->setQtyOrdered($product['qty']) ->setName($product->getName()) ->setSku($product->getSku()) ->setPrice($product->getFinalPrice()) ->setBasePrice($product->getFinalPrice()) ->setOriginalPrice($product->getFinalPrice()) ->setRowTotal($rowTotal) ->setBaseRowTotal($rowTotal) ->setWeeeTaxApplied(serialize(array())) ->setBaseWeeeTaxDisposition(0) ->setWeeeTaxDisposition(0) ->setBaseWeeeTaxRowDisposition(0) ->setWeeeTaxRowDisposition(0) ->setBaseWeeeTaxAppliedAmount(0) ->setBaseWeeeTaxAppliedRowAmount(0) ->setWeeeTaxAppliedAmount(0) ->setWeeeTaxAppliedRowAmount(0) ->setProductOptions($options); $this->_subTotal += $rowTotal; return $orderItem; } }
비교하기