is there any woocommerce API for add To cart [closed] - wordpress

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Is there any woocommerce API for add to cart
So that I can add product to cart via mobile app.
Please help me.

Did not found any "add to cart" functionality in the woocommerce api.
What had been done is that my "add to cart" button will save the product id and its quantity(and other properties) to the shared preferences object.
Firstly getting the products json object from the already shared preferences object and find out that the to-be-added product already is present on the shared preferences object.If it is present already then the execution returns.Otherwise it will advance to add the product property to the shared preferences object.
SharedPreferences pref = getSharedPreferences("CartPref", 0);
String strJson = pref.getString("productCartJson","[]");
JSONArray productsSaveDetailJsonArray = new JSONArray(strJson);
//checking if the product-to-be-added is already present in the shared preferences object
for(int i=0;i<productsSaveDetailJsonArray.length();i++){
if(productsSaveDetailJsonArray.getJSONObject(i).getString("product_id").contentEquals(productIdForDetailsPage)){
strJson = pref.getString("productCartJson","0");
Log.d("strJson",""+strJson);
//if already present then returns.
return;
}
}
JSONObject productSaveDetailJsonObject = new JSONObject();
productSaveDetailJsonObject.put("product_id",""+productIdForDetailsPage);
productSaveDetailJsonObject.put("quantity","1");
productsSaveDetailJsonArray.put(productSaveDetailJsonObject);
SharedPreferences.Editor editor = pref.edit();
editor.putString("productCartJson", ""+productsSaveDetailJsonArray);
editor.apply();
strJson = pref.getString("productCartJson","0");
Log.d("strJson",""+strJson);
If any doubt please comment.
This works for me.Hope it works for you as well.

Please find the following steps to achieve this:
Get the persistent cart. It may be _woocommerce_persistent_cart or _woocommerce_persistent_cart_1; check in the user_meta table.
Create a new cart object.
Add the old cart data to the newly created cart object.
Add the product and quantities coming in from the request to the new cart object.
If there is a current session cart, overwrite it with the new cart.
Update the wp_session table with the updated cart data.
Overwrite the persistent cart with the new cart data.
Please check my answer to the following question with integration of these steps in working code:
Rest API to store products in cart based on the user id in woocommerce

Related

ASP.NET to call my method every hour [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I want to send email remainder. I created a method that will send emails to the users that have an appointment in the next hour. Right now i have to call my method manually. But i want to use HangFire( or if you have a better suggestion) to call my method every hour( 3:00 PM, 4:00 PM, and so on). I don't want to use Windows task scheduler because i won't have access to the server in the future.
//[HttpPost("lll/reminder")]
public IActionResult EventReminder()
{
var date = DateTime.Now;
var events = eventRepository.GetEventsByDateTimeNow();
foreach(Event evnt in events)
{
var usr = userRepository.GetUserById(evnt.AttendeeId);
var message = new MimeMessage();
message.From.Add(new MailboxAddress("", ""));
message.To.Add(new MailboxAddress("User", usr.Email));
message.Subject = "Remainder";
message.Body = new TextPart("html")
{
Text = "message"
};
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("", "");
client.Send(message);
client.Disconnect(true);
}
}
return Ok();
}
This is the method. It's working but i have to call it manually. So, any ideas?
As you've already mentioned, HangFire can help you with this. They have several examples directly on their home page. The one you want is probably the 'recurring job':
Recurring jobs
Recurring jobs fire many times on the specified CRON schedule.
RecurringJob.AddOrUpdate(
() => Console.WriteLine("Recurring!"),
Cron.Daily);
You can setup and configure hangfire a variety of ways, but the most simple is to simply add it to your Startup.cs file.
I'd recommend you follow the quick start guide, since your use case appears fairly straight forward, running the 'server' in the web app is probably good enough for many small sites/apps and use cases.
There are ways to help improve running the server in the web app itself. That said, if you need something extremely robust, setting up the 'server' as a Windows Service or other 'out of process' process is probably time well spent and something hangfire easily supports as well.

woocommerce checkout update shipping value more than once

Woocommerce allows the use of the code below to update the shipping cost.
$('body').trigger('update_checkout', { update_shipping_method: true });
Am using a custom shipping plugin and am able to update the cost through ajax and eventually update my total.
The problem is, the update_checkout can only work when the billing_address_1, billing_city, shipping_city and a few other fields have been changed. So I have to do something like below:
$("#billing_address_1").trigger("keypress").val(function(i,val){return val + ' -';});
$('body').trigger('update_checkout', { update_shipping_method: true });
Is there a better way to achieve this, other than make the form dirty for woocommerce to update the shipping cost?
Thanks in advance!!
This by design of woocommerce. This scripts assumes that update is needed when changing address or country.
jQuery('body').trigger('update_checkout');
/* what this does is update the order review table but what it doesn't do is update shipping costs;
the calculate_shipping function of your shipping class will not be called again;
so if you were like me and you made a shipping method plugin and you had to change costs based on payment method then
this is the only way to ensure it does just that
*/
If you want to make things work add this (to plugin file or functions.php):
function action_woocommerce_checkout_update_order_review($array, $int)
{
WC()->cart->calculate_shipping();
return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);
Quotation from: https://gist.github.com/neamtua/bfdc4521f5a1582f5ad169646f42fcdf
For reson why, read this ticet:
https://github.com/woocommerce/woocommerce/issues/12349

how to delete randomly generated id into sub node in firebase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
{
"likes" : {
"-K_4PuiqVo6BsIsL0tMD" : {//post reference id
"-K_4Pv_JXQHW_LoDNUJY" : {//'like' id(randomely generated)
"id" : "TYS111088",
"photoUrl" : "https://web.smm:8081/Emp_Images/Upload/Emp_Photo/t_TYS111088-c22cbe899f.JPG",
"username" : "Anubhav"
},
"-K_4QP-lGvH9K2cljSIh" : {//'like' id(randomely generated)
"id" : "TYS111088",
"photoUrl" : "https://web.smm:8081/t_TYS111088-c22cbe899f.JPG",
"username" : "Anubhav"
}
}
}
I want to delete particular 'like'id node(with details),on click of dislike button. I am using android app.
We don't know the platform but the answer is very similar
let ref = myRootRef.child("likes")
.child("-K_4Fbj0wBmZf3_l-uXe")
.child("-K_4FdjaJgPoR0EGwexB")
ref.removeValue()
you can also delete data by setting using ref.setValue and setting it to nil.
I think that you have structured your data wrongly. You want to use on("child_added") on "likes". Once you have all childs,
you want to delete a child via user input (e.g. you have a delete button with the referense to the current key)
When storing: ref("likes").push()
When retrieving: ref("likes").on("child_added")
In the data viewer (what you're looking at) you can hover over a node and you'll have the ability to add a new child (green + button) or delete the node (red X button).

Wordpress; if user exists auto change username [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am coding a custom register page for my wordpress site. I am actually hiding the WP registering/member function a bit. This means that people fill in their info, automatically a username is created (combination first + lastname), auto a password is created, and people get logged in automatically. No confirmation e-mail is sent, so people are not aware that they actually created an account and are logged in. Now i have 2 questions:
I was wondering if it would be possible that if a username already exists, people get logged in automatically, for instance by updating all the fields (including password) and log in the user.
If that is not possible (I guess it is not); would it be possible that when the username exists, automatically the username is changed to a combination of: firstname, lastname+number. So: Peter Hanks, Peter Hanks 1, Peter hanks 2 etc. when a username exists?
Hope someone can help me out on this.
Thanks a lot!
You can update the password with wp_set_password
http://codex.wordpress.org/Function_Reference/wp_set_password
After that, you can auto login with wp_signon
http://codex.wordpress.org/Function_Reference/wp_signon
Example (Not tested)
wp_set_password($new_password);
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $new_password;
wp_signon( $creds, false );

Rating system for users and posts (and comments) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 months ago.
Improve this question
I need to implement a message board with rating system. Something similar to stack overflow, but much simpler.
I need to rate both questions / answers and compute the rating for each user.
I'm looking modules in Drupal to implement it. Could you give me some tip ?
thanks
Fivestar, and User Points could be used for the purpose, but you only get something similar to Stack Overflow.
The first module (which requires Voting API) can be used to allow the users to vote, and the second module can be used to transform the votes in points for the users who voted (among other things — the module is not limited to this). To integrate the two modules, there is another module, but I am not sure it's part of "User Points", or User Points Contributed modules.
The problem with Fivestar is that users are allowed to give a vote from 1 to X (I think the maximum vote can be changed), which is different from the voting system used by Stack Overflow, where users can simply report "I like it", or "I don't like it". With Fivestar there would be only positive votes, and nobody would be able to down vote a comment, or a node; it would be possible to lower the average by giving the minimum vote.
Between the modules I listed, there isn't a module that allows to give points to the author of the node / comment; using "Voting API", and "User Points" it would possible to do that, but no module I looked allows to do it (this means that you could probably write a custom module).
If you look at the list of the modules included in the installation profile ArrayShift, you can get an idea of the modules you can use to reach the same purpose.
The list of modules includes
Node comments, which transforms comments in full nodes; with this module, in example, is possible to use a voting module that works only for nodes with comments too.
Voting API.
Vote UP/Down that allows users to up vote, or down vote.
User Points.
ArrayShift Support Modules; it is probable that this module contains code that allows node authors to get points every time a node they created is voted.
In particular, a module that is part of ArrayShift Support Modules (as_tweaks) contains the following code:
/**
* Below, a bunch of simple hook implementations that award userpoints based
* on various events that happen. In theory, Rules module and various other tools
* could be used to do these things, but most of those modules don't have easy
* to export/import configuration data.
*/
// VotingAPI hook. When a user casts a vote on a node, the author should
// get/lose points..
function as_tweaks_votingapi_insert($votes) {
foreach ($votes as $vote) {
if ($vote['content_type'] == 'node' && ($node = node_load($vote['content_id']))) {
// Award the points
userpoints_userpointsapi(array(
'uid' => $node->uid,
'points' => $vote['value'] * 10,
'operation' => 'vote up',
'entity_id' => $node->nid,
'entity_type' => 'node',
));
}
}
}
// VotingAPI hook. When a user casts a vote on a node, the author should
// get/lose points..
function as_tweaks_votingapi_delete($votes) {
foreach ($votes as $vote) {
if ($vote['content_type'] == 'node' && ($node = node_load($vote['content_id']))) {
// Award the points
userpoints_userpointsapi(array(
'uid' => $node->uid,
'points' => $vote['value'] * -10,
'operation' => 'vote up',
'entity_id' => $node->nid,
'entity_type' => 'node',
));
}
}
}
The Drupal Fivestar works great.

Resources