Overriding WooCommerce WC_AJAX::get_variation function - wordpress

I have an API, that gives products from third party ecommerce store. This API also gives product variations, but don't return the price.
Can I override WC_AJAX::get_variation somehow, or add filter or action etc, so when user selects the variation attributes, I can call the API there to fetch the updated price.

Related

WooCommerce REST API : filter products by many attributes

i want a filter wooocommerce products by many attributes in rest api
I have this call back function written by https://stackoverflow.com/users/5173735/mustra
please can you write all custom endpoint and give it
products?category=17&attribute=pa_color&attribute_term=22&attribute=pa_size&attribute_term=24&
this works only for 1 attribute

Is it possible to pass a special query parameter value to a WooCommerce product link?

I am looking to use WooCommerce in a bit of a strange way, and I'm wondering if there is any way to make this possible. Here's my desired workflow:
Step #1: From a different site subdomain, provide a link to a certain virtual product, but with a query parameter with a unique user id number.
Explanation: The user is at othersite.example.com, and they get a link to buy a product in a WooCommerce store set up with a wordpress site at https://example.com/product/virtual-product
However, this product will be a payment to unlock something on the othersite.example.com site which has its own user and authentication system. (Firebase)
Would it be possible to pass a user id from the othersite.example.com by way of a url query parameter and then have that included in the order info?
ie. From the othersite.example.com someone could be given a link to the product like this https://example.com/product/virtual-product?userid=00000000000000000, with 00000000000000000 being their user id at othersite.example.com.
Then if that userid query value could be included in the order, the following steps should be doable.
Step #2: Have a webhook that fires when the product is purchased, telling a server managing the users for othersite.example.com that the user with userid 00000000000000000 has made a successful purchase of that product.
Is there a way to accept custom values like this to the order? Or is this totally out of the scope of WooCommerce's functionality?
Thanks so much.
I believe that it can be done by using a redirection/link directly to the checkout page, like: "http://yourdomain.com/checkout/?add-to-cart=PRODUCTID&000000" (tested)
In the example, "checkout" stands for the name of your checkout page, "PRODUCTID" stands for the id of desired product, "000000" stands for the user id of redirected person in the other website.
Then you can add a hidden input field to the checkout page (into checkout form, so you will see this value in order) and using the URL you can assign the "000000" (user id) to the value of this field.
I hope that works. If you have any problems with implementation, you can ask me.
- Useful link for hidden input field addition: https://stackoverflow.com/a/42614905/11003615
- Useful link for getting value from current URL using JS: https://web-design-weekly.com/snippets/get-url-with-javascript/
- Useful link for getting value from current URL using PHP/Wordpress Core: https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
- Useful link (contains 4 parts) for adding a custom field to checkout & validating this field & saving this field to order fields & displaying the value of this field in order page: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-7

How to filter the response from woocommerce rest API?

I need to filter the response of woocommmerce Rest API. It gives more than enough unnecessary response while making http requests. For example. When i need to show the products of specific category. I just need product id, image and slug but m getting unnnecesary fields too of 250 lines of heavy response for single product. So , I wanna get only specific fields. There is plugin for wordpress rest api filter but it doesn't work for woocommerce. Any way to solve my problem?
When you build your model , do not create all fields in Java/Kotlin , just add the fields you need and assign those values by the converter , it could be a Gson , or Moshi whatever .

Checking a guest customer's email address before order is processed to prevent them from purchasing multiple sample products

Hopefully there's a woocommerce veteran familiar with the checkout process that can help me with this. My store offers a sample product at a discounted price, but I want to limit this to a one-time order per customer. I've found a solution that works for registered users but I was wondering if there's another way to make this work without forcing guests to set up an account before ordering the product.
What I was thinking was if there's a way to check if the customer's billing email address has ordered this particular product at the checkout stage. So they'd fill out the checkout form and click "Proceed" but before the customer is taken through to the payment gateway their email address would be checked for ordering this particular product before, if so, direct them to a "order failed" page rather than through to the payment gateway.
Thanks in advance!
You have two of ways to achieve this functionality:
You can validate email on checkout page itself. Scan for orders with customer/guest's email address and if found with free product, then validate. This way an unnecessary order will not be recorded in the system.
Here is how you can have custom checkout validation.
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ( $_POST['billing_country'] == "NO" )
if (!$_POST['notes'])
$woocommerce->add_error( __('Please add the required information in "Order notes"') );
}
Alternatively, you can let customer place order and when order is placed, then scan for it if it has already ordered a sample product. And if found, then you can make status of that order as failed. - But this seems a bit illogical.

WooCommerce Rest API get products by slug

New to WooCommerce, and using the REST API to get products which I have successfully done.
Not sure on how to get a single product using the slug as permalink gives full url, where as i only need the end slug so I can have clean urls and not use the id to find a record. I know with the Wordpress rest api plugin you can do this posts-api?filter[name]== and get the post by product slug, rather than use:
/wc-api/v3/products/id
I want to be able to do:
/wc-api/v3/products/slug
You just have to adjust the query slightly /wc-api/v3/products/slug should be /wc-api/v3/products/?slug=your-product-slug.
One important difference is that this will return the slug inside an array, so you will have to select the first object in the array once you have parsed it.

Resources