$productModel = Mage::getModel('catalog/product');
$productId = $productModel->getIdBySku($sku);
$product = $productModel->load($productId);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($product, array('qty' => $quantity));
$cart->save();
이 ** 당신이 카트를 생성합니다 $ SKU 및 $ 수량 를 정의해야하고 추가 할 때
// Check whether the product could be loaded
if(isset($_POST['create_order'])){
// Get the customer model
$customer = Mage::getModel('customer/customer');
// Set the website id associated with the customer
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
if($_POST['customer'] == 'exist')
$email = $_POST['mageUser']['email'];
else
$email = $_POST['mageUser']['email_id'];
// Try to load the customer by email
$customer->loadByEmail($email);
// Check whether the customer not exists
if(!$customer->getId())
{
// Create the customer
$customer->setEmail($email);
$customer->setFirstname($_POST['mageUser']['first_name']);
$customer->setLastname($_POST['mageUser']['last_name']);
$customer->save();
$fname = $_POST['mageUser']['first_name'];
$lname = $_POST['mageUser']['last_name'];
}
else {
$fname = $customer->getFirstname();
$lname = $customer->getLastname();
}
// Set the esstial order data
$orderData = array(
'currency' => 'USD',
'account' => array(
'group_id' => Mage_Customer_Model_Group::NOT_LOGGED_IN_ID,
'email' => $email
),
'billing_address' => array(
'firstname' => $fname,
'lastname' => $lname,
'street' => 'pos',
'city' => 'Pos',
'country_id' => '54',
'region_id' => 'BW',
'postcode' => 'pos',
'telephone' => 'pos',
),
'comment' => array(
'customer_note' => "Order placed by point of sales\n"
),
'send_confirmation' => false
);
// Set the shipping address to the billing address
$orderData['shipping_address'] = $orderData['billing_address'];
// Set the payment method
$paymentMethod = 'pay';
// Set the shipping method
$shippingMethod = 'freeshipping_freeshipping';
// Get the backend quote session
$quoteSession = Mage::getSingleton('adminhtml/session_quote');
// Set the session store id
$quoteSession->setStoreId(Mage::app()->getStore('default')->getId());
// Set the session customer id
$quoteSession->setCustomerId($customer->getId());
// Get the backend order create model
$orderCreate = Mage::getSingleton('adminhtml/sales_order_create');
// Import the data
$orderCreate->importPostData($orderData);
// Calculate the shipping rates
$orderCreate->collectShippingRates();
// Set the shipping method
$orderCreate->setPaymentMethod($paymentMethod);
// Set the payment method to the payment instance
$orderCreate->getQuote()->getPayment()->addData(array('method' => $paymentMethod));
// Set the shipping method
$orderCreate->setShippingMethod($shippingMethod);
// Set the quote shipping address shipping method
$orderCreate->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod);
// Add the product
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
$prd = array();
foreach ($cartItems as $item) {
$prd[$item->getId()] = array('qty' => $item->getQty());
}
$orderCreate->addProducts($prd);
//print_r($orderCreate); exit();
// Initialize data for price rules
$orderCreate->initRuleData();
// Save the quote
$orderCreate->saveQuote();
// Create the order
$order = $orderCreate->createOrder();
$order->setData('state', "complete");
$order->setStatus("complete");
$history = $order->addStatusHistoryComment('Order marked as complete automatically.', false);
$history->setIsCustomerNotified(false);
$order->save();
}
그것은
이 오류 메시지가 표시 제품 너무; 잘하면 방향이 올바른지 알 수 있습니다.
그런 다음 문제, 오류 메시지는 무엇입니까? – Amir
문제를 추가했는지 확인하십시오. –