Update custom fields value in magento cart - php

Need to update custom field value in magento order where order_date is my custom field.
$order2 = Mage::getModel('sales/order')->load(100000013);
It returns following exception Call to a member function getMethodInstance() on a non-object.
Any solution for fix this issue ?

Your order not able to load by magento that's why magento giving you error.
you are passing order increment id in the load function
Load By Increment Id
$id = 100000013;
$order = Mage::getModel('sales/order')->loadByIncrementId($id);
$order->setData('order_date', "2014-12-12");
$order->save();
Load By Order Id(Entity Id)
$id = 13;
$order = Mage::getModel('sales/order')->load($id);
$order->setData('order_date', "2014-12-12");
$order->save();
Please check.

Related

Get Woocommerce last order id from database using php

In Woocommerce, I am trying to get the last order id using the following code:
<?php $order = new WC_Order($post->ID);echo $order->id;//to escape # from order id $order_id = trim(str_replace('#', '', $order->get_order_number())); ?>
But it doesn't work as I am getting a zero value.
The purpose is to add 1 to this last order ID, to get the new usable order ID.
Any help is appreciated.
It seems that you would like to get the next usable POST ID (Order Id), to save it, for example, as new Order in database with some related data.
This is absolutely not the way to do it and you need to think it different... Now you can use one of the 3 following ways:
Using the WordPress dedicated function wp_insert_post() that returns the post ID (Order ID).
Using the Woocommerce dedicated function wc_create_order() that returns the WC_Order Object.
Then from the the order object, you can get the order ID using $order->get_id().
Using the Woocommerce empty WC_Order object instance and the save() method:
// Get an empty instance of the `WC_Order` Object
$order = new WC_Order();
// Save the order to the database
$order->save();
// Get the Order ID
$order_id = $order->get_id();
Addition - Get the last Order ID in Woocommerce:
To get and display the last order ID in woocommerce use a WC_Order_Query in this two simple line:
<?php
$last_order_id = wc_get_orders(array('limit' => 1, 'return' => 'ids')); // Get last Order ID (array)
echo (string) reset($last_order_id); // Displaying last order ID
?>
Tested and works

Using wc_get_product() with a PHP variable for product ID

I'm building out custom landing pages for products in WooCommerce and I'd like to get the product price amongst other things in order to display them on the landing page.
Each landing page has some custom fields which allow the WP Admin to add in content, for the landing page as well as the product ID, which will then be used to generate the product price, checkout URL etc..
I can't get the wc_get_product(); to work with my custom field or a variable built off that. It only works when I use a straight ID. I think there's something I'm not understanding about how variables work within PHP. Here's my code.
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// This line is where the problem is...
$_product = wc_get_product('$courseID');
// If I replace the line above with this line
// $_product = wc_get_product('7217');
// everything works great, but that does not let
// each landing page function based on the custom fields where the user determines
// the product ID they are selling on that landing page.
// Get's the price of the product
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
Update
I get the following error using wc_get_product( $courseID ); or get_product( $courseID );:
Fatal error: Call to a member function get_regular_price() on a non-object in ...
Update related to your recent comment. The 2 ways to explore:
1) Instead of you should try to use to get the product object (avoiding the error):
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Get an instance of the product object
$_product = new WC_Product($courseID);
2) Alternatively if this doesn't work, you should try to use get_post_meta() function to get the product price (or any product meta data) this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Get the product price (from this course ID):
$course_price = get_post_meta($courseID, '_regular_price', true);
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This time you should get displayed the price with one or the other solutions.
Update: May be Also you need to convert $courseID to an integer variable.
Because you need to use your variable $courseID inside wc_get_product() (without the 2 ') function this way:
<?php
//Gets the course ID from the custom field entered by user
$courseID = the_field('course_id');
// Optionally try this (uncommenting)
// $courseID = (int)$courseID;
// Here
$_product = wc_get_product( $courseID );
$course_price = $_product->get_regular_price();
// Output the Course price
?> <span class="coursePrice">$<?php echo $course_price;?></span>
This should work now.
You can try this out :
$courseID = the_field('course_id');
$product = get_product( $courseID );
I figured out the answer after running through the possible solution routes that #LoicTheAztec supplied in his response. None of these worked and so I assumed something else was up.
I use Advanced Custom Fields to add custom fields in the back end and I was using ACF's the_field() in order to create my variable. That is incorrect usage of that function as it's designed to display the field, (it's basically using php's echo). To work with these custom field's you need to use ACf's get_field() which is to use it to store a value, echo a value and interact with a value.
Once I switched to setting my $courseID to this..
$courseID = get_field('course_id');
Everything worked. My code worked, and all #LoicTheAztec's code approaches also worked.

Magento/PHP - How to get custom customer attribute in order

i am using Magento 1.9.0.1 and i am developing a custom extension.
Right now i am stuck at the following thing.
Like this i use to get the customers name from an order:
$CustomerName = $observer->getOrder()->getBillingAddress()->getName();
I have custom customer attribute called sms_on_order_change and i am trying to get the value of it like that:
$SMSOnStatusChange = $observer->getResource()->getAttribute('sms_on_order_change')->getFrontend()->getValue();
But it is not working.
Here is the extension that i am using for the creation of custom customer attributes: http://www.magentocommerce.com/magento-connect/manage-customer-attributes.html
So guys, can you help me out so i can get the value of this custom attribute ?
Thanks in advance!
This should help you out:
//get order from observer
$order = $observer->getOrder();
// get customer id from order
$customer_id = $order->getCustomerId();
// condition only necessary if guest orders are allowed
if ($customer_id)
{
$_customer = Mage::getModel('customer/customer')->load($customer_id);
$sms_on_order_change = $_customer->getData('sms_on_order_change');
}

How to get custom category attribute value on checkout and order page in magento

I have created a custom attribute for my category. I want to get that attribute on my checkout page and order page but the value is not getting after cart page. I have also tried by using session variable but it is overwriting the previous category attribute value if there is more than one product added in cart.
here is my code for session variable written in default.phtml
$id = $_item->getProduct()->getId();
$product = Mage::getModel('Catalog/product')->load($id);
$categorysku = $product->getCategory();
$product->getCategoryIds();
echo $categorysku->getData('category-sku');// required category attribute value
Thanks in advance for any suggestion or solution.
Is it not a better idea to persist the custom category attribute value(s) in the order line?
In that case you create an observer that is triggered on
sales_quote_item_set_product
and it will look like:
class Namespace_Modulename_Model_Observer extends Varien_Object
{
public function salesQuoteItemSetCustomCategoryAttribute($observer)
{
// Your save custom category attribute to order line logic...
return $this;
}
}

Magento module for exporting order info

I'm new to Magento and have some difficulties with writing mdoule that will allow me to export (send to remote API) data from my magento store.
Currently what I have is created observer for "sales_order_save_after" event and what I want do in my observer is to get Order data, make them xml and send to some remote url.
Can anyone help with that? I'm using magento 1.8
you can get data in observer like below function
public function orderExportTxt(Varien_Event_Observer $observer)
{
$order = $observer->getEvent()->getOrder();
print_r($order->getData()); // your user detail.
}
EDIT
To get Product detail from order
<?php $order_id = 2314; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id);
//load order by order id
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
//item detail
echo $item->getItemId(); //product id
echo $item->getSku();
echo $item->getQtyOrdered(); //ordered qty of item
echo $item->getName(); // etc.
} ?>
its depends on your xml format for your third party service in which format it accept your order to be in saved.
below is just example to create xml file.
http://www.php.net//manual/en/domdocument.savexml.php
hope this will sure help you.
Let me know if i could help further.

Categories