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

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');

Related

flatsome WooCommerce change postcode to dropbox

im new in WordPress i wan to change my WooCommerce check out form postcode to dropdown box.. im using the solution given online but it's not work on my page.. pls help me.
here the code i put inside flatsome child function file.
add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' );
function customize_postcode_fields( $adresses_fields ) {
$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['options'] = array(
'' => __('Select your postcode', 'woocommerce'),
'option_1' => 'Choice 1',
'option_2' => 'Choice 2',
'option_3' => 'Choice 3'
);
return $adresses_fields;
}
and the result that i get after apply the code
my display after apply
I have updated my post from earlier which may make it easier for you to interpret and put into your code (I changed it from my last answer, as what I published wont work) - the main difference is putting the array into a variable, to make it easier to change in the future. I have done some research as well and found that the information you provided was the best option for this (so I just cleaned it up a bit). However, if you could also inspect/view-source on your code
function wpe_0987_customize_postcode_fields( $postcode_field ) {
$options = array(
'' => __( 'Select...', 'woocommerce' ),
'choice_1' => 'choice_1',
'choice_2' => 'choice_2',
'choice_3' => 'choice_3',
'choice_4' => 'choice_4'
);
$fields['billing_postcode']['type'] = 'select';
$fields['shipping_postcode']['type'] = 'select';
$fields['billing_postcode']['options'] = $options;
$fields['shipping_postcode']['options'] = $options;
return $fields;
}
For Shipping Field Only!
add_filter( 'woocommerce_shipping_fields' , 'wpe_0987_customize_postcode_fields' );
For Shipping and Billing Fields
add_filter( 'woocommerce_default_address_fields' , 'wpe_0987_customize_postcode_fields' );
Also, when you create custom functions, don't forget to put a custom prefix at the start. At the moment you have 'customize_postcode_fields', you should find a series of letters/numbers or something unique to you to ensure that it doesn't clash with any other theme/plugin - eg: 'random123_customize_postcode_fields' and use that prefix on all custom functions you create in that project.
Update:
Do an 'inspect' or 'view source' on the page, check to see what the 'name' of the postcode form is, and update it with one of the two that I have provided above (shipping_postcode, billing_postcode).

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

woocommerce hook on page-new.php

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!';
}

How to add menù section to my WordPress template?

I am pretty new in WordPress blog and I am developing a blog with this template:
http://scorejava.com/wordpress351
As you can see at the top of the page there is a "menù" that only show the page in the site (at this moment: "Home" and "Pagina di esempio").
This menù is showed by the following lines of code into the header.php file:
<?php wp_list_pages('title_li=&depth=1'); ?>
So I think that this is not a true menù but only a list of the statics pages present on my blog.
If, in the administrator dashboard I go to the menù section in the "Position of themes" square say me that: "This theme has no support for menus but it is possible use the personalized menu widget to add every created menu in the sidebar"
So I think that my template have no definied a true menù section on the top (but only a list for the static pages). Can I add a true section where add a true menu? How can I do?
Tnx
Andrea
function dasboard_menu() {
global $menu;
$menu[6] = array( __('Orders'), 'read', 'edit.php?post_type=shop_order', '', 'menu-top menu-top-first menu-icon-orders', 'menu-dashboard', 'none' );
$menu[7] = array( __('Catalogue'), 'read', 'edit.php?post_type=product', '', 'menu-top menu-top-first menu-icon-catalogue', 'menu-dashboard', 'none' );
$menu[8] = array( __('Coupons'), 'read', 'edit.php?post_type=shop_coupon', '', 'menu-top menu-top-first menu-icon-coupon', 'menu-dashboard', 'none' );
}
add_action( 'admin_menu', 'dasboard_sub_menu' );
Your starting point is to register your menus in functions.php. Like this:
register_nav_menus(array(
'main_nav'=>__('Main','mythmeme'),
'footer_nav'=>__('Footer','mythmeme'),)
);
It's all in the codex.
You then just need to call the menu in your header.php (or footer.php):
<nav>
<?php wp_nav_menu(
array('theme_location' => 'main_nav')
); ?>
</nav>
Once registered and called you can you use dashboard > appearance > menu to create and add menus to your theme locations.

"add products" button missing from admin create order page - magento

We've just upgraded our site from 1.6.X to 1.7.0.2 and encountered this problem, 99% of the site is running fine.
When you go to sales/orders & create new order the "add products" button is missing? ive checked the styles and there is just a blank div where the button should be -
<div class="form-buttons"></div>
I've tried un-installing extensions, re-installing magento 1.7.0.2 from magento connect & i've also manually downloaded / over written the adminhtml folder, none of which have had any effect.
We also installed a fresh copy of magento with a blank database to the same server & the button is present.
Any ideas?
The changes is in the file located here :
/app/code/core/Mage/Adminhtml/Block/Sales/Order/Create/Items.php around line 55
In previous version the getButtonsHtml() function was :
public function getButtonsHtml()
{
$addButtonData = array(
'label' => Mage::helper('sales')->__('Add Products'),
'onclick' => "order.productGridShow(this)",
'class' => 'add',
);
return $this->getLayout()->createBlock('adminhtml/widget_button')->setData($addButtonData)->toHtml();
}
The new version is :
public function getButtonsHtml()
{
$html = '';
// Make buttons to be rendered in opposite order of addition. This makes "Add products" the last one.
$this->_buttons = array_reverse($this->_buttons);
foreach ($this->_buttons as $buttonData) {
$html .= $this->getLayout()->createBlock('adminhtml/widget_button')->setData($buttonData)->toHtml();
}
return $html;
}
So now you can have more than 1 button, but the first default button doesn't exists anymore.
I'm not sure how to add it without overwritting this block.