Allow customer to remove order items in Woocommerce - php

hello i have this code which is supposed to remove an item from an order
add_action( 'woocommerce_order_item_meta_end', 'display_remove_order_item_button', 10, 3 );
function display_remove_order_item_button( $item_id, $item, $order ){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;
if( isset($_POST["remove_item_$item_id"]) && $_POST["remove_item_$item_id"] == 'Remove this item' ){
wc_delete_order_item( $item_id );
$order->calculate_totals();
}
echo '<form class="cart" method="post" enctype="multipart/form-data" style= "margin-top:12px;">
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Complete Cancellation" />
</form>';
}
But when i press complete cancellation page refresh but nothing is removed & i refresh again nothing is removed
what am i doing wrong?

There are multiple mistakes in your code like:
the condition $_POST["remove_item_$item_id"] == 'Remove this item' is always false.
You need to remove the item using a hook before page start to load, to get the refresh. If not you will not see that the item has been removed and you will need to reload the page once.
So try the following instead:
// Displaying the form fields (buttons and hidden fields)
add_action( 'woocommerce_order_item_meta_end', 'display_remove_order_item_button', 10, 3 );
function display_remove_order_item_button( $item_id, $item, $order ){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) )
return;
echo '<form class="cart item-'.$item_id.'" method="post" style= "margin-top:12px;">
<input type="hidden" name="item_id" value="'.$item_id.'" />
<input type="hidden" name="order_id" value="'.$order->get_id().'" />
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Complete Cancellation" />
</form>';
}
// Processing the request
add_action( 'template_redirect', 'process_remove_order_item' );
function process_remove_order_item(){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) )
return;
if( isset($_POST['item_id']) && isset($_POST['remove_item_'.$_POST['item_id']]) && isset($_POST['order_id'])
&& is_numeric($_POST['order_id']) && get_post_type($_POST['order_id']) === 'shop_order' ) {
// Get the WC_Order Object
$order = wc_get_order( absint($_POST['order_id']) );
// Remove the desired order item
if( is_a($order, 'WC_Order') && is_numeric($_POST['item_id']) ) {
$order->remove_item( absint($_POST['item_id']) );
$order->calculate_totals();
// Optionally display a notice
wc_add_notice( __('Order item removed successfully'), 'success' );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Related

Adding/removing filter from another filter in Wordpress

I'm trying add/remove Wordpress search filter according to the page author of the page I'm currently on. The search bar is in the theme header and therefore independent of the page I am on. I would like to use the author ID of the current page to add a filter for the next search I would do in the search bar.
This is an e-commerce site. The search bar is in the header (independent of the page). I'm on the shop page. I'd like to search products (i.e. posts with the same author ID) related to the shop page I was on before the search action.
I have written the code for functions.php however it doesn't work, i.e "my_search_filter" is not added (nor deleted). What I'm missing?
function search_filter_by_page_author()
{
$author_id = get_the_author_meta('ID');
#echo $author_id;
if (in_array($author_id, array("1", "2"))
{
add_filter( 'pre_get_posts', 'my_search_filter' );
}
else
{
remove_filter( 'pre_get_posts', 'my_search_filter' );
}
}
add_action( 'wp_head', 'search_filter_by_page_author' );
function my_search_filter( $query )
{
if ( $query->is_search && !is_admin())
{
$query->set( 'author', '1, 2' );
}
return $query;
}
Here is an answer that may be what you're after. In my opinion, you should
Set a new searchform.php
Add your pre_get_posts (which by the way is an action, and not a filter)
searchform.php (example) - This goes in your theme or child theme root folder.
<?php
global $post;
$author_id = $post->post_author;?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search" class="searchform search">
<div class="form-group">
<input type="text" class="form-control" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" id="s" placeholder="<?php esc_attr_e( 'Search …', 'text_domain' ); ?>"/>
<input type="hidden" name="author" value="<?php echo $author_id;?>">
</div>
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
</form>
Search filter (put in functions.php)
add_action('pre_get_posts', 'search_filter_by_page_author');
function search_filter_by_page_author($query){
if ($query->is_search && !is_admin() && isset($_GET['author'])) {
$query->set('author', $_GET['author']);
}
return $query;
}
This may not do exactly what you're looking for, but I think you should be able to get a positive result from this as a starting point.

Hide Coupon notice form in Woocommerce checkout page

On my website the checkout page containing "Have a coupon? Click here to enter your code" box. I don't want this field on checkout page.I applying coupon via URl. I want to remove this box from checkout page by CSS. I attached the screenshot of that box in my checkout page.
Is this possible?
I have already tried this:
function hide_coupon_field_on_checkout( $enabled ) {
if ( is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );
But it disable coupons functionality in checkout page.
Any help will be appreciated.
You can use a custom function hooked in woocommerce_before_checkout_form action hook that will remove the notice coupon form without disabling coupon functionality in checkout page:
add_action( 'woocommerce_before_checkout_form', 'remove_checkout_coupon_form', 9 );
function remove_checkout_coupon_form(){
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
This tag seems not having an unique id/class assigned. But something like this should be able to do it:
$(".showcoupon").closest(".woocommerce-info").hide();
You can override the coupon template file at wp-content/themes/yourtheme/woocommerce/checkout/form-coupon.php and comment wc_print_notice and set display of the form to none.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! wc_coupons_enabled() ) {
return;
}
if ( empty( WC()->cart->applied_coupons ) ) {
$info_message = apply_filters( 'woocommerce_checkout_coupon_message', __( 'Have a coupon?', 'woocommerce' ) . ' ' . __( 'Click here to enter your code', 'woocommerce' ) . '' );
//wc_print_notice( $info_message, 'notice' );
}
?>
<form class="checkout_coupon" method="post" style="display:none">
<p class="form-row form-row-first">
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
</p>
<div class="clear"></div>
</form>

Wordpress in foreach loop updating meta values

I have done foreach loop to get custom post type to frontend. And i have one custom field called 'order_staatus'. When i look in front end my list in loop, i want to add one button what would change this certain post 'order_staatus' to different value.. my code..
<? foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div class="profile_order_row">
<?php the_author(); ?> <?php the_title(); ?>
<?php
Global $post;
if ( isset( $_POST['submit'] ) )
{
if( ! isset( $post ) ) {
echo 'Post not set';
die();
}
else if( ! isset( $_POST['staatus'] ) && ! empty( $_POST['staatus'] ) ){
echo 'Error';
die();
}
$postid = $_POST['post_id'];
update_post_meta($postid,'order_staatus','1');
}
$staatus = get_post_meta($post->ID, 'order_staatus', true);
echo print_r($staatus);
?>
<form method="post" action="">
<input type="hidden" name="post_id" value="'.$post->ID.'" />
<input type='text' name='staatus' value='<? echo $staatus ?>' />
<input type='submit' value='save' />
</form>
</div>
<?php endforeach; wp_reset_postdata(); ?>
I don't think the issue is with the function update_post_meta. I think the more likely issue is that $_POST['submit'] is not set. When submitting a form, the value of the submit button is not sent. The value attribute is simply for assigning the text of the button.
I would rewrite the IF block of code like this:
if ( isset( $_POST ) )
{
if( ! isset( $_POST['staatus'] ) && ! empty( $_POST['staatus'] ) ){
echo 'Error';
die();
}
$postid = sanitize_text_field($_POST['post_id']);
update_post_meta($postid,'order_staatus','1');
}
Note, that I removed the isset($post) check because you are running this code inside of foreach ( $postslist as $post ) which defines $post so it will always be set within that loop.
I also added the function sanitize_text_field() to sanitize the post_id. This is an important security measure to avoid SQL injection. All user input, including $_POST, can contain dangerous data and needs to be sanitized before use.
Hope this helps!

Wordpress add_meta_box does not do anything

I can't seem to get the Wordpress add_meta_box to work.
It is adding the boxes in the edit page, but it isn't reflected in the input boxes anywhere.
How can I get this to work? Is there anything I'm doing wrong? I followed this tutorial.
// Add support for header images
if(function_exists('add_theme_support')){
add_theme_support('post-thumbnails'); // Adds this for both pages and posts, For posts/page only, see https://codex.wordpress.org/Function_Reference/add_theme_support
}
// Callback for adding metaboxes
function actionblock_add_meta_box(){
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box('action_block', 'Action Block', 'actionblock_meta_box_callback', $screen, 'normal'); // Add the action block to both post and page screens
}
}
// Add meta boxes for action section on each post/page
add_action('add_meta_boxes', 'actionblock_add_meta_box');
function actionblock_meta_box_callback($post){
// Security check
wp_nonce_field('actionblock_save_meta_box_data', 'actionblock_meta_box_nonce');
// Get values of the action block fields
$action_prompt_text = get_post_meta($post->ID, 'actionblock_meta_prompt_text', true);
$action_button_text = get_post_meta($post->ID, 'actionblock_meta_button_text', true);
$action_button_link = get_post_meta($post->ID, 'actionblock_meta_button_link', true);
// Display and populate fields from the database if it exists
?>
<p>
<label for="action_prompt_text">Action Prompt</label><br>
<input class="widefat" type="text" name="action_prompt_text" id="action_prompt_text" placeholder="This should be short, to the point and less salesy." value="<?php echo esc_attr($action_prompt_text); ?>">
</p>
<p>
<label for="action_button_text">Action Button Text</label><br>
<input class="widefat" type="text" name="action_button_text" id="action_button_text" placeholder="This should prompt the visitor to take an action." value="<?php echo esc_attr($action_button_text); ?>">
</p>
<p>
<label for="action_button_link">Action Button Link</label><br>
<input class="widefat" type="url" name="action_button_link" id="action_button_link" placeholder="Copy and paste the link from the intended page." value="<?php echo esc_attr($action_button_link); ?>">
</p>
<?php
// Callback for saving metaboxes
function actionblock_save_meta_box_data($post_id) {
// Security checks
if(!isset($_POST['actionblock_meta_box_nonce'])) return;
if(!wp_verify_nonce($_POST['actionblock_meta_box_nonce'], 'actionblock_save_meta_box_data')) return;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
// Do the save/update
if(isset($_POST['action_prompt_text'])){
update_post_meta($post_id, 'actionblock_meta_prompt_text', sanitize_text_field($_POST['action_prompt_text']));
}
if(isset($_POST['action_button_text'])){
update_post_meta($post_id, 'actionblock_meta_button_text', sanitize_text_field($_POST['action_button_text']));
}
if(isset($_POST['action_prompt_link'])){
update_post_meta($post_id, 'actionblock_meta_button_link', esc_url($_POST['action_prompt_link']));
}
}
// Save action block details when the post is saved/updated
add_action('save_post', 'actionblock_save_meta_box_data');
Try Below code :
// Add support for header images
if(function_exists('add_theme_support')){
add_theme_support('post-thumbnails'); // Adds this for both pages and posts, For posts/page only, see https://codex.wordpress.org/Function_Reference/add_theme_support
}
// Callback for adding metaboxes
function actionblock_add_meta_box(){
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box('action_block', 'Action Block', 'actionblock_meta_box_callback', $screen, 'normal'); // Add the action block to both post and page screens
}
}
// Add meta boxes for action section on each post/page
add_action('add_meta_boxes', 'actionblock_add_meta_box');
function actionblock_meta_box_callback($post){
// Security check
wp_nonce_field('actionblock_save_meta_box_data', 'actionblock_meta_box_nonce');
// Get values of the action block fields
$action_prompt_text = get_post_meta($post->ID, 'actionblock_meta_prompt_text', true);
$action_button_text = get_post_meta($post->ID, 'actionblock_meta_button_text', true);
$action_button_link = get_post_meta($post->ID, 'actionblock_meta_button_link', true);
// Display and populate fields from the database if it exists
?>
<p>
<label for="action_prompt_text">Action Prompt</label><br>
<input class="widefat" type="text" name="action_prompt_text" id="action_prompt_text" placeholder="This should be short, to the point and less salesy." value="<?php echo esc_attr($action_prompt_text); ?>">
</p>
<p>
<label for="action_button_text">Action Button Text</label><br>
<input class="widefat" type="text" name="action_button_text" id="action_button_text" placeholder="This should prompt the visitor to take an action." value="<?php echo esc_attr($action_button_text); ?>">
</p>
<p>
<label for="action_button_link">Action Button Link</label><br>
<input class="widefat" type="url" name="action_button_link" id="action_button_link" placeholder="Copy and paste the link from the intended page." value="<?php echo esc_attr($action_button_link); ?>">
</p>
<?php
}
// Callback for saving metaboxes
function actionblock_save_meta_box_data($post_id) {
// Security checks
if(!isset($_POST['actionblock_meta_box_nonce'])) return;
if(!wp_verify_nonce($_POST['actionblock_meta_box_nonce'], 'actionblock_save_meta_box_data')) return;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
// Do the save/update
if(isset($_POST['action_prompt_text'])){
update_post_meta($post_id, 'actionblock_meta_prompt_text', sanitize_text_field($_POST['action_prompt_text']));
}
if(isset($_POST['action_button_text'])){
update_post_meta($post_id, 'actionblock_meta_button_text', sanitize_text_field($_POST['action_button_text']));
}
if(isset($_POST['action_button_link'])){
update_post_meta($post_id, 'actionblock_meta_button_link', esc_url($_POST['action_button_link']));
}
}
// Save action block details when the post is saved/updated
add_action('save_post', 'actionblock_save_meta_box_data');

How to change WP redirect on blog search?

I am running a WP Blog.
In permalinks I set WP base URL to https://www.url.com/blog.
That is necessary and can't be changed.
If I try to use the WP-Search Widget for Blog it fails.
Because the URL WP is trying to redirect is wrong
(../blog/blog/?s=test).
Is there a way to say WP to use
../blog/?s=test
instead of
../blog/blog/?s=...
What I tried:
Put in functions.php this code (no success):
function fb_change_search_url_rewrite() {
if ( ! empty( $_GET['s'] ) ) {
wp_redirect( site_url( "/" ) . urlencode( get_query_var( 's' ) ) );
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
.htaccess can't be changed
Ideas are highly appreciated.
Update to precise the question:
When investigating the search with Chrome, it shows that WP redirect the request.
If I only could prevent WP vom redirecting!
Chrome console output:
Remote Address:10.236.1.21:443
Request URL:https://www.domain.com/blog/?s=schnuffi
Request Method:GET
Status Code:302 Found
Response Headers
content-length:0
content-type:text/html; charset=UTF-8
date:Wed, 13 May 2015 08:52:46 GMT
location:https://www.domain.com/blog/blog/?s=schnuffi
server:nginx
status:302 Found
version:HTTP/1.1
....
After removing the following function (I learnt you have implemented in your code):
function change_blog_url() {
if ( is_search() && ! empty( $_GET's' ) )
{ wp_redirect( home_url( "/blog/?s=" ) . urlencode( get_query_var( 's' ) ) ); exit(); }
}
Wordpress should stop adding an additional "/blog" and the URL is resolved properly.
Try to add the is_search() function into your condition. This function checks if search result page archive is being displayed. --> more at Wordpress codex page
And then try to add the exit() function into the end of your function.
So your function may looks like:
function fb_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( site_url( "/" ) . urlencode( get_query_var( 's' ) ) ); exit();
}
}
In theme directory create file searchform.php and put
this code in it:
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
</div>
</form>
Change the form action attribute whatever you want, by default it will always print home url which will be your site url. eg: http://example.com/ so when your form get submitted it will make request to http://example.com?s=query.

Categories