I'm using woocommerce and installed a print delivery note plugin. What I am trying to do is edit the print file so if someone selects a certain 'additional shipping rate', it will print the relevant printed postage impression - 1st class vs 2nd class
I have in my head an IF statement based on the value of the shipping (FREE vs £1) which I can I do.
BUT, I'm having problems trying to extract the value so I can check it.
Digging deep it looks like I need to use the get_shipping() method in the WC_Order class so I can check the value - http://docs.woothemes.com/wc-apidocs/class-WC_Order.html - and this is where I fail.... miserably.
Looking at this page - http ://docs.woothemes.com/document/class-reference/#listofclassesinwoocommerce - I can copy and paste the WC_Cart example directly into the .php file that is used to print from and echo the value successfully, but when I change it to what I think is correct to use get_shipping(), I get "Fatal error: Call to a member function get_shipping() on a non-object in....." instead.
Here's what I put
<?php global $woocommerce;
$order_shipping_total = $woocommerce->order->get_shipping();
echo $order_shipping_total ;
?>
Now I am guessing, as I have reached the limit of my ability, I need to initialise the WC_Order class but I'm stumped at how to do this.
Any help would be greatly received.
Cheers
You were almost there! You need to initialise the order indeed. $woocommerce has no idea which order you are talking about. So you create an object from the order:
$order = new WC_Order( $order_id);
Now all information from this order (including your shipping value) is accessible via $order. The only thing you need to know is the appropriate $order_id.
Since you are in the print delivery notes environment, you can access that value via
$wcdn->print->order_id.
this will do what you need:
<?php
global $wcdn;
$order = new WC_Order($wcdn->print->order_id);
$order_shipping_total = $order->get_total_shipping();
echo $order_shipping_total;
?>
from > WC 3.you can get the total shipping cost of the current order with
<?php
$current_shipping_cost = WC()->cart->get_cart_shipping_total();
echo $current_shipping_cost;
?>
[2018 Update]
You can use
<?php
$order = wc_get_order( $order_id );
$order_shipping_total = $order->get_shipping_total();
echo $order_shipping_total;
?>
Related
I need to create a function that looks for all the new orders entered in woocommerce and then just update this order without changing anything. (like if you clicked on the order and then clicked update with everything remaining the same)
The reason for this is that I'm bringing orders from another source into woocommerce but the plugin to export them to another platform isn't listening for these external orders until they are updated even if nothing changes.
I know I have to use the woocommerce_new_order hook to listen for the new orders coming in but how can I just update it without changing anything?
function custom_woocommerce_order($order_id) {
if (!$order_id) {
return;
}
//Will this work for what I need?
$order_id = $order->save();
//Or should I do this instead?
$order = wc_get_order($order_id);
do_action( 'woocommerce_update_order', $order );
}
add_action('woocommerce_new_order', 'custom_woocommerce_order');
I have a script that changes the status of orders based on our ERP system.
In addition to that, we need to add customer notes. I found the way to do it:
$order->add_order_note($note);
$order->save();
Unfortunately this won't work outside the order edit screen, I tried to run it from my custom plugin. (source)
If I do it via $order->update_status($status, $note); it only updates the status.
Is there a way to add a note outside the edit screen? (Including e-mailing the customer)
If the note is for the customer (and has to be visible for him) you need to use instead the WC_Order method set_customer_note() (or both):
$order->set_customer_note($note);
// $order->add_order_note($note);
$order->save();
Or:
$order->set_customer_note($note);
$order->update_status($status, $note);
This need to be done before saving the order data or updating the order status.
To re-send the email notification to the customer (if needed) you can use from the current order ID:
$emails = WC()->mailer()->get_emails();
$emails['WC_Email_Customer_Completed_Order']->trigger( $order_id );
// OR: $emails['WC_Email_Customer_Processing_Order']->trigger( $order_id );
//Pass order id from hook or function with $order_id
$order = new WC_Order( $order_id );
$note = 'Add note here';
$order->add_order_note($note);
$order->save();
I'm constructing a new class of order. Passing the order ID and order note and then saving the order again.
This is how we update our site from our ERP. But as Loic said this method creates a private note. Use his
$order->set_customer_note($note);
to create a customer note.
My issue: The client has 12 locations, each location is a different corporation hence a different PayPal account per business. By default woocommerce only supports one email to be entered to process the payment. The goal is to use one installation of wordpress / woocommerce then direct the user to the PayPal account associated with the location they have selected upon checkout.
My Theory / Attempt: originally I thought of implementing this feature by setting up a variation so the user can select a location which will then pass a parameter to the URL. The parameter would later be used within the PHP to overwrite the default email.
My Problem: I am having trouble with overwriting the default email that is entered within the admin settings, I cant seem to locate this email in the database. I am assuming the file pertaining this modification is located at: wp-content/plugins/woocommerce/includes/gateways/paypal but would prefer doing this the wordpress way vs editing core files, for obvious reasons. After doing some research I have found the following action shown below, but this is for the proceed to checkout button, I am looking to interact with the proceed to PayPal button. I am fluent in PHP but not the best with WordPress development. I would think this is a popular issue since majority of franchises would deal with such a scenario, but I am unpleasantly surprised on the amount of information regarding this topic. If someone could point me in the right direction of conquering this task it would be greatly appreciated!
remove_action('woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
add_action('woocommerce_proceed_to_checkout', 'change_url_to_checkout', 20);
function change_url_to_checkout(){
$extra_url = 'put_your_extra_page_url_here';
?>
<?php _e( 'Proceed to Checkout', 'woocommerce' ); ?>
<?php
}
I can see two functions to be written here.
1. To alter the order data when an order is created. This is where we save the email needed.
add_action( 'woocommerce_checkout_update_order_meta', 'woocommerce_checkout_update_order_meta' );
function woocommerce_checkout_update_order_meta( $order_id ) {
$email = 'paypal#location1.com';
// do something here as to use the right email.
// you have $order_id.
// can be used as:
// $order = wc_get_order( $order_id );
// $order->get_billing_address_1() to get the address to check order address.
// or use $_POST['location'] if ever you posted some data.
update_post_meta( $order_id, '_alternative_paypal_email', $email );
}
2. Then use woocommerce_paypal_args to alter the args that is being passed to paypal.
add_filter( 'woocommerce_paypal_args', 'woocommerce_paypal_args', 10, 2 );
function woocommerce_paypal_args( $paypal_args, $order ) {
$email = get_post_meta( $order->get_id(), '_alternative_paypal_email', true );
if ( !empty( $email ) ) {
$paypal_args['business'] = $email;
}
return $paypal_args;
}
To summarize, this is just an example. But these two hooks is enough to get what you need.
I hope someone can help me with this, I am trying to extract the product prices from magento based on customer group.
I am not using tier pricing, I simply have a product with a standard price and I have specified different prices for each customer group. For some reason I have been unable to extract this information.
I can see that the price mappings seems to be held in the table 'catalog_product_index_group_price' so I guess I could write direct SQL to extract these but I would much rather use the PHP Mage model to do this, or the V2 SOAP API.
I have tried many methods, currently im using something like below, but without success the price variable is always empty.
$rules = Mage::getResourceModel('catalogrule/rule');
$price = $rules->getRulePrice($now, $websiteId, $customer_group_id, $productID);
Please try the following
$product = Mage::getModel('catalog/product')->load($productId);
$groupPrices = $product->getData('group_price')
$groupPrices should now be an array with the data you are looking for.
the code didnt format well in the comment so here it is again!
<?php
include_once '../App/Mage.php';
Mage::app();
$productID = $_GET["id"];
$pd = Mage::getModel('catalog/product')->load($productID);
$groupPrices = $pd->getData('group_price');
echo json_encode($groupPrices);
?>
I'm trying to get the Order Increment Id in Magento, on the success.phtml page so that I can use this for affiliate tracking.
I'm using the following code, but it is giving an error on the second line;
$order = Mage::getSingleton('sales/order')->getLastOrderId();
$lastOrderId = $order->getIncrementId();
The error reads:
Fatal error: Call to a member function getIncrementId() on a non-object on line 34: $LastOrderId = $order->getIncrementId();
I was wondering if anyone has any ideas on how to get the Order Increment Id? This is the reference number seen in the admin, usually something like: #1000123
If you're specifically doing this on the checkout success page - in success.phtml - then the code to get the order increment ID is already available in the template, since it is displayed to the customer.
You just need the following:
$orderId = $this->getOrderId();
Note that this won't work on other pages so, for those, you'd need to use:
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order in your code is the last order ID...as the function name implies. If this isn't the value you want, then use it to load an order, and then use the getter on that:
$order = Mage::getModel('sales/order');
$order->load(Mage::getSingleton('sales/order')->getLastOrderId());
$lastOrderId = $order->getIncrementId();
This will work perfectly, I m running this one in my module now.
$last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
Hope it helps thanks. :)
Your call to
Mage::getSingleton('sales/order')
isn't returning an object. Try
var_dump(Mage::getSingleton('sales/order'));
to confirm.
I haven't dived into the checkout code recently, but I'm pretty sure that's because sales/order will get you the order in progress. Once the order's been placed it's no longer in progress.
The "right" way to do this would be to create an observer for one of the events that Magento fires during checkout. The
checkout_onepage_controller_success_action
event should be sufficient, assuming you haven't done too much customization of the checkout process.
There's a terse explaination of how to do this on the Wiki (for a different event)
Once you get your event setup and responding, do a
$event = $observer->getEvent();
var_dump($event->getData());
to see what kind of information you have available. Chances are there's an order object in there which will let you get the ID you're after.
I had to use...
$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
While in the success.phtml template. Instead of load() I used loadByIncrementId - then my order object was no longer empty.
If you are in admin mode - try this:
$orderModel = Mage::getModel('sales/order');
$orders = $orderModel->getCollection()->setOrder('increment_id', 'DESC')->setPageSize(1)->setCurPage(1);
$orderId = $orders->getFirstItem()->getIncrementId();
getRealOrderId() appears to return the order number as presented in data grids. getId() will return the internal id of row in the database, which you probably don't want.
You can get the increment id using this code snippet:
$orderId = 12;
$order = Mage::getModel('sales/order')->load($orderId);
$Incrementid = $order->getIncrementId();
Now you can do an echo to the $Incrementid variable and see the increment id.
I hope this helps.
$lastOrderIncrementId = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
$shipmentID = $shipment->increment_id;
$order = $shipment->getOrder();
$orderID = $order->increment_id;