I'm trying to get my order number on the event sales_order_payment_pay
But somehow I got nothing .. maybe you can help me ?
Here is the sample of my code in my Observer.php
public function functioninobserver($observer) {
$orderid = $observer->getEvent()->getInvoice()->getIncrementId();
}
First of all it returns nothing and I think Incrementid is not the same as orderthe number ...
The invoice isn't the same as the order, you can get the order in a slightly roundabout way...
$orderId = $observer->getPayment()->getOrder()->getId();
$orderNumber = $observer->getPayment()->getOrder()->getIncrementId();
Just to clarify; The order ID is used internally in the database. The order number is what's displayed on screen and looks like #100000123.
Related
I want to decrease quantity from products table when order is added to cart ,right now when I add something to cart, correct quantity is not decreasing. For example if a I place order of 6 items then a random amount gets decreased from my products table.
Here is my code in order controller of livewire:
public function IncrementQty($cartId)
{
$carts=Cart::find($cartId);
$carts->increment('product_qty',1);
$updatePrice=$carts->product_qty * $carts->product->price;
$carts->update(['product_price'=>$updatePrice]);
$getProductStock=Product::where(['id'=>$carts['product_id']])->first()->toArray();
$newStock=$getProductStock['quantity'] - $carts['product_qty'];
Product::where(['id'=>$carts['product_id']])->update(['quantity'=>$newStock]);
$this->mount();
}
Being a beginner I am unable to understand about what's wrong here. Should I be substracting it from orderDetails table instead of carts table? If yes then how?
A couple of things to note first,
You should not call $this->mount() or any other lifecycle hooks from Livewire. If there are code inside your mount-method you need to run again, then abstract it into a separate method, and call it from both your mount() method and your IncrementQty() method.
You can use model-route-binding to inject the correct model into the method
public function IncrementQty(Cart $cart)
You can then simplify your code by using the product relation on the cart directly, and calling decrement() on that.
public function IncrementQty(Cart $cart)
{
$product = $cart->product;
$cart->increment('product_qty', 1);
$cart->update(['product_price' => $cart->product_qty * $product->price]);
$product->decrement('quantity', 1);
$this->mount(); // Do not call mount manually, abstract the contents you need into a separate method instead
}
If you are still experiencing issues, then it cannot possibly be this piece of code, or you are misreading your data somehow - in any case, if you are still experiencing issues, you will need to provide additional information with your table-structure, models, how you call this method, and data before and after your actions.
it seems you have a problem in this line:
$newStock=$getProductStock['quantity'] - $carts['product_qty'];
$carts is an object not an array, so, do:
$newStock = $getProductStock['quantity'] - $carts->product_qty;
I am looking to something similar to this one (thats works with email), But instead of get the customer by email, i want to get him by the Tax/VAT number.
$customer = Mage::getModel('customer/customer')->setWebsiteId($website->getWebsiteId())->loadByEmail($customerEmail);
I found this example but its doesn't work.
$customer = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*')->addFieldToFilter('taxvat', $ss_5_last)->load();
Thank you.
If the attribute taxvat contains unique values, your approach was almost right. But the result of your second try returns a collection. I think you need a customer object, so try this one:
$customer = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('taxvat', $ss_5_last)
->getFirstItem();
This returns a customer object you can work with. Caution: If taxvat does NOT contain unique values, this method could return the wrong customer, because it always returns the first item of a collection with more than one item.
I totally begin with Magento during my training period. I've been working on a project for two weeks now and sometimes I can't figure out how to proceed.
I'm gonna try to represent my categories tree :
- accessories
* visors
* communication systems
* other
- helmets
* a lot of subcategories and subcategories...
My actual problem is : I'm in one of the accessories subcategory (for example visors). I added a form with a select allowing to choose a helmet model. When the select is submitted, I'd like to display the list of visors related to the chosen helmet model (that is actually a virtual product).
I can get the current category ID (in this case, visors) and the virtual product ID (so the helmet model). But I can't figure out how to display related products by both product ID and category ID.
I tried stuff like this :
$relatedProducts = Mage::getModel('catalog/product_link')
->getCollection()
->addCategoryFilter($myCurrentCat)
->addFieldToFilter('product_id',$myVirtualProductId)
->addFieldToFilter('link_type_id','1');
But it seems not to work.
Any help is welcome. Thanks.
EDIT : 10 days after I asked this question, I still don't know how to solve my problem. If anyone could help, even a little, just a clue...
I cannot test this at the moment, but you can try to do something like this:
$collection = Mage::getSingleton('catalog/product_link')
->useRelatedLinks()
->getProductCollection()
->setIsStrongMode();
$product = Mage::getModel('catalog/product')->load($productId);
$collection->setProduct($product);
$collection->addCategoryFilter($category); //I'm not sure if this will work correctly
I'm gonna test it, when I will get some more time.
Actually I just solved my problem by myself.
Here is my solution if it could help anyone in the future :
$mainProduct = Mage::getModel('catalog/category')->load($myCurrentCat->getId());
$mainProduct = $mainProduct->getProductCollection();
$mainProduct = $mainProduct->addAttributeToFilter('product_id', $myVirtualProductId);
foreach ($mainProduct as $product) {
$related = $product->getRelatedProductIds();
if ($this->array_contains($related, $myVirtualProductId)) {
//TODO
}
}
I'm seeing an unexpected behavior in collections that maybe someone can enlighten me on (or maybe it's a bit of PHP I don't grok) (sorry for the length of this post, but I had to include sample code and results).
My goal was to write a report where I get an order and it's order items, then go to the invoice and shipment data for the order and get the matching order item data from their items. I know that there is only one invoice and shipment for all orders, so even though Magento uses a 1-M relationship between orders and invoices/shipments, I can take advantage of it as if it were 1-1
I know that the items are all related using the order_item_id fields, so I tried to write a function that used the the following call -
$invoiceItem = $order
->getInvoiceCollection()
->getFirstItem()
->getItemsCollection()
->addFieldToFilter('order_item_id', $orderItem->getItemId())
->getFirstItem();
But that didn't results I expected, what I saw was the same invoice item returned regardless of the order item id used in the filter.
So, to try to understand the problem, I wrote the following small program to see how the queries where created.
<?php
require dirname(__FILE__).'/../app/Mage.php';
umask(0);
Mage::app('default');
$orders = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('state', 'processing')
->addAttributeToSort('created_at', 'desc')
->addAttributeToSort('status', 'asc')
->load();
foreach ($orders as $order) {
echo "\ngetting data for order id ". $order->getId();
$items = $order->getAllItems();
$invoice = $order->getInvoiceCollection()->getFirstItem();
foreach ($items as $orderItem) {
echo "\n\ngetting data for order item id ". $orderItem->getItemId();
$invoiceItems = $order
->getInvoiceCollection()
->getFirstItem()
->getItemsCollection()
->addFieldToFilter('order_item_id', $orderItem->getItemId());
echo "\n".$invoiceItems->getSelect();
}
die; //just quit after one iteration
}
The output from this program was the following -
getting data for order id 7692
getting data for order item id 20870
SELECT `main_table`.* FROM `sales_flat_invoice_item` AS `main_table` WHERE (parent_id = '7623') AND (order_item_id = '20870')
getting data for order item id 20871
SELECT `main_table`.* FROM `sales_flat_invoice_item` AS `main_table` WHERE (parent_id = '7623') AND (order_item_id = '20870') AND (order_item_id = '20871')
getting data for order item id 20872
SELECT `main_table`.* FROM `sales_flat_invoice_item` AS `main_table` WHERE (parent_id = '7623') AND (order_item_id = '20870') AND (order_item_id = '20871') AND (order_item_id = '20872')
As you can see, every time though the loop another "AND (order_item_id =" was added for each item Id that I was filtering on. I thought that every time though the loop, I'd be getting a fresh version of the collection from using $order->getInvoiceCollection().
So, can anyone tell me what's going wrong in my sample code and educate me on the correct way to do this?
Thanks!
Regarding your business question: need more info. Is the goal to generate a collection with order item objects which are EACH aware of invoice and shipment details? It seems like there are rendering concerns getting pushed into modeling concerns.
Regarding the select statement question: Varien collections have an optimization which prevents them from accessing the storage backend more than once. This standard behavior is achieved in DB collection instances by setting the _isCollectionLoaded property to true.
In your case, the invoice collection instance is created via the order instance stored in a protected property, and immediately load()ed via IteratorAggregate (invoked via foreach). Because you are using the same order object instance in each iteration, you are dealing with this loaded invoice collection instance and are effectively calling addFieldToFilter(/* next order id */) with each iteration, resulting in the ever-expanding WHERE clause. This specific optimization can easily be worked around by calling $order->reset(). This brings us back to the salient issue though, which is the need to better understand the goal and (likely) use a custom collection or to manipulate a collection to join in the specific data that you need.
I'm trying to work out the total value of all the products in my inventory. Each product in the table has a price and quantity. So I need to multiply each product's price by the quantity and then add all of these together to get a total for all products. From a previous question, I now have the MySQL query to do this:
select sum(product_price*product_quantity) as stockvalue from products
But now I'm trying to work out the correct way to do this with CodeIgniter's ActiveRecord.
Grateful for any advice on this. I've looked in the docs but can only find select_sum() for one field, rather than the sum of multiplying two together.
I have tried using the following:
public function getStockValue() {
$this->db->select('sum('product_price'*'product_quantity') as stockvalue', FALSE);
$this->db->from('products');
$query = $this->db->get();
return $query->row()->stockvalue;
}
Try this:
$this->db->select('sum(`product_price`*`product_quantity`) as stockvalue', FAlSE);
I'm not sure if you need the second argument. I haven't worked with CI for quite awhile.
$this->db->select('sum(product_price) * sum(product_quantity) as stockvalue', FALSE);