Get WooCommerce Order ID from Plugin [duplicate] - woocommerce

This question already has answers here:
Woocommerce - internal server error: Notice: id was called incorrectly
(1 answer)
How to get WooCommerce order details
(6 answers)
Get Order items and WC_Order_Item_Product in WooCommerce 3
(2 answers)
How to change order status in Woocommerce with my custom plugin
(1 answer)
Closed 1 year ago.
I'm trying to create a payment gateway and I need to get the WooCommerce order ID. Problem is the notice I'm getting and to be fair, I am new to the world of WordPress and WooCommerce.
Notice: Notice: ID was called incorrectly. Order properties should not be accessed directly.
This is my code:
public function get_order_details( $post ) {
if ( $post->post_type=="shop_order" ) :
$order = new WC_Order( $post );
if ( $order ) :
I'm later, whenever needed, referencing it like this: <?php echo $order->get_id; ?>
I'm using the latest WP and WC versions. If anyone can point me in the right direction in terms of how to get the Order ID from my plugin, I would really appreciate it.

Related

Modern Events Calendar filtering with variable on frontend

I am using both WooCommerce (WC) and Modern Events Calendar (MEC). I am trying to display a list view of the MEC events on a corresponding WC product page. As there is no connecting between these two instances it will need some creativity to get to work.
The WC product pages won't be changing a lot in the future so I figured I'd use the product ID. Then every event I make in MEC, I tag with the WC product ID. Then on the frontend I have the MEC shortcode (the list view widget) filter it's output on the WC product ID. Sounds simple enough but I have not been able to figure out how I can get it to work.
In my quest to archive this I have created a shortcode in MEC and changed the filter setting to filter by tag with the following variable [prod_id] after I created this shortcode in the functions file:
function product_id_shortcode() {
global $product;
$id = $product->get_id();
return $id;
}
add_shortcode( 'prod_id', 'product_id_shortcode' );
Unfortunately this did not give the expected result, so I assume filtering by [prod_id] is not being accepted. Any help would be greatly appreciated!

WooCommerce - Variant Product Price Filter not firing

Really struggling with this one and have a sneaky suspicion that the WooCommerce filter may have been removed. I have to tweak variation prices based on a condition that worked at about version 4.4 of Woo.
The code no longer runs so for now I am just trying to break it. Below is the code I am running.
function _custom_price_varaints( $price, $product ) {
die('123');
// -- My Logic here
}
add_filter('woocommerce_product_variation_get_regular_price', '_custom_price_varaints', 10, 2 );
add_filter('woocommerce_product_variation_get_price', '_custom_price_varaints', 10, 2 );
I expect on the product category front end template ( and shop top level ) that when it loads a variant product on the site, it will just break. This never happens. If I change the filter to woocommerce_product_get_price then it has the desired effect ( i.e. breaks on a non variant product ). It's like that filter is never being run and if I am not using the correct filter which one should I be using?

Can I check if WooCommerce product is subscription? [duplicate]

This question already has an answer here:
WooCommerce Subscriptions - Check if a product is a subscription product
(1 answer)
Closed 5 years ago.
I found some solutions but non of them seem to work.
<?php if ( get_post_type() == 'product') : ?>
Since both Subscription and Single product are type of product, is there a way to ask which product type they are?
Woo can do!
$product->is_type( $type )

WooCommerce checkout minimum order based on city

I am new to woocommerce so I need you help to findout the issue i am trying to solve from last 3 days. In short, I have a pizza store at 5 locations. There is a minimum order per location. In checkout I made a custom city select option field based on it total bill will be accepted or rejected. I am using checkout manager. Its really confusing.
So my question is how can I do this and achieve this result. I don't want to hard code everything because In future locations might change.
Any solution for this problem. Thanks In advance.
function check_min_order($order_id) {
global $woocommerce;
$order = new WC_Order($order_id);
$total = $order->calculate_totals();
if (!$total > 'YOUR MINIMUM AMOUNT HERE') {
wc_add_notice( 'You did not meet the minimum order total.', 'error' );
exit;
}
}
add_action( 'woocommerce_checkout_order_processed', 'check_min_order', 1, 1 );
Add this to your functions.php
I might have the $total variable being set using the wrong command. But this should give you a general idea of how to acheive this. It interupts the order process and checks the total to see if it meets your minimum amount. You can create a series of if statements or a switch statement for each case in your form fields. Whatever the name of the request object for the city is you can use $_REQUEST['your_city_field_name'] to get the value.
If the condition is met then it will return a woocommerce formatted error and stop the processing of the transaction. If not it will continue as normal.

Woocommerce get product subtotal in cart page

I am trying to implement ajax functionality to my cart page for updating quantity. So far so good. But I am stuck at one point - updating the product subtotal. I was trying to use this : get_product_subtotal($_product, $quantity), but could not succeed. Because the 1st argument ($_product) is an object. I don't know how to get that. I can get product id and quantity which I passed with ajax call. Now I need the project object with the id. Can anyone help with solution or suggestion?
You can create product object with the following code
$product = new WC_Product( $product_id );

Resources