I have added the title to all the pages of the account using the following code:
add_filter( 'the_title', 'wc_page_endpoint_title' );
the_title( '<h2>', '</h2>' );
Path: plugins/woocommerce/templates/myaccount/my-account.php
<?php
/**
* My Account page
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-account.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.5.0
*/
defined( 'ABSPATH' ) || exit;
/**
* My Account navigation.
*
* #since 2.6.0
*/
do_action( 'woocommerce_account_navigation' ); ?>
<div class="woocommerce-MyAccount-content">
<?php
add_filter( 'the_title', 'wc_page_endpoint_title' );
the_title( '<h2>', '</h2>' );
?>
<?php
/**
* My Account content.
*
* #since 2.6.0
*/
do_action( 'woocommerce_account_content' );
?>
</div>
I also changed the url of the view-order page using the following url :
Change "view-order/order-id" url/endpoint in WooCommerce My account - orders to "orders/order-id"
Title Before the change url view-order page : order # (order-id)
Title after the change url view-order page : orders
I want it to be the same as before
The following code does not work either:
function filter_woocommerce_endpoint_view_order_title( $title, $endpoint, $action ) {
$title = __( 'test', 'woocommerce' );
return $title;
}
add_filter( 'woocommerce_endpoint_view-order_title', 'filter_woocommerce_endpoint_view_order_title', 10, 3 );
With the changes you made, you also changed the endpoint. To make this run smoothly for that specific change, you must:
Replace:
<?php
add_filter( 'the_title', 'wc_page_endpoint_title' );
the_title( '<h2>', '</h2>' );
?>
With:
<?php
function filter_the_title( $title, $id ) {
global $wp;
if ( isset( $wp->query_vars['orders'] ) && is_numeric( $wp->query_vars['orders'] ) ) {
$order = wc_get_order( $wp->query_vars['orders'] );
/* translators: %s: order number */
$title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
} else {
$title = wc_page_endpoint_title( $title );
}
return $title;
}
add_filter( 'the_title', 'filter_the_title', 10, 2 );
the_title( '<h2>', '</h2>' );
?>
Related: Change "view-order/order-id" url/endpoint in WooCommerce My account - orders to "orders/order-id"
Related
What i'm trying to do is to prettify standard woocommerce filter urls.
I mean change this filter url sitename.com/shop/?color=blue to this sitename.com/shop/color-blue/
I've added rewrite rules:
add_action('init', 'filter_rewrite');
function filter_rewrite() {
$shop_page_id = get_option( 'woocommerce_shop_page_id' );
$page_data = get_post( $shop_page_id );
if( ! is_object($page_data) ) {
return;
}
add_rewrite_rule(
$page_data->post_name . '/color-([^/]+)/?$',
'index.php?pagename=' . $page_data->post_name . '&color=$matches[1]',
'top'
);
}
and then updated permalinks.
But when I open page sitename.com/shop/color-blue/ there are no products and some errors in Woocommerce product filters widgets like
Notice: Trying to get property 'post_count' of non-object in SITEPATH/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-price-filter.php on line 69
I'm using standard woocommerce archive-product loop to display products:
<?php
if ( woocommerce_product_loop() ) {
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*
* #hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
get_template_part( 'template-parts/part-product-preview' );
}
}
} else {
/**
* Hook: woocommerce_no_products_found.
*
* #hooked wc_no_products_found - 10
*/
?>
Function wc_get_loop_prop( 'total' ) returns 0 while woocommerce_product_loop() returns 1
Page sitename.com/shop/ is working fine.
How can I solve this problem?
Thanks in advance.
I'm new on plugin development.
I'm try to create a custom Printable form page in wp-admin to create Customer Postal Address.
very Similar This plugin
when administrator click on "print Address" link , pop-up template.php page with customer address and information for print address
The Problem is :
I get fatal Error when click on print order anchor tag and i can't run any wordpress action on template.php:
Fatal error: Call to undefined function add_action() in C:\xampp\htdocs\wp-content\plugins\address generator\template.php on line 4
<?php
/**
* Plugin Name: Address Generator
* Plugin URI: http://CGTV.ir
* Description:Generate Postal Label for Parcel
* Version: 1.0 or
* Author: Hamed Mayahian
* Author URI: CGTV.ir
* License: A "Slug" license name e.g. GPL12
*/
// ADDING COLUMN TITLES (Here 2 columns)
/*define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
include( MY_PLUGIN_PATH . 'template.php');
*/
require_once(ADDRESS__PLUGIN_DIR .'template.php');
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
function custom_shop_order_column($columns)
{
//add columns
$columns['my-column1'] = __( 'چاپ آدرس','theme_slug');
return $columns;
}
// adding the data for each orders by column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'cbsp_credit_details', 10, 2 );
function cbsp_credit_details( $column )
{
global $post, $woocommerce, $the_order;
$order_id = $the_order->id;
switch ( $column )
{
case 'my-column1' :
$myVarOne = wc_get_order_item_meta( $order_id, '_the_meta_key1', true );
echo $myVarOne;
echo "<a target='_blank' href='".plugins_url( 'template.php' , __FILE__ )."?order=$order_id'>Print Address</a>";
break;
}
}
Template.php
<?php
add_action('init', 'my_init', 1);
function my_init(){
global $post, $woocommerce, $the_order;
$id = $_GET['order'];
$order = new WC_Order($id);
$address = $order->get_billing_address();
$customer_id = get_current_user_id();
if($_GET['order'] == "") {
// no username entered
echo "آدرس پیدا نشد";
} else {
echo "Hello, " . $address;
}
}
?>
Since I don't know what you are trying to accomplish, I can only suggest the following as an improvement in how you are launching your plugin and how you are displaying the custom column.
/**
* Plugin Name: Custom Shop Column Link
* Plugin URI: http://stackoverflow.com/a/39280792/383847
* Description: Link for shop column to display billing address
* Version: 1.0.0
* Author: helgatheviking
* Author URI: http://kathyisawesome.com/
* Text Domain: your-plugin
* Domain Path: /languages
*
* Copyright: © 2015 Kathy Darling and Manos Psychogyiopoulos
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// add all your hooks only when woocommerce has fully loaded it's files
add_action( 'woocommerce_loaded', 'custom_address_generator_init' );
function custom_address_generator_init(){
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
add_action( 'manage_shop_order_posts_custom_column', 'cbsp_credit_details',11);
}
// add your custom column
function custom_shop_order_column($columns)
{
//add columns
$columns['my-column1'] = __( 'چاپ آدرس', 'your-plugin');
return $columns;
}
// adding the data for each orders by column (example)
function cbsp_credit_details( $column )
{
global $the_order;
$order_id = $the_order->id;
switch ( $column )
{
case 'my-column1' :
$myVarOne = get_post_meta( $order_id, '_the_meta_key1', true );
echo $myVarOne;
$url = add_query_arg( array( 'order_id' => $order_id, 'my-action' => 'do-something-cool', ), wp_nonce_url( admin_url(), 'my_order_nonce', 'my_nonce' ) );
printf( '<a class="custom-class" href="%s" data-order_id="%s">%s</a>', $url, $order_id, __( 'Print Address', 'your-plugin' ) );
break;
}
}
EDIT 2 We're going to create a link to the front-end so we can load a custom template via template_include. It should have enough security on it to keep it limited to only the appropriate users.
// load a custom template when special link is clicked
add_action( 'template_include', 'my_template', 1 );
function my_template(){
if( isset( $_GET['my-action'] ) && $_GET['my-action'] == 'do-something-cool' && isset( $_GET['order_id'] ) && current_user_can( 'edit_shop_order', $_GET['order_id'] ) && wp_verify_nonce( $_GET['my_nonce'], 'my_order_nonce' ) ){
return untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/my-template.php';
}
}
Then a /templates/my-plugin.php file in your plugin folder:
<?php
$order_id = intval( $_GET['order_id'] );
$order = wc_get_order($order_id);
if( is_a( $order, 'WC_Order' ) ){
$address = $order->get_formatted_billing_address ();
if( $address ){
printf( '%s, %s', __( 'Hello', 'your-plugin' ), $address );
} else {
_e( 'No billing address', 'your-plugin' );
}
} else {
_e( 'Not a valid order ID', 'your-plugin' );
}
I've dropped the my_init() function in favor of my_template() which will now load a custom template (/templates/my-template.php) via the template_include filter. This template is loaded by WordPress and has all the WordPress functions available for you to use.
template.php file is out of Wordpress then we are not access Wordpress core functions. Include this file in main plugin is works properly but when we access by url directly this file then we can't access Wordpress core functions because we are not follow Wordpress stranded. Order list table have button called url generate like something is http://localhost/wp-content/plugins/address%20generator/template.php?order=5147. When we access this get following error "Fatal error: Call to undefined function add_action() in.."
First comment this line in your main plugin file.
// require_once('template.php');
Changes in template.php file.
<?php
require('../../../wp-load.php');
$id = $_GET['order'];
$order = new WC_Order($id);
$address = $order->get_billing_address();
$customer_id = get_current_user_id();
if($_GET['order'] == "") {
// no username entered
echo "آدرس پیدا نشد";
} else {
echo "Hello, " . $address;
}
But this is not a Wordpress stranded solution. User #helgatheviking is provide best solution for this.
Disclosure: I'm not a programmer, or trained in web design. Please forgive me if this is something stupid.
Today I've been trying to fix a problem with overlapping Masonry on my Wordpress.org website: nathanhewitt.net. I found that the likely solution was to use imagesLoaded, a jquery script (imagesloaded.desandro.com). since this isn't my area of expertise, I didn't know how to use a jquery script so I went through many different forums, primarily using this and this as my guides on how to add them to the theme's functions.php file.
This is what I added, toward the top of the code (I'll put the full code below):
function my_scripts_method() {
// register your script location, dependencies and version
wp_register_script('imagesLoaded',
get_template_directory_uri() . '/custom_jquery/imagesloaded.pkgd.min.js',
array('jquery'),
'1.0' );
// enqueue the script
wp_enqueue_script('imagesLoaded');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
As soon as I added it, I began to get a Parse error: syntax error, unexpected end of file in /home/nathanhe/public_html/wp-content/themes/balance2/functions.php on line 359, but no matter how I shift the code around, copying and pasting and changing the order, it always says line 359.
I should mention that the code I added related to imagesLoaded is no where near line 359, and also that I have put the text through an online php checker and it didn't find any errors.
Does anyone have any idea what I've done wrong?
I've checked around for several hours and can't seem to figure this out; hopefully someone out there might also be having the same problem and might run into this forum.
Here's the full code:
<?php
require_once ( get_stylesheet_directory() . '/theme-options.php' );
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', get_bloginfo('stylesheet_directory').'/libs/jquery-1.6.1.min.js' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery_masonry', get_bloginfo('stylesheet_directory').'/libs/jquery.masonry.min.js' );
wp_enqueue_script( 'jquery_ui', get_bloginfo('stylesheet_directory').'/libs/jquery-ui.custom.min.js' );
// javascript for infinite scroll
$imbalance2_theme_options = get_option('imbalance2_theme_options');
if ( $imbalance2_theme_options['navigation'] == 1 )
{
wp_enqueue_script( 'jquery_infinitescroll', get_bloginfo('stylesheet_directory').'/libs/jquery.infinitescroll.min.js' );
}
}
function my_scripts_method() {
// register your script location, dependencies and version
wp_register_script('imagesLoaded',
get_template_directory_uri() . '/custom_jquery/imagesloaded.pkgd.min.js',
array('jquery'),
'1.0' );
// enqueue the script
wp_enqueue_script('imagesLoaded');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
// shortcodes
function imbalance2_wide( $atts, $content = null )
{
return '<div class="wide">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'wide', 'imbalance2_wide' );
function imbalance2_aside( $atts, $content = null )
{
return '<div class="aside">' . do_shortcode($content) . '</div>';
}
add_shortcode( 'aside', 'imbalance2_aside' );
// 210px width images for the grid
if ( function_exists( 'add_theme_support' ) )
{
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 210 );
}
if ( function_exists( 'add_image_size' ) )
{
add_image_size( 'homepage-thumb', 210 );
}
// font output for css
function getFonts()
{
global $imbalance2_theme_options;
if ($imbalance2_theme_options['font'] == 0) return 'Georgia, "Times New Roman", Serif';
return '"Helvetica Neue", Helvetica, Arial, "Sans-Serif"';
}
// favicon for <head>
function getFavicon()
{
global $imbalance2_theme_options;
return '<link rel="shortcut icon" href="'.($imbalance2_theme_options['favicon'] != '' ? $imbalance2_theme_options['favicon'] : get_bloginfo('stylesheet_directory').'/favico.ico').'" />';
}
// color option for css
function getColor()
{
global $imbalance2_theme_options;
return $imbalance2_theme_options['color'] != '' ? $imbalance2_theme_options['color'] : '#f05133';
}
// fluid grid option for css
function fluidGrid()
{
global $imbalance2_theme_options;
return $imbalance2_theme_options['fluid'];
}
// images only option for css
function imagesOnly()
{
global $imbalance2_theme_options;
return $imbalance2_theme_options['images_only'];
}
// google analytics
function imbalance2google()
{
global $imbalance2_theme_options;
return $imbalance2_theme_options['google'];
}
// custom menu
class Imbalance2_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<div class=\"imbalance2_submenu_container\"><ul class=\"sub-menu\"><li><ul class=\"imbalance2_submenu\">\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul></li></ul></div>\n";
}
}
/**
* Functions and definitions
*/
/**
* Set the content width based on the theme's design and stylesheet.
*
* Used to set the width of images and content. Should be equal to the width the theme
* is designed for, generally via the style.css stylesheet.
*/
if ( ! isset( $content_width ) )
$content_width = 720;
/** Tell WordPress to run imbalance2_setup() when the 'after_setup_theme' hook is run. */
add_action( 'after_setup_theme', 'imbalance2_setup' );
if ( ! function_exists( 'imbalance2_setup' ) ):
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* To override imbalance2_setup() in a child theme, add your own imbalance2_setup to your child theme's
* functions.php file.
*
* #uses add_theme_support() To add support for post thumbnails and automatic feed links.
* #uses register_nav_menus() To add support for navigation menus.
* #uses add_custom_background() To add support for a custom background.
* #uses add_editor_style() To style the visual editor.
* #uses load_theme_textdomain() For translation/localization support.
* #uses add_custom_image_header() To add support for a custom header.
* #uses register_default_headers() To register the default custom header images provided with the theme.
* #uses set_post_thumbnail_size() To set a custom post thumbnail size.
*
* #since Twenty Ten 1.0
*/
function imbalance2_setup() {
// This theme styles the visual editor with editor-style.css to match the theme style.
add_editor_style();
// This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
// Make theme available for translation
// Translations can be filed in the /languages/ directory
load_theme_textdomain( 'imbalance2', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
'header-left' => __( 'Header Left Navigation', 'imbalance2' ),
'header-center' => __( 'Header Center Navigation', 'imbalance2' ),
'header-right' => __( 'Header Right Navigation', 'imbalance2' ),
'footer-left' => __( 'Footer Left Navigation', 'imbalance2' ),
'footer-right' => __( 'Footer Right Navigation', 'imbalance2' )
) );
}
endif;
/**
* Sets the post excerpt length to 40 characters.
*
* To override this length in a child theme, remove the filter and add your own
* function tied to the excerpt_length filter hook.
*
* #since Twenty Ten 1.0
* #return int
*/
function imbalance2_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'imbalance2_excerpt_length' );
/**
* Replaces "[...]" (appended to automatically generated excerpts).
*
* To override this in a child theme, remove the filter and add your own
* function tied to the excerpt_more filter hook.
*
* #since Twenty Ten 1.0
* #return string An ellipsis
*/
function imbalance2_auto_excerpt_more( $more ) {
return '';
}
add_filter( 'excerpt_more', 'imbalance2_auto_excerpt_more' );
/**
* Remove inline styles printed when the gallery shortcode is used.
*
* Galleries are styled by the theme in Twenty Ten's style.css. This is just
* a simple filter call that tells WordPress to not use the default styles.
*
* #since Twenty Ten 1.2
*/
add_filter( 'use_default_gallery_style', '__return_false' );
/**
* Deprecated way to remove inline styles printed when the gallery shortcode is used.
*
* This function is no longer needed or used. Use the use_default_gallery_style
* filter instead, as seen above.
*
* #since Twenty Ten 1.0
* #deprecated Deprecated in Twenty Ten 1.2 for WordPress 3.1
*
* #return string The gallery style filter, with the styles themselves removed.
*/
function imbalance2_remove_gallery_css( $css ) {
return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
}
// Backwards compatibility with WordPress 3.0.
if ( version_compare( $GLOBALS['wp_version'], '3.1', '<' ) )
add_filter( 'gallery_style', 'imbalance2_remove_gallery_css' );
if ( ! function_exists( 'imbalance2_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own imbalance2_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*
* #since Twenty Ten 1.0
*/
function imbalance2_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-avatar">
<?php echo get_avatar( $comment, 60 ); ?>
</div>
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'imbalance2' ); ?></em>
<br />
<?php endif; ?>
<div class="comment-author">
<?php printf( __( '%s', 'imbalance2' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div>
<div class="comment-meta commentmetadata">
<?php
/* translators: 1: date, 2: time */
printf( __( '%1$s at %2$s', 'imbalance2' ), get_comment_date(), get_comment_time() ); ?><?php edit_comment_link( __( '(Edit)', 'imbalance2' ), ' ' );
?>
</div><!-- .comment-meta .commentmetadata -->
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
<div class="comment-body"><?php comment_text(); ?></div>
</div><!-- #comment-## -->
<?php
break;
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'imbalance2' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'imbalance2' ), ' ' ); ?></p>
<?php
break;
endswitch;
}
endif;
/**
* Removes the default styles that are packaged with the Recent Comments widget.
*
* To override this in a child theme, remove the filter and optionally add your own
* function tied to the widgets_init action hook.
*
* This function uses a filter (show_recent_comments_widget_style) new in WordPress 3.1
* to remove the default style. Using Twenty Ten 1.2 in WordPress 3.0 will show the styles,
* but they won't have any effect on the widget in default Twenty Ten styling.
*
* #since Twenty Ten 1.0
*/
function imbalance2_remove_recent_comments_style() {
add_filter( 'show_recent_comments_widget_style', '__return_false' );
}
add_action( 'widgets_init', 'imbalance2_remove_recent_comments_style' );
if ( ! function_exists( 'imbalance2_posted_by' ) ) :
function imbalance2_posted_by() {
printf( __( '<span class="meta-sep">By</span> %1$s', 'imbalance2' ),
sprintf( '%3$s',
get_author_posts_url( get_the_author_meta( 'ID' ) ),
sprintf( esc_attr__( 'View all posts by %s', 'imbalance2' ), get_the_author() ),
get_the_author()
)
);
}
endif;
if ( ! function_exists( 'imbalance2_posted_on' ) ) :
function imbalance2_posted_on() {
printf( __( '%1$s', 'imbalance2' ),
sprintf( '<span class="entry-date">%1$s</span>',
get_the_date()
)
);
}
endif;
if ( ! function_exists( 'imbalance2_posted_in' ) ) :
function imbalance2_posted_in() {
if ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
$posted_in = __( '%1$s', 'imbalance2' );
} else {
$posted_in = __( 'Bookmark the permalink.', 'imbalance2' );
}
printf(
$posted_in,
get_the_category_list( ', ' ),
get_permalink(),
the_title_attribute( 'echo=0' )
);
}
endif;
if ( ! function_exists( 'imbalance2_tags' ) ) :
function imbalance2_tags() {
$tag_list = get_the_tag_list( '', ', ' );
if ( $tag_list ) printf(__( '<div class="entry-tags"><span>Tags:</span> %1$s</div>', 'imbalance2' ), $tag_list );
}
endif;
function wpse_custom_header_setup() {
add_theme_support( 'custom-header', apply_filters( 'wpse_header_args', array(
'width' => 1460,
'height' => 220,
) ) );
}
add_action( 'after_setup_theme', 'wpse_custom_header_setup' );
?>
It is the problem of character encoding while saving your file after editing. It may be due to the file editor or Charset setting in FTP you are using to upload. You need to select UTF-8 character set.
To overcome this issue, follow below instructions:
Create a new file in notepad++ and Copy all the content of the editing file(functions.php) and paste it in this new file.
Click on 'Encoding' menu and select 'Encode in UTF-8' to apply UTF-8 character encoding.
Now, save this file as named 'functions.php' and upload it.
If it doesn't work, you can select 'Force in UTF-8' under 'charset' tab in FTP manger(like FileZilla) you are using.
I added this to my functions.php file :
add_filter ('woocommerce_add_to_cart_redirect', 'woo_redirect_to_checkout');
function woo_redirect_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
But now, all the products are re-directing strait to check-out. I would like to have this option only in one product. Is that a way I can add a product ID to that same filer?
Thank you!
You need to get the product when It is just added to cart , then check if for this product you want to redirect the cart page to checkout page . You need to change $desire_product = 'certain_product'; line on below code and It will definitely work.
add_filter( 'woocommerce_add_to_cart_redirect', 'woo_redirect_checkout' );
function woo_redirect_checkout() {
global $woocommerce;
$desire_product = 'certain_product';
//Get product ID
$product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
//Check if current product is subscription
if ( $product_id == $desire_product ){
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
exit;
} else {
$cart_url = $woocommerce->cart->get_cart_url();
return $cart_url;
exit;
}
}
I wrote a little plugin for this, sharing it here. The plugin adds a small checkbox to the product metabox, so you can specify which products should trigger the automatic skip to checkout. Basically using the same woocommerce_add_to_cart_redirect filter as in the other answers, but providing the admin backend option to determine which products trigger the redirection.
<?php
/**
* Plugin Name: Redirect to checkout
* Plugin URI: http://stackoverflow.com/q/32962653/383847
* Description: redirect to checkout for certain products
* Version: 1.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Requires at least: 3.8
* Tested up to: 3.9
*
* Text Domain: kia-redirect-to-checkout
* Domain Path: /languages/
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/*
* Add text inputs to product metabox
*/
function kia_add_to_wc_metabox(){
global $post;
echo '<div class="options_group">';
// Suggested Price
echo woocommerce_wp_checkbox( array(
'id' => '_redirect_to_checkout',
'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' );
/*
* Save extra meta info
*/
function kia_process_wc_meta_box( $post_id, $post ) {
if ( isset( $_POST['_redirect_to_checkout'] ) ) {
update_post_meta( $post_id, '_redirect_to_checkout', 'yes' );
} else {
update_post_meta( $post_id, '_redirect_to_checkout', 'no' );
}
}
add_action( 'woocommerce_process_product_meta', 'kia_process_wc_meta_box', 1, 2 );
/*
* Redirect to checkout
*/
function kia_add_to_cart_redirect( $url ){
// If product is one of our special types
if ( is_numeric( $_REQUEST['add-to-cart'] ) && kia_maybe_redirect_cart( (int) $_REQUEST['add-to-cart'] ) ) {
// Remove default cart message
WC()->clear_messages();
// Redirect to checkout
$url = WC()->cart->get_checkout_url();
}
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect' );
/*
* check if an item has custom field
*/
function kia_maybe_redirect_cart( $product_id ){
if ( 'yes' == get_post_meta( $product_id, '_redirect_to_checkout', true ) ){
return TRUE;
} else {
return false;
}
}
Updating WooCommerce 3.0+
<?php
/**
* Plugin Name: WC Redirect to checkout
* Plugin URI: http://stackoverflow.com/q/32962653/383847
* Description: Redirect to checkout for certain products
* Version: 1.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Requires at least: 3.8
* Tested up to: 3.9
* WC requires at least: 3.1.0
* WC tested up to: 4.0.1
*
* Text Domain: kia-redirect-to-checkout
* Domain Path: /languages/
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Add text inputs to product metabox
*/
function kia_add_to_wc_metabox(){
global $post;
echo '<div class="options_group">';
// Suggested Price
echo woocommerce_wp_checkbox( array(
'id' => '_redirect_to_checkout',
'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' );
/**
* Save extra meta info
*
* #param WC_Product $product
*/
function kia_process_wc_meta_box( $product ) {
if ( isset( $_POST['_redirect_to_checkout'] ) ) {
$product->update_meta_data( '_redirect_to_checkout', 'yes' );
} else {
$product->update_meta_data( '_redirect_to_checkout', 'no' );
}
}
add_action( 'woocommerce_admin_process_product_object', 'kia_process_wc_meta_box' );
/**
* Redirect to checkout
*
* #param WC_Product $product
*/
function kia_add_to_cart_redirect( $url, $product ) {
// If product is one of our special products.
if ( kia_maybe_redirect_cart( $product ) ) {
// Remove default cart message.
wc_clear_notices();
// Redirect to checkout.
$url = wc_get_checkout_url();
}
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect', 10, 2 );
/**
* Check if an item has custom field.
*
* #param WC_Product $product
*/
function kia_maybe_redirect_cart( $product ) {
return wc_string_to_bool( $product instanceof WC_Product && $product->get_meta( '_redirect_to_checkout', true ) );
}
https://gist.github.com/helgatheviking/f76b97d7d19813538e32b8f5f2dae6ec
There are a few action hooks as well that you can use, for eg: woocommerce_add_to_cart which passes the product id to the callback function:
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 10, 2 );
function custom_add_to_cart( $cart_item_key, $product_id ) {
// replace 123 with a valid product id
if( 123 == $product_id ) {
wp_redirect( WC()->cart->get_checkout_url() );
exit;
}
}
Woocommerce has product category pages. They act just like wordpress regular category pages, but for products rather than posts.
In the product category editor there is a description box where you can add text. The text becomes displayed below the category page title when viewing that product category page.
I am trying to find a way to also add content below the products as well. Basically I want to have a 1,000 word article underneath my products on that product category page for SEO purpose.
However, I cannot figure out how to do this.
To display the text on the category page below the products, you need to add this code to the beginning of the file wp-includes/functions.php, before code "mysql2date ($ format, $ date, $ translate = true) {"
add_action( 'init', 'wpm_product_cat_register_meta' );
/**
* Register details product_cat meta.
*
* Register the details metabox for WooCommerce product categories.
*
*/
function wpm_product_cat_register_meta() {
register_meta( 'term', 'details', 'wpm_sanitize_details' );
}
/**
* Sanitize the details custom meta field.
*
* #param string $details The existing details field.
* #return string The sanitized details field
*/
function wpm_sanitize_details( $details ) {
return wp_kses_post( $details );
}
add_action( 'product_cat_add_form_fields', 'wpm_product_cat_add_details_meta' );
/**
* Add a details metabox to the Add New Product Category page.
*
* For adding a details metabox to the WordPress admin when
* creating new product categories in WooCommerce.
*
*/
function wpm_product_cat_add_details_meta() {
wp_nonce_field( basename( __FILE__ ), 'wpm_product_cat_details_nonce' );
?>
<div class="form-field">
<label for="wpm-product-cat-details"><?php esc_html_e( 'Details', 'wpm' ); ?></label>
<textarea name="wpm-product-cat-details" id="wpm-product-cat-details" rows="5" cols="40"></textarea>
<p class="description"><?php esc_html_e( 'Detailed category info to appear below the product list', 'wpm' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_edit_form_fields', 'wpm_product_cat_edit_details_meta' );
/**
* Add a details metabox to the Edit Product Category page.
*
* For adding a details metabox to the WordPress admin when
* editing an existing product category in WooCommerce.
*
* #param object $term The existing term object.
*/
function wpm_product_cat_edit_details_meta( $term ) {
$product_cat_details = get_term_meta( $term->term_id, 'details', true );
if ( ! $product_cat_details ) {
$product_cat_details = '';
}
$settings = array( 'textarea_name' => 'wpm-product-cat-details' );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="wpm-product-cat-details"><?php esc_html_e( 'Details', 'wpm' ); ?></label></th>
<td>
<?php wp_nonce_field( basename( __FILE__ ), 'wpm_product_cat_details_nonce' ); ?>
<?php wp_editor( wpm_sanitize_details( $product_cat_details ), 'product_cat_details', $settings ); ?>
<p class="description"><?php esc_html_e( 'Detailed category info to appear below the product list','wpm' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'create_product_cat', 'wpm_product_cat_details_meta_save' );
add_action( 'edit_product_cat', 'wpm_product_cat_details_meta_save' );
/**
* Save Product Category details meta.
*
* Save the product_cat details meta POSTed from the
* edit product_cat page or the add product_cat page.
*
* #param int $term_id The term ID of the term to update.
*/
function wpm_product_cat_details_meta_save( $term_id ) {
if ( ! isset( $_POST['wpm_product_cat_details_nonce'] ) || ! wp_verify_nonce( $_POST['wpm_product_cat_details_nonce'], basename( __FILE__ ) ) ) {
return;
}
$old_details = get_term_meta( $term_id, 'details', true );
$new_details = isset( $_POST['wpm-product-cat-details'] ) ? $_POST['wpm-product-cat-details'] : '';
if ( $old_details && '' === $new_details ) {
delete_term_meta( $term_id, 'details' );
} else if ( $old_details !== $new_details ) {
update_term_meta(
$term_id,
'details',
wpm_sanitize_details( $new_details )
);
}
}
add_action( 'woocommerce_after_shop_loop', 'wpm_product_cat_display_details_meta' );
/**
* Display details meta on Product Category archives.
*
*/
function wpm_product_cat_display_details_meta() {
if ( ! is_tax( 'product_cat' ) ) {
return;
}
$t_id = get_queried_object()->term_id;
$details = get_term_meta( $t_id, 'details', true );
if ( '' !== $details ) {
?>
<div class="product-cat-details">
<?php echo apply_filters( 'the_content', wp_kses_post( $details ) ); ?>
</div>
<?php
}
}
You will get an additional field in the category properties named "Details". Add your article here and you will get the text underneath the products list on category page. This will work for each category.
There are countless ways to achieve this. One way is to override the archive-product.php template, and include the content there.
The general steps are:
Create a /woocommerce/ directory in your theme root
Copy the archive-product.php template from /plugins/woocommerce/templates/ to your newly created folder.
Edit and save the copied file.
Read more about template overrides in the WooCommerce docs.