Woocommerce Cart not clearing - wordpress

I know there will be this type of questions but i have tried almost all options without luck so i'm asking here.
I have issue that cart is not empty in woocommerce after order completed and also some users just add items to cart then not go to checkout and after somedays come back again they still see old cart.
I think it may be caching issue as i'm using W3-total-cache plugin.
I want to clear cart data after checkout or if user add items to cart and not checkout and after a while come back again for shopping then cart must be empty and should not store old session to cart.
I tried below code but no luck :
function nom_empty_cart_init_login(){
$opt = get_option('nom_empty_cart_browser_close_do_login');
$opt = $opt != 1 ? false : true;
if( $opt ){
session_destroy();
}
}
// destroy session on user logout
function nom_empty_cart_init_logout(){
$opt = get_option('nom_empty_cart_browser_close_do_logout');
$opt = $opt != 1 ? false : true;
if( $opt ){
session_destroy();
}
}
add_action('admin_menu','nom_empty_cart_init_admin_init');
function nom_empty_cart_init_admin_init(){
add_options_page( 'Woocommerce Clear Cart on Browser Closing', 'WC Clear Cart on Browser Close', 'manage_options', 'wc-clear-cart-on-browser-close', 'wc_clear_cart_on_browser_close' );
}
function wc_clear_cart_on_browser_close(){
if( isset( $_REQUEST['save_accconc'] ) and wp_verify_nonce($_REQUEST['wc-clear-cart-on-browser-close-name'],'wc-clear-cart-on-browser-close-action')):
// SAVING THE FORM DATA
// enable wcccobc
if( isset($_REQUEST['enable_wcccobc']) )
update_option('nom_empty_cart_browser_close_enable',1);
// enable wcccobc on login
if( isset($_REQUEST['enable_wcccobc_on_login']) )
update_option('nom_empty_cart_browser_close_do_login',1);
// enable wcccobc on logout
if( isset($_REQUEST['enable_wcccobc_on_logout']) )
update_option('nom_empty_cart_browser_close_do_logout',1);
// SAVING ;) ENDS
endif;
?>
<div class="wrap">
<div class="inside">
<h2>Woocommerce Clear Cart on Browser Closing</h2>
<p>Note: the cart will be empty if the visitor close the whole browser, not just the widow. (will be updated soon)</p>
<form action="<?php admin_url('options-general.php?page=wc-clear-cart-on-browser-close');?>" method="post">
<?php wp_nonce_field('wc-clear-cart-on-browser-close-action','wc-clear-cart-on-browser-close-name')?>
<p>
<input id="enable_wcccobc" type="checkbox" class="checkbox" name="enable_wcccobc" value="1" <?php checked(get_option('nom_empty_cart_browser_close_enable'),'1');?>>
<label for="enable_wcccobc" >Enable clear cart on browser closing</label>
</p>
<p>
<input id="enable_wcccobc_on_login" type="checkbox" class="checkbox" name="enable_wcccobc_on_login" value="1" <?php checked(get_option('nom_empty_cart_browser_close_do_login'),1);?>>
<label for="enable_wcccobc_on_login" >Enable clear cart on on user login</label>
</p>
<p>
<input id="enable_wcccobc_on_logout" type="checkbox" class="checkbox" name="enable_wcccobc_on_logout" value="1" <?php checked(get_option('nom_empty_cart_browser_close_do_logout'),1);?>>
<label for="enable_wcccobc_on_logout">Enable clear cart on user logout</label>
</p>
<p>
<input type="submit" class="button-primary" value="Save" name="save_accconc">
</p>
</form>
</div>
</div>
<?php
}

I'm using method from #shoelaced and it work.
edit on theme's functions.php
add_action( 'woocommerce_payment_complete', 'order_received_empty_cart_action', 10, 1 );
function order_received_empty_cart_action( $order_id ){
WC()->cart->empty_cart();
}

Related

Why is this piece of code making two fields?

The problem is i need a custom field for my buyers to input some dates in a textarea, and when i make the code for it with a plugin or hardcode a function for it, the code makes 2 input fields as shown in this picture/gyazo, i i cant seem to figure out why :/
https://gyazo.com/97e61de41071324491268258d84f8aae
You can see the code in the next gyazo:
https://gyazo.com/e811efd8b0e9c60b361dc52027693797
I have tried everything that i could figure out with my compatebilities
//Custom Product Option
function custom_field_by_category() {
global $product;
if ( is_product_category( 'work-date' ) ) {
return;
}
?>
<div class="custom-date-field">
<label for="custom-date"><?php _e( 'Engraving (10 characters)', 'iconic' ); ?></label>
<input type="text" id="custom-date" name="dato_oensker" placeholder="<?php _e( 'Dato Ønsker', 'iconic' ); ?>" maxlength="10">
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'custom_field_by_category', 10 );

Add exrtra user approve status filed in user profile, visible to admin only and send mail if checkbox is checked at first time in wordpress?

I want to give the functionality to admin to approve user status manually. I was able to give this functionality, but having some issue in this functionality. How can I hide this field from user's profile for user role. i.e. Field should be visible to admin only. If I hide this filed from user profile using jQuery then when user hits update profile button at that time value is updated. I want value to be same as per user selected (set by admin). I also want to send mail to user if checkbox is selected from uncheck to check, but currently it is sending mail every time. How can i solve out this issue?
/** Add user approve field */
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );
function Add_user_fields( $user ) { ?>
<h2 class="user_prmission">User Permission</h3>
<table class="form-table">
<tr class="user-approve_status-wrap">
<th><label for="dropdown">Approve User Permission</label></th>
<td>
<?php
//get dropdown saved value
$checked = (isset($user->artwork_approved) && $user->artwork_approved) ? ' checked="checked"' : '';
?>
<label for="artwork_approved">
<input name="artwork_approved" type="checkbox" id="artwork_approved" value="1" <?php echo $checked; ?>>
<?php _e('Approve Status','AA'); ?>
</label>
</td>
</tr>
</table>
<?php
}
/* Update selected option **/
add_action( 'user_register', 'save_user_fields');
add_action( 'personal_options_update', 'save_user_fields' );
add_action( 'edit_user_profile_update', 'save_user_fields' );
function save_user_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']));
/* Send email to agent to notify about thier account is approved. **/
if (get_user_meta($user_id, 'artwork_approved', true)) {
// sent mail if checkbox is selected from unchecked to checked,and hide this filed from user profile only visible to admin.
}
}
What I understand from your question is, you have added a custom field on user profile which is a checkbox. You want that field to be editable only by admins and not by the users.
If this understanding is correct, you can check the user capabilities of current user while rendering the custom field on user profile page.
That way, if current user has administrator privileges only then the custom field will be rendered. If a non admin user is logged in then the custom field will not be rendered.
Also, in your function to add custom field, you are setting the value of checkbox to '1' irrespective of whether it is checked or not. I guess that is causing the email to be sent every time. You should set the value of checkbox based on your condition.
So your function to add custom field will become as follows.
/** Add user approve field */
add_action( 'user_new_form', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );
add_action( 'show_user_profile', 'Add_user_fields' );
function Add_user_fields( $user ) { ?>
if( !current_user_can( 'manage_options' ) ){
return;
}
<h2 class="user_prmission">User Permission</h3>
<table class="form-table">
<tr class="user-approve_status-wrap">
<th><label for="dropdown">Approve User Permission</label></th>
<td>
<?php
//get dropdown saved value
if( isset($user->artwork_approved) && $user->artwork_approved ){
$checked = 'checked="checked"';
$value = '1';
}else{
$checked = null;
$value = null;
}
?>
<label for="artwork_approved">
<input name="artwork_approved" type="checkbox" id="artwork_approved" value="<?php echo $value; ?>" <?php echo $checked; ?>>
<?php _e('Approve Status','AA'); ?>
</label>
</td>
</tr>
</table>
<?php
}
Hope this helps.

How to get value from user in wordpress custom plugin?

I have created a plugin for wordpress that show a form to users by shortcode, i want submit the form ,get the values and store in defined table that created when my plugin is activated.
Now i don't know where this form should be submit. I define a submenu for plugin in admin panel (and set form action to this submenu slug) to get that value and store in db, but only those how logged in can submit that form.
You can handle any form submission using template_redirect hook if your form is in the frontend. If your form in the backend then you can use admin_init hook
Say, Your form code looks like in front end
<form method="post">
<input type="text" name="input_1"/>
<input type="number" name="input_2"/>
<?php wp_nonce_field( 'name_of_your_nonce_action', 'name_of_your_nonce_field' ) ?>
<input type="submit" name="submit_form" value="Submit" />
</form>
Now in theme functions.php file, you can handle this form like
<?php
add_action( 'template_redirect', 'wp1213_handle_custom_form', 11 );
function wp1213_handle_custom_form() {
if( ! isset( $_POST['submit_form'] ) ) {
return;
}
if( ! wp_verify_nonce( $_POST['name_of_your_nonce_field'], 'name_of_your_nonce_action' ) ) {
return;
}
// Then you can handle all post data ($_POST) and save those data in db
.......
}

Set Wordpress post status to 'Draft' from front end similar to get_delete_post_link

I am using the below code to allow a logged in user to set delete their own posts from the front end. Is there a way to do the same thing but setting the post to 'draft' rather than deleting it completely?
<?php if ($post->post_author == $current_user->ID) { ?>
<p><a onclick="return confirm('Are you SURE you want to delete this?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>
<?php } ?>
use this function wp_update_post(), you can test with this example:
First create a form were you want to users select if the post are going to be a draft
<form action="" method="POST" >
<input type="checkbox" value="ok" name="draft">
<input type="submit" value="Ok">
</form>
Then create a function to save the new state put this in function.php:
function toDraft($pid){
$toDraft = $_POST['draft'];
if($toDraft == 'ok'){
echo "string";
wp_update_post(array('ID' => $pid, 'post_status' => 'draft'));
}
}
Then add this function below the form you create.
toDraft($post->ID);
And test. Read this to know more about update post status

Wordpress: Page template with default fields

WordPress 3.2.1
I'm creating separate page templates, however, there are some page templates that should have default custom fields. That is, if someone created a new page of template type "Contact Us", it should by default have custom fields:
Success Message
Email To
Phone Number
etc.
Right now, the only way I can think of knocking this out is by having the admin add those custom fields to the page, and then fill them in. However, this isn't the best method for basically giving an admin a "turn-key" type of feature, i.e., they create a page with a specific template and just fill in the fields.
Thanks guys!
The following will add 1 extra meta box to your (insert-edit) page.
In it you can add custom fields to save.
It does not switch depending on the selected template.
I would recomend using javascript to show/hide fields to do this. Based on ID of the <p>
If you get the meta box working with ALL possible fields you can show I'm willing to help with the javascript to hide and show based on the template.
GL
<?php
add_action( 'add_meta_boxes', 'carrousel_build' );
function carrousel_build()
{
add_meta_box('carrousel', 'Carrousel opties', 'carrousel_options', 'page',/*show on 'pages'*/ 'normal', 'high');
}
//this will add a meta box with custom fields
function carrousel_options ($post)
{ ?>
<p>
Description
</p>
<p id="field1_container">
<label for="field1">Custom field 1</label>
<br/>
<input type="text" id="field1" name="field1" value="<?php echo get_post_meta($post->ID, 'field1', true) ?>" size="25" />
</p>
<p id="field2_container">
<label for="field2">Custom field 2</label>
<br/>
<input type="text" id="field2" name="field2" value="<?php echo get_post_meta($post->ID, 'field2', true) ?>" size="25" />
</p>
<?php }
//save the values of the meta box
add_action( 'save_post', 'post_save' );
function post_save($post_id)
{
// Check permissions
if (isset ($_POST['post_type']) && 'carrousel' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ))
{
return;
}
if (isset($_POST['field1'])) {
$subtitle = $_POST['field1'];
update_post_meta($post_id, 'field1', $subtitle);
}
if (isset($_POST['field2']))
{
$link = $_POST['field2'];
update_post_meta($post_id, 'field2', $link);
}
}

Resources