1

I am not very familiar with Magento, but I am learning.

Basically, when a order is made with a "subscription" product, the subscription has several options.

I store the selected options out to a data table.

On subscription renewal, I get a notification from a third party payment provider.

Using that data, I can pull up the data from my custom table to identify the customer, the product id, the order id (from the initial order), and the product options the customer selected on the initial order. (The initial order has other products on it as well).

When I get the product custom options I am using:

//$item is the order item on the initial order
$productOptions = $item->getData('product_options'); 

This is the option data that I am storing for the product.

When I attempt to create the new order upon notification I am using:

            $customer = Mage::getModel('customer/customer')->load($MyCustomModule->getMagentocustomerId());

            $productId = $MyCustomModule->getProductId(); 

            $websiteId = Mage::app()->getWebsite()->getId();
            $store = Mage::app()->getStore();

            $customerBillingAddress = $customer->getDefaultBillingAddress(); 

            $customerShippingAddress = $customer->getDefaultShippingAddress(); 


            $quote = Mage::getModel('sales/quote')->setStoreId($store->getId()); 
            $quote->assignCustomer($customer); 

            $addressBillingData = array(
                'firstname' => $customerBillingAddress->getFirstName(),
                'lastname' => $customerBillingAddress->getLastName,
                'street' => $customerBillingAddress->getStreet,
                'city' => $customerBillingAddress->getCity,
                'postcode'=>$customerBillingAddress->getPostCode,
                'telephone' => $customerBillingAddress->getPhoneNumber,
                'country_id' => $customerBillingAddress->getCountryId,
                'region_id' => $customerBillingAddress->getRegionId
            );


            $addressShippingData = array(
                'firstname' => $customerShippingAddress->getFirstName(),
                'lastname' => $customerShippingAddress->getLastName,
                'street' => $customerShippingAddress->getStreet,
                'city' => $customerShippingAddress->getCity,
                'postcode'=>$customerShippingAddress->getPostCode,
                'telephone' => $customerShippingAddress->getPhoneNumber,
                'country_id' => $customerShippingAddress->getCountryId,
                'region_id' => $customerShippingAddress->getRegionId
            );


            $billingAddress = $quote->getBillingAddress()->addData($addressBillingData);
            $shippingAddress = $quote->getShippingAddress()->addData($addressShippingData);


            $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping')->setPaymentMethod('checkmo');



            $quote->getPayment()->importData(array('method' => 'checkmo'));


            /// Need to add the product with the custom options to the quote. 



            $quote->collectTotals()->save();


            $service = Mage::getModel('sales/service_quote', $quote);


            $service->submitAll();


            $order = $service->getOrder();



            $order->setStatus('processing');


            $order->save();      

I am not sure how I would add the product to the quote with the custom options. In addition, I am not sure how to set the payment method other than checkmo. The third party billing system is billing and in most cases, the payment is already completed when I get the notification.

I plan to have a separate function that is close to this one for when a payment fails. I plan to set that order status as onhold so that we can query onhold orders to contact customers to get new payment data.

Shipping will always be free with orders created here.

Can someone help point me in the right direction to add the custom product options to the product? And do I need to do this on the quote or wait and do it on the order?

The custom options data looks like this:

a:6:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:128:"aHR0cDovL2lhbWNvbG9yYnJhdmUuY29tL2J1c2luZXNzZXMtY3JlYXRpbmctYS1oZWFsdGh5LXBsYW5ldC0xLWZvci10aGUtcGxhbmV0LWNvdXJhZ2UtMjEzNC5odG1s";s:7:"product";s:4:"2110";s:8:"form_key";s:16:"6dcig9wK6tXQc1xx";s:15:"related_product";s:0:"";s:15:"super_attribute";a:1:{i:156;s:2:"72";}s:3:"qty";s:1:"1";}s:15:"attributes_info";a:1:{i:0;a:2:{s:5:"label";s:6:"Finish";s:5:"value";s:4:"Gold";}}s:11:"simple_name";s:66:"Custom Description";s:10:"simple_sku";s:5:"11031";s:20:"product_calculations";i:1;s:13:"shipment_type";i:0;}

Update

I used

$productOptions = $item->getData('product_options'); 
$productOptions = print_r($productOptions, true);

And was able to get a better stored value. When I add the product to the quote that is being created I use the following:

// options is the $productOptions value from above. 
$quote->addProduct($product,new Varien_Object($options));

However, I keep getting the error "Please add Shipping Method" on the quote creation.

I try to set the Shipping Method this way:

$quote->getShippingAddress()
                ->setShippingMethod('tablerate_bestway')
                ->setPaymentMethod('purchaseorder'); 

I have tried several ways, and have been searching for the answer for this. Once I get the Shipping Method taken care of, perhaps I can see if the way I am loading the options is working.

  • @adarsh-khatri any ideas on how to do this? – user1579943 Dec 8 '16 at 4:28
  • @adarsh-khatri I am not opposed to copying the data from the old order, but I am still not sure how to get the product options from the old order to the new quote. – user1579943 Dec 8 '16 at 4:29
  • The shipping method error log is from: /app/code/core/Mage/Sales/Model/Service/Quote.php(303): Mage::throwException('Please specify ...') – user1579943 Dec 8 '16 at 23:09
0

You can add product with custom option this way.

$product = Mage::getModel('catalog/product')->load($productId);
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(
        '[option_id]' => '[option value]', //replace these
     )
);

$quote->addProduct($product, new Varien_Object($params));
$quote->save();

You will need to find the option_id and option value then replace it in the code.

I haven't tried but this may help you.

UPDATE

To set shipping address from order, you can do this way:

        $converter = Mage::getModel('sales/convert_order');

        $quote->setBillingAddress($converter->addressToQuoteAddress($order->getBillingAddress())); //this will set billing address, comment if you don't need this
        $quote->setShippingAddress($converter->toQuoteShippingAddress($order); //this will convert order to shipping address
  • Is there a way to serialize the option values provided? – user1579943 Dec 8 '16 at 13:00
  • Do print_r($value) and you will see how can you get specific data. You might need to do unserialize first. – Adarsh Khatri Dec 8 '16 at 22:11
  • So using print_r do I need to unserialize and store the data, then when I get ready to use the stored data in the order I need to serialize? – user1579943 Dec 9 '16 at 2:49
  • You will need to provide data as $params (qty, options) etc, rest magento will take care. – Adarsh Khatri Dec 9 '16 at 3:27
  • I changed up a bit, and just pulled the previous order and copied it over. The order is creating and the product is adding. But the options are not adding. – user1579943 Dec 9 '16 at 20:24
0

Here is my code I have, but it doesn't list the product custom options select like on the original order.

            $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
            $converter = Mage::getModel('sales/convert_order');
            $quote = $converter->toQuote($order);

            $quote->setBillingAddress($converter->addressToQuoteAddress($order->getBillingAddress())); 
            $quote->setShippingAddress($converter->addressToQuoteAddress($order->getShippingAddress()));

            $quote->setStore($order->getStore()); 
            $quote->setCustomer($customer);
            $converter->paymentToQuotePayment($order->getPayment(), $quote->getPayment()); 
            $grandtotal = 0; 
            $cart = Mage::getSingleton('checkout/cart');
            foreach ($order->getItemsCollection() as $item) {
                if($item->getSku() == '8087'){
                    //$cart->addOrderItem($item);
                    $quoteItem = Mage::getModel('sales/quote_item');
                    $prod = Mage::getSingleton('catalog/product')->load($productId);

                    $grandtotal = $item->getPrice(); 
                    $quoteItem->setProduct($prod); 
                    $quoteItem->setCustomPrice($item->getPrice());
                    $quoteItem->setBaseSubTotal($item->getPrice()); 
                    $quoteItem->setRowTotal($item->getPrice()); 
                    $quoteItem->setOriginalPrice($item->getPrice());
                    $quoteItem->setProductOptions($item->getProductOptions());
                    // $a_options = array(
                        // 'options' => $item->getProductOptions()
                    // );

                    // $quoteItem->addOption(new Varien_Object(
                        // array(
                            // 'product' => $prod,
                            // 'code' => 'additional_options',
                            // 'value' => unserialize($a_options)
                        // )
                    // ));


                    $quoteItem->setQty(1); 
                    //$quoteItem->setOptions = $item->getProductOptions(); 


                    $quote->addItem($quoteItem);

                }
            }

            $quote->save();

            // $quote->setGrandTotal($grandtotal);
            // $totals = $quote->getTotals();
            // $subtotal = $totals["subtotal"]->getValue();
            // $quote->setSubTotal($subtotal);
            $quote->collectTotals()->save();






            $convert = Mage::getModel('sales/convert_quote');
            $newOrder = $convert->toOrder($quote);

            foreach($quote->getAllItems() as $item){
                $orderItem = $convert->itemToOrderItem($item);
                if ($item->getParentItem()) {
                   $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
                }
                $newOrder->addItem($orderItem);
            }

            $payment = $convert->paymentToOrderPayment($quote->getPayment());

            $newOrder->setPayment($payment);
            $newOrder->setBillingAddress($convert->addressToOrderAddress($quote->getBillingAddress())); 
            $newOrder->setShippingAddress($convert->addressToOrderAddress($quote->getShippingAddress())); 



            $newOrder->setSubtotal($grandtotal)
            ->setBaseSubtotal($grandtotal)
            ->setGrandTotal($grandtotal)
            ->setBaseGrandTotal($grandtotal);


            $message = 'Generated Order';
            $newOrder->addStatusToHistory($newOrder->getStatus(), $message);

            $newOrder->save();

            $quote->setIsActive(false)->save();

            $invoice = Mage::getModel('sales/service_order', $newOrder)->prepareInvoice();
            $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::STATE_PAID);
            $invoice->register();
            $invoice->setAmount($grandtotal);
            $invoice->pay()->save();
            $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($newOrder);

            // $transactionSave->addCommitCallback(array($newOrder, 'place'));
            // $transactionSave->addCommitCallback(array($newOrder, 'save'));
            $transactionSave->save();

            $payment->pay($invoice);
  • @adarsh-khatri basically I am trying to copy the selected options from the original order to the quote. Then convert that quote to a new order and retain that originally selected custom option values. – user1579943 Dec 9 '16 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.