i was working in a wordpress registration plugin. i stucked in expiry of the user. actually i want to expire the member after one year of his/her registration. and i want to notify them via email before 1 month of their expiry. i am using add_action('init','my function name') to check how many of the user is going to expire after a month and also to send the mail. bt this action hook will run every time a user visits the site which will make my site too slow to load everytime a user will visit. so i want something dat will make this code run once in a day. e.g. when the first user visit the site this code will run and for the whole remaining day this code will not be invoke no matter how many user will visit the website.
Wordpress has a built-in function/API that just do exactly what you want - doing something every day/hour/any interval you specify.
http://codex.wordpress.org/Function_Reference/wp_schedule_event
Taken shamelessly from the above page
add_action( 'wp', 'prefix_setup_schedule' );
/**
* On an early action hook, check if the hook is scheduled - if not, schedule it.
*/
function prefix_setup_schedule() {
if ( ! wp_next_scheduled( 'prefix_daily_event' ) ) {
wp_schedule_event( time(), 'daily', 'prefix_daily_event');
}
}
add_action( 'prefix_daily_event', 'prefix_do_this_daily' );
/**
* On the scheduled action hook, run a function.
*/
function prefix_do_this_daily() {
// check every user and see if their account is expiring, if yes, send your email.
}
prefix_ is presumably to ensure there will be no collision with other plugins, so I suggest you to change this to something unique.
See http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/ if you want to know more.
Related
I know that to reduce the days and eliminate the records, this is done
function wca_reduce_action_scheduler_retention() {
return WEEK_IN_SECONDS;
}
add_filter( 'action_scheduler_retention_period', 'wca_reduce_action_scheduler_retention' );
However what I need is to discard the logs of the group rocket-preload
To be more precise it skips saving logs for that group. Since I don't need them.
This is possible?
This on the Wordpress Scheduled Actions page (/wp-admin/tools.php?page=action-scheduler)
I'm having troubles with applying coupons. The code below work except when the user visits the site for the first time and nothing has been added to the cart yet. I need to clear my cache in order to reproduce this error.
function process_url()
{
if( empty($_GET['coupon']) )
return;
// Add the discount.
global $woocommerce;
$result = $woocommerce->cart->add_discount($_GET['coupon']);
}
add_action('init', 'process_url');
Am I doing something wrong?
Apparently the user session is not stored if you apply a coupon. You need to have first added a product or do something else which stores your session. We can do it manually though, namely by creating the session cookie ourselves.
if( !WC()->session->has_session() )
WC()->session->set_customer_session_cookie(true);
Took me all day. Hopefully someone will be able use it.
function cronstarter_activation() {
if( !wp_next_scheduled( 'mycronjob' ) ) {
wp_schedule_event( strtotime( '00:02:00' ), 'daily', 'mycronjob' );
}
}
add_action('wp', 'cronstarter_activation');
function my_repeat_function() {
//My code the same information to database
}
add_action ('mycronjob', 'my_repeat_function');
Everything works great.
My problem is that I have php code in sidebar to recieve the storaged information from the database that saved from cron_job,
and the first time that a visitor visits the site in the new day after the 00:02:00 the recieved information is the previous day information.
I think that the first visitor of day, first see the sidebar's result, which is the previous day information, and after activate the cronjob.
Is that true? How can I run the cronjob without needed the first visitor?
Yes WP Cron have big caveat is that it requires someone to visit the site in order to run. For example, if you schedule your job to run at 2AM but no one visits at 2AM, it won’t run at 2AM. It will run when someone visits the site after 2AM.
More info Official : https://codex.wordpress.org/Function_Reference/wp_schedule_event
More info public blog:
http://brianshim.com/webtricks/cron-job-wordpress/
For more control on cron job use schedule at system level:
Use PHP to create, edit and delete crontab jobs?
Setup/configuration will differ based on your server & hosting type.
I'm developing a plugin with Woocommerce and Woosensei
I can't seem to get the process working of creating an order programmatically, mark the order completed and activate the woosensei course for that particular person.
// create a new checkout instance and order id
$checkout = new WC_Checkout();
$this_order_id = $checkout->create_order();
// add some data to the order here //
[ ......... ]
// execute order
$order = new WC_Order($this_order_id);
$order->update_status('completed');
$learning = new WooThemes_Sensei();
$learning->sensei_woocommerce_complete_order($this_order_id);
$cart = new WC_Cart();
$cart->empty_cart();
The order is marked as complete indeed, but the user doesn't have access to the course.
If I manually change the order back to in processing and after that to completed (in the WP admin), then the course is assigned to the user.
This is really annoying, hope somebody has some cues (Woo is poorly documented ....)
Thx in advance!
Hmm, it seems like the sensei_woocommerce_complete_order function checks whether a user is logged in or not.
So I duplicated that function and removed the 'logged in' check, and called that specific function when the user is not logged in.
End to the grey hairs ... ;-)
I have an ExpressCheckout up and running. Now I want to give the user the chance to make the payment at a later time. For example he closes the paypal process I keep data saved.
I guess this must be pretty straightforward but somehow it wont redirect me to paypal anymore. The only thing I did so far is to use the identifier to find an existing model instead of creating a new one. This part works so it'll find the specific record of the PaypalExpressPaymentDetails table. But as I said it wont redirect to paypal. Here is the code:
$paymentName = 'demo_paypal';
$storage = $this->get('payum')->getStorageForClass(
'Demo\UserBundle\Entity\PaypalExpressPaymentDetails',
$paymentName
);
/** #var $paymentDetails PaymentDetails */
$paymentDetails = $storage->createModel();
$paymentDetails->setPaymentrequestCurrencycode(0, $currency);
$paymentDetails->setPaymentrequestAmt(0, $amount);
$storage->updateModel($paymentDetails);
$captureToken = $this->getTokenFactory()->createCaptureToken(
$paymentName,
$paymentDetails,
'payments_transaction_success'
);
$paymentDetails->setReturnurl($captureToken->getTargetUrl());
$paymentDetails->setCancelurl($captureToken->getTargetUrl());
$paymentDetails->setInvnum($paymentDetails->getId());
$storage->updateModel($paymentDetails);
Executed Controller:
...
$identificator = new Identificator($entity->getId(), 'Demo\UserBundle\Entity\PaypalExpressPaymentDetails');
$captureToken = $this->payLateByPaypal($entity->getAmount(), "USD", $entity->getId(), $identificator);
return $this->redirect($captureToken->getTargetUrl());
Any ideas?
I've reproduce it in the sandbox. Unfortunately it is a known issue. If the user logged in and access the page second time the payment considered as canceled. That's done this way to avoid endless redirection between your site and paypal one. I dont see an easy way to solve it.
UPDATE
In version 1.0 the cancel issue is handled correctly.