woocommerce hook on page-new.php - forms

I'm using woocommerce and mgates vendor software while adding hooks on the page-new.php page to make instructions for my vendors on the add product page. I'm using
add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
function myprefix_edit_form_after_title() {
echo 'This is my text!';
}
as well as after editor and form advanced
On my add product page I have:
Title
'edit_form_after_title'
Description
'edit_form_after_editor'
Product Short Description
How do I figure out what hook to put between these to sections?
Product Data
How or where would I put these hooks to have them only get them to show up on the Add Products post page and not ever post page?

You can fire your hook selectively using:
add_action( 'load-post-new.php', 'your_callback' );
And then check for the post type before adding the hook:
function your_callback()
{
global $typenow;
if( 'product' != $typenow )
return;
add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
}
Or, another option:
add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
function myprefix_edit_form_after_title()
{
global $pagenow, $typenow;
if( 'post-new.php' != $pagenow || 'product' != $typenow )
return;
echo 'This is my text!';
}

Related

Gravity Form Shortcode Confirmation

Hi I am echoing a shortcode of a gform but I need to check what the confirmation message is programmatically in php to call a function if successful and another if failed. Is it possible?
The confirmation message to be used is stashed in the GFFormDisplay::$submission property when the submission is processed so it can be retrieved when the form shortcode or block is processed on page render. You can access it like so:
$confirmation = rgars( GFFormDisplay::$submission, $form_id . '/confirmation_message' );
Alternatively you could use the gform_confirmation filter to override the confirmation to be used when the submission is being processed, before it is added to the GFFormDisplay::$submission property e.g.
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry ) {
if ( empty( $entry ) || rgar( $entry, 'status' ) === 'spam' ) {
// Return the default confirmation for spam.
return $confirmation;
}
// Check your condition here and replace the $confirmation if needed.
return $confirmation;
}, 11, 3 );

Yii2 Redirect to previous page after update

I have a application where after update user should be redirected to previous page from pagination.
let's say there is a gridview and user is at page 3. Then he update some record at that page. There should be a redirect to index page 3. What if, while user is updating record, before save, he opens another controller/action in new tab. Then ReturnUrl is now that new action and after save the record he is updating, he is redirected to that new url.
I've tried to set in every action "index" Url::remember(); and then in action "update" - return $this->goBack().
Also return $this->redirect(Yii::$app->request->referrer);, but it stays at same page.
There is a way to store every index URL in session, but in large project that means many sessions.
You could provide the returnUrl to the link, say:
Url::to(['update','id'=>$model->url,'returnUrl'=> Yii::$app->request->url]);
Then in your controller, use $this->request->queryParams['returnUrl'] to redirect to the previousUrl.
To take it one step further, to always provide the returnUrl, you could extend the Url Helper class:
namespace app\helpers;
use yii\helpers;
class Url extends yii\helpers\Url
public function toRouteAndReturn($route, array $params = [], $scheme = false) {
$params['returnUrl'] = Yii::$app->request->url;
return parent::toRoute($route,$params,$scheme);
}
You could provide in your main config:
'on afterAction' => function($event) {
if(!Yii::$app->getResponse()->isSent && !empty(Yii::$app->getRequest()->queryParams['returnUrl']) {
Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->queryParams['returnUrl']);
}
}
Then you could use app\helpers\Url::toRouteAndReturn() instead of yii\helpers\Url::toRoute() to have it return to the previous url.
You can try below Solution.
First in your index page, get current page url and encode it.
$current_url=base64_encode(\Yii::$app->request->getUrl());
Append this url with your update link as below.
'urlCreator' => function ($action, $model, $key, $index) use ($current_url) {
if ($action === 'update') {
$url = Yii::$app->request->baseUrl . '/controllerName/update?id=' . $model->id.'&prev='.$current_url;
return $url;
}
// ......
}
In Controller, in Update method decode url as below and use for redirection.
public function actionUpdate($id)
{
$model = $this->findModel($id);
$prev=base64_decode($_REQUEST['prev']);
// ......
return $this->redirect($prev); // you will redirect from where update method is called
// ......
}
Isn't it quite easy to pass page param into your update url (<model/update>) like <model>/update?id=<id>&page=<page>?
in your index.php view, edit your ActionColumn as follow:
[
'class' => 'yii\grid\ActionColumn',
'urlCreator' => function ($action, $model, $key, $index) {
return \yii\helpers\Url::to([$action, 'id' => $model->id, 'page' => Yii::$app->request->getQueryParam('page', null)]);
},
],
As you can see, I'm getting page param from request url and pass it to models' action buttons
And when you click to update model, the page that we entered from is stored/placed in url.
Controller:
public function actionUpdate($id, $page = null)
{
$model = $this->findModel($id);
...
if($model->save()) {
return $this->redirect(['index', 'page' => $page]);
}
...
}
Finally, after we successfully update the model, the action redirects us to previous index page.

Wordpress plugin: Working with REST api

Before I start, I want to tell you guys that I have no experience in anything Wordpress related. I do have worked in PHP and Codeigniter before.
User Case
The user enters a preferred feature in a form (Air conditioning, etc.).
When the user submits the form, the feature will be send to a REST API.
The results of the REST API ( a list of cars with AC ) will be shown on the page.
It should roughly look something like this.
What i have so far
An empty plugin that is shown in the Wordpress admin panel.
Question(s)
How do create the front-end for this plugin?
How and where do I create the form action?
How do I access the form action?
What I have/know so far
I know there are some action hooks that will place your content in the header and footer by creating something like:
<php add_action('wp_footer', 'mp_footer'); ?>
In your empty plugins php file place this:
function get_search_results(){
$args = array( 's' => $_GET['term'] );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>'.get_the_title().'</li>';
}
} else {
echo "Nothing Found";
}
die();
}
add_action( 'wp_ajax_get_search', 'get_search_results' );
add_action( 'wp_ajax_nopriv_get_search', 'get_search_results' );
function get_searchbox( $atts ){
ob_start(); ?>
<form id="searchform">
<input type="text" id="searchbox">
<button type="submit">Search</button>
</form>
<ul id="search-result"></ul>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode( 'searchbox', 'get_searchbox' );
function add_search_script() {
wp_enqueue_script( 'search', plugins_url( '/search.js' , __FILE__ ), array( 'jquery' ) );
wp_localize_script( 'search', 'search_ajax', array( 'url'=>admin_url( 'admin-ajax.php' ), 'action'=>'get_search' ) );
}
add_action( 'wp_enqueue_scripts', 'add_search_script' );
In your plugin's directory create a new javascript file - search.js and place this:
jQuery(function($){
$('#searchform').submit(function(e){
e.preventDefault();
$.ajax({
url: search_ajax.url,
data: { 'action': search_ajax.action, 'term': $('#searchbox').val() }
}).done(function(r) {
$('#search-result').html(r);
});
});
});
Now you can use shortcode [searchbox] in your wordpress page to get your searchbox.
In php you can get same result with <? echo do_shortcode('[searchbox]') ?>
Explanation:
When we add [searchbox] shortcode, it is processed and replaced with a form via get_searchbox() function.
In jQuery code On form submit we are sending an action : get_search (defined in wp_localize_script).
Server receives that request via wp_ajax_get_search and processes get_search_results() and returns an output.
In jQuery done(function(r){ r is the response from server. Use that data to manipulate your html.
action is the essential part of REST api in wordpress. We need not have a url. Instead we define an action and for that action return a response.
Once you understand this, modify the action and response according to your need.
Helpful articles: Daniel's, wpmudev

Wordpress, redirect a user directly to a custom post edit screen after login

currently I got this:
function redirect_companies()
{
if ( current_user_can( 'ca_company' ) )
{
$screen = get_current_screen();
if ( $screen->post_type != 'unternehmen' && $screen->id != 'profile' )
{
global $current_user;
$current_users_posts = get_posts(
array(
'post_type' => 'unternehmen',
'author' => $current_user->ID
)
);
if ( count( $current_users_posts ) > 1 )
{
$redirect = admin_url( 'edit.php?post_type=unternehmen' );
}
else
{
$redirect = get_edit_post_link( $current_users_posts[0]->ID );
}
wp_redirect( $redirect, 301 );
}
}
}
add_action('current_screen', 'redirect_companies');
What it should do: A user with role 'ca_company' logs into wordpress backend and instantly gets redirected to either the overview screen of the custom post type posts of "unternehmen" or, if only one post by this user exists, to the edit screen of that one post.
Also, it should perform this redirect routine, if the user is trying to access any page that is not from post type "unternehmen" and is not the user-profile-edit screen.
I successfully tested this when I already was logged in as auch user and then trying to access for example the dashboard. This works.
But if I completely log out of WP and then log in again, wordpress is performing this:
http://i.stack.imgur.com/M60aJ.png
... and then my browser is telling me, that there is a redirecting error. Infinite redirecting loop. But why? Why does it even go into that "if" where I check for post type "unternehmen". Because if I log in, I am first getting to dashboard...
Hope someone can help :)
Use this action 'add_action('wp_login', 'do_anything');'. And in callback function you can give link to wp_redirect('link') where you want to redirect your screen.

How to change position of woocommerce "added to cart" message

I am using woocommerce version 2.3.13 and I want to change position of woocommerce messages like "Product was successfully added to cart", which appears on the top of the product page when I click on "add to cart" button.
I want this message to appear somewhere else.
I've tired many solutions including this by removing 'woocommerce_show_messages' on 'woocommerce_before_single_product' hook
code: remove_action( 'woocommerce_before_single_product', 'woocommerce_show_messages' );
and then calling woocommerce_show_messages(); function where I want the message to appear but with no luck.
Anyone please help me in this regard.
EDIT:
I've forgot to mention that I am using storefront theme and using custom template for single product.
And these messages are appearing at the top of page by default.
The messages 'Product was successfully added to cart' are woocommerce notices.
These messages are printed by the function
<?php wc_print_notices(); ?>
Try changing the position of this function in template.
To add notices via hooks
add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
STOREFRONT Theme Edit:
** Works with Woocoommerce: 3.1.2 + Storefront: 2.2.5 **
In storefront theme the wc_print_notices is hooked into storefront_content_top
so to remove messages from top in storefront theme you need to add following code in functions.php
remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 ); /*Archive Product*/
remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 ); /*Single Product*/
remove_action( 'storefront_content_top', 'storefront_shop_messages', 1 );
and add function
wc_print_notices();
Wherever you want notices to appear.
EDIT: The messages will not appear on Single Product Page and Archive Product Page. Though they might appear on any other woocommerce page(cart, checkout, etc).
This works for me (WooCommerce 3.2+) (based on this answer for #LoicTheAztec)
add_action( 'wp_head', 'reposition_sf_messages' );
function reposition_sf_messages(){
if( is_product() ) {
remove_action( 'storefront_content_top','storefront_shop_messages',15 );
}
remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 ); /*Single Product*/
add_action('woocommerce_product_meta_end', 'storefront_shop_messages', 1 );
}
If someone is still looking for a solution here it goes.
you can use something like
remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
Now put this code where you want to show the notices.
<?php wc_print_notices(); ?>
I had the same problem. I have woocommerce 6.3.1 installed and nothing worked for me. In the end I started looking at the woocommerce files and found that it is "woocommerce_output_all_notices".
So I went to my functions.php of my child theme and put the following:
//Remove for original position
remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
//Move under cart button (option 1)
add_action ( 'woocommerce_single_product_summary', 'woocommerce_output_all_notices', 35 );
//Move under cart button (option 2)
add_action ( 'woocommerce_single_product_summary', 'mover_mensaje_product_added', 35 );
function mover_mensaje_product_added() {
echo woocommerce_show_messages();
}
//Change text (optional)
add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
$message = '¡Estupendo! Ya tienes el producto en tu carrito :)' ;
return $message;
}
From option 1 and option 2, you should only choose 1. Or you put one or the other. I only give you alternatives that have worked for me, and in my case both options are valid.
Have you tried this:
remove_action( 'woocommerce_before_single_product', 'woocommerce_show_messages' );
add_action( 'woocommerce_after_single_product', 'woocommerce_show_messages', 15 );
Here third parameter is for positioning priority
i think this will help you ............
add_action('woocommerce_before_my_page', 'wc_print_notices', 10);
Now go to your page, where you want the messages displayed, and add
<?php
do_action( 'woocommerce_before_my_page' );
?>
This is the complete code to add in functions.php of the theme.
function move_woocommerce_message(){
remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
add_action( 'woocommerce_single_product_summary', 'wc_print_notices', 35 );
}
add_filter('wp', 'move_woocommerce_message');