Given this function in Magento:
public function capture(Varien_Object $payment, $amount)
{
$order = $payment->getOrder();
$order_id = $order->getId();
$invoice = ????
$invoice_id = $invoice->getId();
}
How can I get the invoice or invoice ID?
The Mage_Sales_Model_Order has methods like hasInvoices() and getInvoiceCollection():
public function capture(Varien_Object $payment, $amount)
{
$order = $payment->getOrder();
$order_id = $order->getId();
if ($order->hasInvoices()) {
$oInvoiceCollection = $order->getInvoiceCollection();
foreach ($oInvoiceCollection as $oInvoice) {
$invoice_id = $oInvoice->getId();
// ...
}
}
}
Related
Hello I am making a cart but when I click on add to cart link then it says:
Undefined property: App\Cart::$totalPrice
Error: https://ibb.co/ysB5CfG
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart
{
private $contents;
private $totalQty;
private $contentsPrice;
public function __construct($oldCart){
if ($oldCart) {
$this->contents = $oldCart->contents;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function addProduct($product, $qty){
$products = ['qty' => 0, 'price' => $product->price, 'product' => $product];
if ($this->contents) {
if (array_key_exists($product->slug, $this->contents)) {
$product = $this->contents[$product->slug];
}
}
$products['qty'] +=$qty;
$products['price'] +=$product->price * $product['qty'];
$this->contents[$product->slug] = $product;
$this->totalQty+=$qty;
$this->totalPrice += $product->price;
}
public function getContents()
{
return $this->contents;
}
public function getTotalQty()
{
return $this->totalQty;
}
public function getTotalPrice()
{
return $this->totalPrice;
}
}
controller:
public function cart()
{
if (!Session::has('cart')) {
return view('products.cart');
}
$cart = Session::has('cart');
return view('product.cart', compact('cart'));
}
public function addToCart(Product $product, Request $request, $qty= null)
{
if(empty(Auth::user()->email)){
$data['email'] = '';
}else{
$data['email'] = Auth::user()->email;
}
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$qty = $request->qty ? $request->qty : 1;
$cart = new Cart($oldCart);
$cart->addProduct($product, $qty);
Session::put('cart', $cart);
return redirect()->back()->with('flash_message_success', 'Product $product->title has been successfully added to Cart');
}
routes:
Route::get('cart', 'Admin\ProductController#cart')->name('product.cart');
// Add to cart
Route::get('/addToCart/{product}/{qty?}', 'Admin\ProductController#addToCart')->name('addToCart');
You should use get() methods in cart() function in controller file.
public function cart()
{
if (!Session::has('cart')) {
return view('products.cart');
}
$cart = Session::get('cart');
return view('product.cart', compact('cart'));
}
I want to show success message after submitting the form. The currently message is working it is saying item successfully saved. But i also want to change this message. Is there a way or am i doing something wrong. This is my code example in model of my custom component.
class IAdonaModelPost extends JModelAdmin
{
protected function allowEdit($data = array(), $key = 'id')
{
//echo "<pre>"; print_R($data); print_r($key);die;
//return JFactory::getUser()->authorise('core.edit', 'com_events.message.'.((int) isset($data[$key]) ? $data[$key] : 0)) or parent::allowEdit($data, $key);
}
public function getTable($type = 'Eventpost', $prefix = 'iAdonaTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
public function getForm($data = array(), $loadData = true)
{
$form = $this->loadForm('com_iadona.post', 'post', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
//$displaymsg = "My message text..";
// JFactory::getApplication()->enqueueMessage($displaymsg);
}
protected function loadFormData()
{
$data = JFactory::getApplication()->getUserState('com_iadona.edit.post.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
}
I have the following code, which will get all products from all orders for one logged in customer which works fine. I want to add to this code so that it only returns products from a specified attribute set. I believe I have both bits of code that I need they just won't work together.
Code I want to add to is:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
$customer_id = $customer->getId();
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
echo various variables here;
}
}
}
?>
Code I have used before to get products from a specified attribute set
$attrSetName = 'Beer';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->load($attrSetName, 'attribute_set_name')
->getAttributeSetId();
//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('attribute_set_id', $attributeSetId);
You can add this piece of code to make your script work in the foreach loop.
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if(0 == strcmp($attributeSetName, 'Beer') {
//add your logic
}else{
continue;
}
Update:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
$customer_id = $customer->getId();
}
$collection =
Mage::getModel('sales/order')->getCollection();
$collection->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
$item->getProduct()->getSku();
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($item->getProduct()->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if(0 == strcmp($attributeSetName, 'Beer')) {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
// echo various variables here;
}else{
continue;
}
}
}
}
print_r($uniuqProductSkus);
?>
I develop observer method in Magento for filter payment methods based on shipping methods. This is my method:
class Devpassion_Paymentfilter_Model_Observer {
public function paymentMethodIsActive(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$method = $event->getMethodInstance();
$result = $event->getResult();
$carriers = Mage::getSingleton('shipping/config')->getActiveCarriers();
foreach ($carriers as $carrier) {
// $carrierCode = $carrier->getId();
if ($carrier->getId() == 'flatrate' ){
if($method->getCode() == 'checkmo' OR $method->getCode() == 'paypal_standard'){
$result->isAvailable = true;
}else{
$result->isAvailable = false;
}
}
}
}
}
Results of this is that for all shipping method this filter is true. So for all shipping method paypal and money check shows up and all other not.
Please advice me how to set up this condition to filter just for one specific shipping method.
public function paymentMethodIsActive($observer)
{
/**
* #var $quote Mage_Sales_Model_Quote
*/
$quote = $observer->getEvent()->getQuote();
$method = $observer->getEvent()->getMethodInstance();
$result = $observer->getEvent()->getResult();
$shipping_method = $quote->getShippingAddress()->getShippingMethod();
if ($shipping_method == 'flatrate_flatrate' && $method->getCode() == 'checkmo') {
$result->isAvailable = false;
}
}
How can I show invoice id in \app\code\local\Mage\Sales\Model\Order\Pdf\Abstract.php
I don't need to show getRealOrderId() into pdf invoice, but I need invoice id.
How can I do that?
First be sure, that your order is loaded ... take a look at:
protected function insertOrder(&$page, $obj, $putOrderId = true)
{
if ($obj instanceof Mage_Sales_Model_Order) {
$shipment = null;
$order = $obj;
} elseif ($obj instanceof Mage_Sales_Model_Order_Shipment) {
$shipment = $obj;
$order = $shipment->getOrder();
}
.....
}
So later you can use this snippet:
$invoiceIncrementId = '';
if ($order->hasInvoices()) {
// "$_eachInvoice" is each of the Invoice object of the order "$order"
foreach ($order->getInvoiceCollection() as $_eachInvoice) {
$invoiceIncrementId = $_eachInvoice->getIncrementId();
}
}
I referred, that forum reply: http://www.magentocommerce.com/boards/viewthread/198222/#t393368
Good luck.