How I get Current selected shipping address Id magneto 2.3.4 - magento

I Want to get selected shipping id on the checkout page.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$session = $objectManager->get('Magento\Checkout\Model\Session');
$quote = $session->getQuote();
$shippingMethod = $quote->getShippingAddress();
echo $addressId = $shippingMethod->getCustomerAddressId();
I try to get shipping address id but it always gives save id when I change bellow image from checkout
https://drive.google.com/file/d/11N3SBaahNlugWzMw2ySAZHn4Yh4kQ64q/view
It not change the shipping id show same. How I get the current Selected shipping address Id?

protected $quoteRepository;
public function __construct (
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
) {
$this->quoteRepository = $quoteRepository;
}
$quote = $this->quoteRepository->get($quoteId);
$addressId = $quote->getCustomerAddressId();

Related

How to get Shipping data in magento

I am trying to get shipping data but getting blank
$orderID = 100000062;
$order = Mage::getModel('sales/order')->load($orderID);
foreach ($order->getShipmentsCollection() as $shipment) {
Mage::log($shipment->getData());
}
You can try this way :
$shipmentCollection = Mage::getResourceModel('sales/order_shipment_collection')
->setOrderFilter($order)
->load();

Magento Collection get clients by payment method

I would like to know if anyone already coded a Magento Collection to get the customer name by the payment method? I used the code bellow and now I have the payment method that I need, now I just have to get the client's first and last name. Thank you all for the help.
$collection = Mage::getResourceModel('sales/order_payment_collection')
->addFieldToSelect('*');
foreach ($collection as $method) {
if ($method->getMethod() == "mundipagg_boleto") {
print $method->getMethod()."<br>";
}
}
$collection = Mage::getResourceModel('sales/order_payment_collection')
->addFieldToSelect('*')
->addFieldToFilter('method', "mundipagg_boleto");
foreach ($collection as $orderPayment) {
$orderId = $orderPayment->getParentId();
$order = Mage::getModel('sales/order')->load($orderId);
$customerId = $order->getCustomerId();
}
After that you can load customer's model by customerId

Magento Multiple Wishlist creation programmatically

I know how to add products to a customer's wishlist programmatically however it only adds to one wishlist. I have the multiple wishlist option set to enabled however I do not know how to create a new wishlist instead of merging products into the existing wishlist.
public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();
$items = $currentQuote->getAllVisibleItems();
//$wishlist = Mage::helper('wishlist')->getWishlist();
//Mage::register('wishlist', $wishlist);
$wishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('m/d/Y', time());
$wishlist->setCustomerId($customer->getId());
$wishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
foreach ($items as $item)
{
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($product, $buyRequest);
if(is_string($result))
{
Mage::throwException($result);
}
$wishlist->save();
}
//Mage::unregister('wishlist');
}
I've had this problem before. To add a product to a customer's wishlist you need to start a wishlist model and call the addNewItem method passing the product object. Here is how I do it.
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));
Hope this helps!
EDITED:
$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);
Check for reference this: app/code/core/Mage/Wishlist/Model/Wishlist.php
LATEST EDIT:
I see that your problem is here:
$result = $wishlist->addNewItem($product, $buyRequest);
FINAL EDIT:
Then create another function and add the items and wishlist you want to add it to (as parameters). This should add items to the wishlist that you have passed as an argument when calling the function. Its the only solution I can come up with. Hope this helps!
I just stumbled across this question because I'm trying to do something similar - your code part helped me to create a new wishlist, and I've managed to do the rest so thought I'd share.
This code will;
Create a new wishlist with the name 'Quote dd/mm/YY h:m:s'
Load the wishlist collection for the current user, ordered by highest ID first
Load the newest wishlist
Add all basket items to it
Redirect the user to the new wishlist view.
public function addToQuoteAction() {
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerId = $customerData->getId();
} else {
Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
return $this->_redirectUrl($this->_getRefererUrl());
}
// Create the wishlist
$newWishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('d/m/Y h:m:s', time());
$newWishlist->setCustomerId($customerId);
$newWishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
// Find the newly create list and load it
$wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;
$firstWish = $wishlistCollection->getFirstItem();
$wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
$cart = Mage::getModel('checkout/cart')->getQuote();
//getAllItems
foreach ($cart->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($productId, $buyRequest);
$wishlist->save();
}
Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
}

Creating Magento orders programmatically

Is there a way that an order can be created within Magento with a "custom" product which would be defined within the order. The order wouldn't require the custom products to be created. These custom products would have custom prices and a custom product title determined when creating the order.
So when creating order, I would just simply specify some custom products with the prices then add them to the order.
Again, the products will not be defined anywhere within Magento, so I would just say, I want to add xyz at 9.99 and zxy at 1.99 and maybe I want to add another xyz at 3.99.
The order would show as,
xyz | 9.99
zxy | 1.99
xyz | 3.99
TOTAL | 15.97
Programatically create an order with custom product is possible, here is an example code:
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('admin');
$order = create();
echo $order;
function create()
{
$storeId = 1;
if (!$storeId) {
$storeIds = Mage::app()->getWebsite($customer->getWebsiteId())->getStoreIds();
reset($storeIds);
$storeId = current($storeIds);
}
$order = Mage::getModel('sales/order')
->setState('new');
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('checkmo')
->setPo_number(' - ');
$order->setPayment($orderPayment);
$billingAddress = Mage::getModel('sales/order_address');
$shippingAddress = Mage::getModel('sales/order_address');
$order->setStoreId($storeId)
->setQuoteId(0)
->setGlobal_currency_code('EUR')
->setBase_currency_code('EUR')
->setStore_currency_code('EUR')
->setOrder_currency_code('EUR')
->setStatus($orderData['status']);
// set Customer data
$order->setCustomer_email('a#b.com')
->setCustomerFirstname('firstname')
->setCustomerLastname('lastname')
->setCustomer_is_guest(1);
// set Billing Address
$billingAddress
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setPrefix('mr')
->setFirstname('firstname')
->setLastname('lastname')
->setCompany('company')
->setStreet('street')
->setCity('city')
->setCountry_id('US')
->setPostcode('12345');
$order->setBillingAddress($billingAddress);
$shippingAddress
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setPrefix('mr')
->setFirstname('firstname')
->setLastname('lastname')
->setCompany('company')
->setStreet('street')
->setCity('city')
->setCountry_id('US')
->setPostcode('12345');
$order->setShippingAddress($shippingAddress)
->setShipping_method('freeshipping_freeshipping')
->setShippingDescription('Free Shipping - Free');
$orderItem = Mage::getModel('sales/order_item')
->setStoreId(1)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setQtyBackordered(NULL)
->setTotalQtyOrdered(10)
->setQtyOrdered(10)
->setName('custom product name')
->setPrice(100)
->setBasePrice(10)
->setOriginalPrice(10)
->setRowTotal(1000)
->setBaseRowTotal(1000);
$order->addItem($orderItem);
$order->setSubtotal(2000)
->setSubtotalIncludingTax(2000)
->setBaseSubtotal(2000)
->setGrandTotal(2000)
->setBaseGrandTotal(2000)
->setTaxAmount(0)
->setTotalQtyOrdered(10);
$order->save();
return $order;
}
?>

Magento: Increase "Qty" upon cancel a shipped order

I'm on Magento 1.7.0.2. I'm using "Cash On Delivery" as my payment method.I'll tell you exactly the steps That i follow on any order. When an order is placed(Qty decreased 1 item), I create a shipment for it and if customer paid the order grand total price. I create an invoice for that order.
My problem, If an order is placed(Qty decreased 1 item), I create a shipment for this order. If the customer refused to pay, I open this order and "Cancel" it and on this case the "Qty" doesn't increase so How can I make it increase?
If order status is Processing
Create a custom module with observer for 'order_cancel_before' (see example # Change Magento default status for duplicated products change <catalog_model_product_duplicate> to <order_cancel_before>
since <order_cancel_before> is not defined in app/code/core/Mage/Sales/Model/Order.php
You could override/rewrite order model class see e.g http://phprelated.myworks.ro/how-to-override-rewrite-model-class-in-magento/
In your local module do
public function cancel()
{
if ($this->canCancel()) {
Mage::dispatchEvent('order_cancel_before', array('order' => $this));
$this->getPayment()->cancel();
$this->registerCancellation();
Mage::dispatchEvent('order_cancel_after', array('order' => $this));
}
return $this;
}
Or you could create a new method increaseProductQty() in your model and copy the code below into it (this way you would not need an observer). Then replace the line Mage::dispatchEvent('order_cancel_before'... with $this->increaseProductQty()
In your observer method (pseudo code)
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
foreach ($order->getItemsCollection() as $item)
{
$productId = $item->getProductId();
$qty = $item->getQty();
// you need to check order status to make sure it processing
//$order->getStatus() (assuming you are canceling entire order)
//$order->getPayment();
$product = Mage::getModel('catalog/product')->load($product_id);
$stock_obj = Mage::getModel('cataloginventory/stock_item')->load($product_id);
$stockData = $stock_obj->getData();
$product_qty_before = (int)$stock_obj->getQty();
$product_qty_after = (int)($product_qty_before + $qty);
$stockData['qty'] = $product_qty_after;
$productInfoData = $product->getData();
$productInfoData['updated_at'] = $curr_date;
$product->setData($productInfoData);
$product->setStockData($stockData);
$product->save();
}
If you have issue with updating stock see Set default product values when adding new product in Magento 1.7
Reference http://pragneshkaria.com/programatically-change-products-quantity-after-order-cancelled-magento/
If order status is Pending
Take a look at System > Configuration > Inventory
Set Items’ Status to be In Stock When Order is Cancelled — Controls whether products in pending orders automatically return to the stock if orders are cancelled. Scope: STORE VIEW.
Read more #
How to Manage Magento Store Inventory?
ADMIN: System → Configuration → Inventory Tab
Thanks to R.S as he helped me more & more.
I followed all instructions on R.S's reply https://stackoverflow.com/a/13330543/1794834 and I've only changed the observer code. Here is the observer code that worked with me on Magento 1.7.0.2.
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
foreach ($order->getItemsCollection() as $item)
{
$productId = $item->getProductId();
$qty = (int)$item->getQtyOrdered();
$product = Mage::getModel('catalog/product')->load($productId);
$stock_obj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockData = $stock_obj->getData();
$product_qty_before = (int)$stock_obj->getQty();
$product_qty_after = (int)($product_qty_before + $qty);
$stockData['qty'] = $product_qty_after;
/*
* it may be case that admin has enabled product add in stock, after product sold,
* he set is_in_stock = 0 and if order cancelled then we need to update only qty not is_in_stock status.
* make a note of it
*/
if($product_qty_after != 0) {
$stockData['is_in_stock'] = 1;
}else{
$stockData['is_in_stock'] = 0;
}
$productInfoData = $product->getData();
$productInfoData['updated_at'] = $curr_date;
$product->setData($productInfoData);
$product->setStockData($stockData);
$product->save();
}

Resources