woocommerce delete all variations using REST API - woocommerce

I want to delete all variations using REST API without passing variation ID. I didn't see any instructions in wooCommerce REST API doc.

I solved it by using an alternative method. The two requests created in this method. First, to call the product than pass the response variation ID's to another batch request, It will delete all the variations but still there is a limit of maximum 100 variation ID's. It won't delete more than 100 variations in a single request.
If anyone knows the better method, please share.
WooCommerce.get("products/794")
.then((response) => {
WooCommerce.post("products/"+response.data.id+"/variations/batch", response.data.variations)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error.response.data);
});
})
.catch((error) => {
console.log(error.response.data);
});

Related

wordpress-woocommerce rest api is so slow

I'm trying to create 100k products in woocommerce through woocommerce rest api.
But it takes too long to get response from woocommerce. It takes almost 3 minutes per 100 data.
It's normal situation? or Is there any way to solve this issue?
help me please. Thank you in advance.
const CreateToWP = function (data) {
let data_batch = {
create: []
};
data_batch['create'].push(data)
return WooCommerce.post("products/batch", data_batch)
.then((res) => {
console.log(res.data.create.length)
})

Paypal recurring Smart Payment Button

Is it possible with the Smart Payment Button for Paypal recurring payments to pass additional parameters like an invoice id for example.
paypal.Buttons({
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-2UF78835G6983425GLSM44AM'
});
},
onApprove: function(data, actions) {
alert('You have successfully created subscription ' + data.subscriptionID);
}
}).render('#paypal-button-container');
This is the example code from https://developer.paypal.com/docs/subscriptions/integrate/#4-create-a-subscription
and it says:
Calls PayPal using actions.subscription.create() to create a subscription for your plan and includes the plan ID, subscriber details, shipping, and other details.
But I can't pass anything besides the plan_id.
You can add the custom_id field as explained on this page
In that case, your code would look like
return actions.subscription.create({
'custom_id': 'my-custom-code-for-integration',
'plan_id': 'P-2UF78835G6983425GLSM44AM'
});
The underlying API is /v1/billing/subscriptions
Here is the reference: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create
It does not appear to have a field for an invoice_id -- so, the answer to your question is No, it is not possible.
You will get a unique PayPal subscription profile ID in the resulting response, e.g. "id": "I-BW452GLLEP1G" -- So, what you can do is store that profile ID in your own invoice record.
Example of a createSubscription function that calls a server that can check for pre-existing active duplicates before creating the subscription (or return an error otherwise)
createSubscription: function(data, actions) {
return fetch('/path/on/your/server/paypal/subscription/create/', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(serverData) {
console.log(serverData);
return serverData.id;
});
},

Vuex - data from different modules are returned in random order when using then()

In one of my vuex modules I'm loading data 3 times step by step with 3 different API requests using then():
actions: {
loadRoamingHistory: function loadRoamingHistory(context, roamingFilter): Promise<Array<RoamingHistoryEvent>> {
return new Promise((resolve) => {
store.dispatch('network/loadNetworks').then(() => {
store.dispatch('country/loadCountries').then(() => {
providerApi.loadRoamingHistory(roamingFilter).then(data => {
// read already loaded networks and countries from store
let networks = context.rootState.network.networks;
let countries = context.rootState.country.countries;
// .. some data processing using informations from
// networks and countries request, that are not allways available at this point..
console.log('data processing');
commitSetRoamingHistoryEvents(context, data.roamingHistoryEvent);
resolve();
}).catch(err => console.log(err));
});
});
});
}
}
I also added console.log() commands to the network and country vuex setters in order to see what is executed first:
mutations: {
setNetworks: function setNetworks(state: NetworkState, networks: Array<Network>) {
console.log('networks loaded');
state.networks = networks;
},
I would expect the 3 requests to be executed one by one, but the log messages shows that sometimes it is executed in different order, for example log messages goes like this:
networks loaded
countries loaded
networks loaded
data processing
countries loaded
Notice that data processing should be last log, otherwise I cannot process the data correctly. Why it is executed in random order and what could be done in order to fix it?
first of all, I need to correct myself, dispatch is an action, it is asynchronous, so you're correct to use promises with them. (I'm used to mapping actions, so I don't see them much)
anyway, the point of promises was to alleviate the "callback hell". so if your structure is nested like this:
action
action
action
action
you're defeating the point of using a promise in the first place.
Instead the point is to chain them in a readable fashion like so
action
action
action
action
actions: {
loadRoamingHistory: function loadRoamingHistory(context, roamingFilter): Promise<Array<RoamingHistoryEvent>> {
return store.dispatch('network/loadNetworks')
.then(() => {
return store.dispatch('country/loadCountries')
})
.then(() => {
return providerApi.loadRoamingHistory(roamingFilter)
})
.then(data => {
// read already loaded networks and countries from store
let networks = context.rootState.network.networks;
let countries = context.rootState.country.countries;
// .. some data processing using informations from
// networks and countries request, that are not allways available at this point..
console.log('data processing');
return commitSetRoamingHistoryEvents(context, data.roamingHistoryEvent);
})
.catch(err => console.log(err));
}
}
note that...
- the initial promise is not defined. Because the dispatch is asynchronous, it already creates a promise, we just add additional calls to it.
- when returning a promise inside a promise, the next then() will handle it, whether it is withing this function or outside of it
- your catch at the end will log an error anywhere along the chain of promises

EmberFire: Getting property generated by Cloud Function when saving record completes

I use a Cloud Function to generate a short unique URL on a record on the 'onWrite' event, and save it. This works well, but when I save a record from my Ember app using EmberFire, I do get a model back as an argument to a callback, but the URL of this model is undefined. Is there a way to return this back to the client? Or do I need to query the record to get the generated URL?
This is how my Cloud Function code looks:
exports.generateUrl = functions.database.ref('/tournaments/{tid}')
.onWrite(event => {
if (event.data.previous.exists()) {
return;
}
if (!event.data.exists()) {
return;
}
const url = shortid.generate();
return event.data.ref.update({ url });
});
Here is my component that saves data through form submission. I'm using an add-on called ember-changeset to handle some validations, but this shouldn't be related to the issue.
export default Ember.Component.extend({
submit(e) {
e.preventDefault();
let snapshot = this.changeset.snapshot();
return this.changeset
.cast(Object.keys(this.get('schema')))
.validate()
.then(() => {
if (this.changeset.get('isValid')) {
return this.changeset
.save()
.then((result) => {
// Here, result.get('url') is undefined.
})
}
})
}
});
If you have a function that writes new data back to a location in the database after a write, you'll have to keep listening to that location on the client in order to get that data back. Don't use a one-time read (once()), use a persistent listener (on()), and in that listener, make sure you're getting the URL or whatever you expect to be generated by the function. Then remove that listener if you don't need it any more.
(Sorry, I don't know Ember or what abstractions it provides around Realtime Database - I'm giving you the plain JavaScript API methods you'd use on a reference.)

How to handle multiple action types in one epic? Any cons of doing the same?

Pretty new to redux-observables, rxjs and observables. Wanted to know how can I handle another action, say 'ActionTwo' in the same epic
const Epic1 = (action$,store) => {
return action$.ofType('ActionOne')
.mergeMap((action) => {
return ajax({'method': 'GET', 'url': 'someUrl')
.map(response => resultActoin(action.userId, response.response));
}
);
}
Something like
const Epic1 = (action$){
if('ActionOne') make a API call.
if('ActionTwo') make some other API call.
else do nothing.
}
Is it the same API call? If so, ofType() accepts more than one type. You can just do action$.ofType('ActionOne', 'ActionTwo').
If you want to make a request to another API/URL, I would recommend to make another epic. You can "merge" all you epics with combineEpics see: https://redux-observable.js.org/docs/basics/SettingUpTheMiddleware.html

Resources