How to change url in "View Cart" button in Woocommerce which shows after clicking "Add to cart" button? I have default http://myshop.com/checkout and I want to change url to custom page (with my cart view combined with checkout): http://myshop.com/custom-page
I know that I should change js behavior, but how?
You can filter the checkout url via woocommerce_get_checkout_url
function so_37863005_checkout_url( $url ){
// Force SSL if needed
$scheme = ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) ? 'https' : 'http';
$url = site_url( '/custom-page/', $scheme );
return $url;
}
add_filter( 'woocommerce_get_checkout_url', 'so_37863005_checkout_url', 10, 2 );
This filter works for languages. Polylang and Loco handle the UI display string for the button and this function handles the url.
$currentlanguage = get_bloginfo('language');
add_filter('woocommerce_get_checkout_url', 'my_checkout');
function my_checkout($url) {
global $woocommerce;
if($currentlanguage = "zh-CN"){
return $checkout_url = 'http://example.com/zh/chinese_checkout/';
}
if ($currentlanguage = "en-US"){
return $checkout_url = 'http://example.com/en/english_checkout/';
}
};
function change_view_cart_link( $params, $handle )
{
switch ($handle) {
case 'wc-add-to-cart':
$params['cart_url'] = "http://myshop.com/custom-page";
break;
}
return $params;
}
add_filter( 'woocommerce_get_script_data', 'change_view_cart_link',10,2 );
go to wp-content\plugins\woocommerce\includes\wc-template-functions.php
then edit raw 1429
function woocommerce_widget_shopping_cart_button_view_cart() {
echo '' . esc_html__( 'View cart', 'woocommerce' ) . '';
}
function woocommerce_widget_shopping_cart_button_view_cart() {
echo '' . esc_html__( 'your text', 'woocommerce' ) . '';
}
version woocommerce 3.0.3strong text
Related
WooCommerce has a product option to “allow backorder but inform customer” which shows a notice on the frontend product page. How can I change the text of this notice?
I have seen this helpful post https://storepro.io/learn/how-to-change-out-of-stock-text-in-woocommerce/ sharing how to change "out of stock" text, but that is different from the "backorder allowed" text.
Code snippet for function.php from the above linked page:
add_filter('woocommerce_get_availability_text', 'themeprefix_change_soldout', 10, 2 );
/* Change Sold Out Text to Something Else */
function themeprefix_change_soldout ( $text, $product) {
if ( !$product->is_in_stock() ) {
$text = '<div class="">My custom sold out message.</div>';
}
return $text;
}
You can copy the code from original source code for the backorder validation and rewrite the status text and return it from the function.
Here is the code:
function vh_wc_custom_get_availability_text( $availability, $_product ) {
if ( $_product->managing_stock() && $_product->is_on_backorder( 1 ) ) {
$availability = $_product->backorders_require_notification() ? __( 'Your new text goes here', 'your-child-theme-text-domain' ) : '';
} elseif ( ! $_product->managing_stock() && $_product->is_on_backorder( 1 ) ) {
$availability = __( 'Your new text goes here', 'your-child-theme-text-domain' );
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'vh_wc_custom_get_availability_text', 10, 2 );
You can check this page for more detail on woocommerce_get_availability_text filter hook.
I`m using Contact form Multi Step plugin in wordpress, i have 5 form and everytime I press the previous button it will not redirect to the previous form
Hi guys Everytime I press the previous button it has an error:
Error:
{"code":"rest_no_route","message":"No route was found matching the URL and
request method","data":{"status":404}}
Additional:
And Everytime i submit the thank you message appear
The answer below is an unofficial fix of a bug with Contact Form 7 Multi-Step Forms v2.1 on WordPress v4.7.5.
The plugin does not work properly anymore according to the author's update.
UPDATE: This plugin still works as expected for most people, but it does not for some.
To fix this, you will have to edit some codes of the plugin.
Update this function in wp-content/plugins/contact-form-7-multi-step-module/form-tags/module-back.php.
/**
* Handle the back form shortcode.
*/
function cf7msm_back_shortcode_handler( $tag ) {
if (!class_exists('WPCF7_Shortcode') || !function_exists('wpcf7_form_controls_class'))
return;
$tag = new WPCF7_Shortcode( $tag );
$class = wpcf7_form_controls_class( $tag->type );
$atts = array();
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );
$value = isset( $tag->values[0] ) ? $tag->values[0] : '';
$previous_url = isset( $tag->values[1] ) ? $tag->values[1] : '.';
if ( empty( $value ) ) {
if ( $tag->type == 'previous') {
$value = __( 'Previous', 'contact-form-7-multi-step-module' );
}
else {
//using old version
$value = __( 'Back', 'contact-form-7-multi-step-module' );
}
}
$atts['type'] = 'button';
$atts['value'] = $value;
$atts['href'] = $previous_url;
$atts = wpcf7_format_atts( $atts );
$html = sprintf( '<input %1$s />', $atts );
return $html;
}
And update 1 line in wp-content/plugins/contact-form-7-multi-step-module/resources/cf7msm.js changing
window.location.href = val[step_field.val()];
to
window.location.href = $(this).attr('href');
Finally, you need to change how you insert [previous] tag in contact form. After making the changes above, the [previous] tag works like so.
[previous "Button Label" "URL of previous step"]
Hi WooCommerce ninjas!
I'm developing WooCommerce plugin and every time when I submit form (Save Changes button) on top shows update notice with 'Your settings have been saved.'. How I can hide or change this notice, I'm use woocommerce_show_admin_notice filter but don't work in my plugin class. Below is part of code from my plugin. Any ideas for hook who will be usefull?
Great thanks!
<?php
class name_of_plugin extends WC_Payment_Gateway {
public $error = '';
// Setup our Gateway's id, description and other values
function __construct() {
$this->id = "name_of_plugin";
$this->method_title = __( "Name", 'domain' );
$this->method_description = __( "Gateway Plug-in for WooCommerce", 'domain' );
$this->title = __( "TFS VPOS", 'domain' );
$this->icon = null;
$this->has_fields = false;
$this->init_settings();
$this->init_form_fields();
$this->title = $this->get_option( 'title' );
$this->testurl = 'https://example.com/payment/api';
$this->liveurl = 'https://example.com/payment/api';
// Save settings
if ( is_admin()) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}
// Disable Admin Notice
add_filter( 'woocommerce_show_admin_notice', array( $this, 'shapeSpace_custom_admin_notice' ), 10, 2 );
// add the filter
add_filter( 'woocommerce_add_error', array( $this, 'filter_woocommerce_add_notice_type' ), 10, 1 );
} // End __construct()
// display custom admin notice
public function filter_woocommerce_add_notice_type($true, $notice ) {
// magic happen here...
return $true;
}
I can't see any proper hook that can be used to remove this.
But these 2 seems to work.
My first thought is to reload the page so the settings text will be gone at the time. Something like this.
add_action( 'woocommerce_sections_checkout', 'woocommerce_sections_checkout' );
function woocommerce_sections_checkout() {
if ( isset( $_GET['section'] ) && isset( $_REQUEST['_wpnonce'] ) && ( $_GET['section'] === 'paypal' )
&& wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
WC_Admin_Settings::add_message( __( 'Your settings have been saved!', 'woocommerce' ) );
wp_safe_redirect( wp_get_raw_referer() );
exit();
}
}
Then my second option is if you wanted to change the text, we can use the filter gettext.
add_filter( 'gettext', 'woocommerce_save_settings_text', 20, 3 );
function woocommerce_save_settings_text( $translated_text, $text, $domain ) {
if ( ( $domain == 'woocommerce' ) && isset( $_GET['section'] ) && ( $_GET['section'] === 'paypal' ) ) {
switch ( $translated_text ) {
case 'Your settings have been saved.' :
$translated_text = __( 'Your awesome settings have been saved.', 'woocommerce' );
break;
}
}
return $translated_text;
}
Please do note that this code is just an example and will work for paypal settings. Change everything that is needed.
I am using Wordpress 3.5, I have a custom post (sp_product) with a metabox and some input field. One of those input (sp_title).
I want to Search by the custom post title name by typing in my input (sp_title) field and when i press add button (that also in my custom meta box), It will find that post by that Title name and bring some post meta data into this Meta box and show into other field.
Here in this picture (Example)
Search
Click Button
Get some value by AJAX from a custom post.
Please give me a example code (just simple)
I will search a simple custom post Title,
Click a button
Get the Title of that post (that i search or match) with any other post meta value, By AJAX (jQuery-AJAX).
Please Help me.
I was able to find the lead because one of my plugins uses something similar to Re-attach images.
So, the relevant Javascript function is findPosts.open('action','find_posts').
It doesn't seem well documented, and I could only found two articles about it:
Find Posts Dialog Box
Using Built-in Post Finder in Plugins
Tried to implement both code samples, the modal window opens but dumps a -1 error. And that's because the Ajax call is not passing the check_ajax_referer in the function wp_ajax_find_posts.
So, the following works and it's based on the second article. But it has a security breach that has to be tackled, which is wp_nonce_field --> check_ajax_referer. It is indicated in the code comments.
To open the Post Selector, double click the text field.
The jQuery Select needs to be worked out.
Plugin file
add_action( 'load-post.php', 'enqueue_scripts_so_14416409' );
add_action( 'add_meta_boxes', 'add_custom_box_so_14416409' );
add_action( 'wp_ajax_find_posts', 'replace_default_ajax_so_14416409', 1 );
/* Scripts */
function enqueue_scripts_so_14416409() {
# Enqueue scripts
wp_enqueue_script( 'open-posts-scripts', plugins_url('open-posts.js', __FILE__), array('media', 'wp-ajax-response'), '0.1', true );
# Add the finder dialog box
add_action( 'admin_footer', 'find_posts_div', 99 );
}
/* Meta box create */
function add_custom_box_so_14416409()
{
add_meta_box(
'sectionid_so_14416409',
__( 'Select a Post' ),
'inner_custom_box_so_14416409',
'post'
);
}
/* Meta box content */
function inner_custom_box_so_14416409( $post )
{
?>
<form id="emc2pdc_form" method="post" action="">
<?php wp_nonce_field( 'find-posts', '_ajax_nonce', false); ?>
<input type="text" name="kc-find-post" id="kc-find-post" class="kc-find-post">
</form>
<?php
}
/* Ajax replacement - Verbatim copy from wp_ajax_find_posts() */
function replace_default_ajax_so_14416409()
{
global $wpdb;
// SECURITY BREACH
// check_ajax_referer( '_ajax_nonce' );
$post_types = get_post_types( array( 'public' => true ), 'objects' );
unset( $post_types['attachment'] );
$s = stripslashes( $_POST['ps'] );
$searchand = $search = '';
$args = array(
'post_type' => array_keys( $post_types ),
'post_status' => 'any',
'posts_per_page' => 50,
);
if ( '' !== $s )
$args['s'] = $s;
$posts = get_posts( $args );
if ( ! $posts )
wp_die( __('No items found.') );
$html = '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th class="no-break">'.__('Type').'</th><th class="no-break">'.__('Date').'</th><th class="no-break">'.__('Status').'</th></tr></thead><tbody>';
foreach ( $posts as $post ) {
$title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' );
switch ( $post->post_status ) {
case 'publish' :
case 'private' :
$stat = __('Published');
break;
case 'future' :
$stat = __('Scheduled');
break;
case 'pending' :
$stat = __('Pending Review');
break;
case 'draft' :
$stat = __('Draft');
break;
}
if ( '0000-00-00 00:00:00' == $post->post_date ) {
$time = '';
} else {
/* translators: date format in table columns, see http://php.net/date */
$time = mysql2date(__('Y/m/d'), $post->post_date);
}
$html .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>';
$html .= '<td><label for="found-'.$post->ID.'">' . esc_html( $title ) . '</label></td><td class="no-break">' . esc_html( $post_types[$post->post_type]->labels->singular_name ) . '</td><td class="no-break">'.esc_html( $time ) . '</td><td class="no-break">' . esc_html( $stat ). ' </td></tr>' . "\n\n";
}
$html .= '</tbody></table>';
$x = new WP_Ajax_Response();
$x->add( array(
'data' => $html
));
$x->send();
}
Javascript file open-posts.js
jQuery(document).ready(function($) {
// Find posts
var $findBox = $('#find-posts'),
$found = $('#find-posts-response'),
$findBoxSubmit = $('#find-posts-submit');
// Open
$('input.kc-find-post').live('dblclick', function() {
$findBox.data('kcTarget', $(this));
findPosts.open();
});
// Insert
$findBoxSubmit.click(function(e) {
e.preventDefault();
// Be nice!
if ( !$findBox.data('kcTarget') )
return;
var $selected = $found.find('input:checked');
if ( !$selected.length )
return false;
var $target = $findBox.data('kcTarget'),
current = $target.val(),
current = current === '' ? [] : current.split(','),
newID = $selected.val();
if ( $.inArray(newID, current) < 0 ) {
current.push(newID);
$target.val( current.join(',') );
}
});
// Double click on the radios
$('input[name="found_post_id"]', $findBox).live('dblclick', function() {
$findBoxSubmit.trigger('click');
});
// Close
$( '#find-posts-close' ).click(function() {
$findBox.removeData('kcTarget');
});
});
I'm trying to add a custom default avatar to WordPress in functions.php, but the image is not displaying in Settings/Discussion or elsewhere on the site. The code works because a new radio field is added with the custom field name, but the image won't display. Is the avatar not displaying because I'm using Localhost?
I don't have enough reps to comment on similar questions.
here's the code:
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
$new_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.png';
$avatar_defaults[$new_avatar] = "Default Avatar";
return $avatar_defaults;
}
I've tried other examples and the 'Add-New-Default-Avatar' plugin with the same result.
I was facing the same issue and came up with this completely hackish solution... It works though :)
add_filter( 'get_avatar', 'so_14088040_localhost_avatar', 10, 5 );
function so_14088040_localhost_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
$whitelist = array( 'localhost', '127.0.0.1' );
if( !in_array( $_SERVER['SERVER_ADDR'] , $whitelist ) )
return $avatar;
$doc = new DOMDocument;
$doc->loadHTML( $avatar );
$imgs = $doc->getElementsByTagName('img');
if ( $imgs->length > 0 )
{
$url = urldecode( $imgs->item(0)->getAttribute('src') );
$url2 = explode( 'd=', $url );
$url3 = explode( '&', $url2[1] );
$avatar= "<img src='{$url3[0]}' alt='' class='avatar avatar-64 photo' height='64' width='64' />";
}
return $avatar;
}
Result:
Of course, this filter is meant for development only.