Add second single product page in Woocommerce - wordpress

Is it possible to add a second single product page in Woocommerce?
So basically when I am at the single product page I click the "next" button and I get directed to the same single product page with another template. So I just want to retrieve the same data on the second page.
Single product page:
Next page:
And the next page would be the checkout page but that would just be a link to the checkout page so that part would be easy.

What I would do in this case is add a link that will reload the page with a custom query arg in the URL.
Then you can filter the template via template_include to load a different template. Untested, so be careful of syntax typos.
add_filter( 'template_include', 'so_30978278_single_product_alt' );
function so_30978278_single_product_alt( $template ){
if ( is_single() && get_post_type() == 'product' && isset( $_GET['next-step'] ) && intval( $_GET['next-step'] ) == 1 ) {
$template = locate_template( 'single-product-alt.php' );
}
return $template;
}
add_action( 'woocommerce_before_add_to_cart_form', 'so_30978278_additional_template_button' );
function so_30978278_additional_template_button(){
printf( '<a class="button" href="%s">%s</a>', esc_url( add_query_arg( 'next-step', 1 ) ), __( 'Next Step' ) );
}
See add_query_arg for reference.

Related

Add a custom "Buy now" button on WooCommerce single product page which clears the cart, add a product and redirect to checkout

I'm using this simple code to add a 'buy now' button on a single product page.
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
function add_content_after_addtocart() {
// get the current post/product ID
$current_product_id = get_the_ID();
// get the product based on the ID
$product = wc_get_product( $current_product_id );
// get the "Checkout Page" URL
$checkout_url = WC()->cart->get_checkout_url();
// run only on simple products
if( $product->is_type( 'simple' ) ) {
echo 'Buy Now';
}
}
This code effectively redirects to the checkout page and add the product to the cart, but I want to add two little features to it:
After clicking on the button, I want it to clear the cart before taking the action.
After adding the product to the cart, I want it to redirect users to '/checkout' page. Right now it sends users on 'checkout/?add-to-cart=3122', which means that any refresh on the checkout page adds 1 product on the cart automatically.
Any advice?
Instead of using the add-to-cart param in your url, which will cause the product to be added (but also perform other actions in WooCommerce), you can use a custom url for your button and the template_redirect action hook
That way you get rid of the built-in functionality in WooCommerce and you can perform your own custom actions based on the GET parameters
So you get:
// Add new/extra button
function action_woocommerce_after_add_to_cart_button() {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Run only on simple products
if ( $product->is_type( 'simple' ) ) {
// Get product ID
$product_id = $product->get_id();
// Get permalink
$permalink = $product->get_permalink();
// Output url
echo ''. __ ( 'Buy Now', 'woocommerce' ) . '';
}
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10 );
// Redirect
function action_template_redirect() {
// Determines whether the current request is for an administrative interface page
if ( is_admin() ) return;
// Returns true when viewing a single product
if ( ! is_product() ) return;
// Get params
if ( isset( $_GET['product_id'] ) && isset( $_GET['redirect_checkout'] ) ) {
// Get param 1
$product_id = $_GET['product_id'];
// Get param 2
$boolean = $_GET['redirect_checkout'];
// WC Cart
if ( WC()->cart ) {
// 1. Empty cart
WC()->cart->empty_cart();
// 2. Add to cart
WC()->cart->add_to_cart( $product_id );
// 3. Redirect
// When true
if ( $boolean ) {
// Gets the url to the checkout page
$checkout_url = wc_get_checkout_url();
// Performs a safe (local) redirect
wp_safe_redirect( $checkout_url );
exit;
}
}
}
}
add_action( 'template_redirect', 'action_template_redirect' );
Here is code for clear cart before add item
add_filter( 'woocommerce_add_to_cart_validation', 'ji_remove_cart_item_before_add_to_cart', 20, 3 );
function ji_remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( ! WC()->cart->is_empty() )
WC()->cart->empty_cart();
return $passed;}
After adding the product to the cart, I want it to redirect users to '/checkout' page. Right now it sends users on 'checkout/?add-to-cart=3122', which means that any refresh on the checkout page adds 1 product on the cart automatically.
add_filter( 'woocommerce_add_to_cart_redirect', 'ji_redirect_checkout_after_add_to_cart' );
function ji_redirect_checkout_after_add_to_cart() {
return wc_get_checkout_url();
}

Custom Product Search URL in Wordpress

Hi I have tried the following code and does seem to work and changes the regular search url for a keyword "holi"
https://www.englishbix.com/search/?s=holi&post_type=product to https://www.englishbix.com/search/holi/
but it uses the default search template to display the product search results instead of the woocommerce search result template.
//change the url to static slug instead of dynamic one
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
Results I want
Results I get with above code

How to fix 'Woocommerce Cart PDF' functions file to move pdf-link to checkout page instead of cart page

I'm using a plugin for wordpress-woocommerce called Woocommerce Cart PDF (https://wordpress.org/plugins/wc-cart-pdf/). It generates a pdf-link of the current cart, but is located on the cart page.
I have a combined cart and checkout page so the link does not appear on my website. I don't have the knowledge on how to edit the plugin files myself for it to appear on my cart page.
I've tried the wordpress plugin support forum for this specific plugin, but no answer.
/**
* Generates the PDF for download
*
* #return void
*/
function wc_cart_pdf_process_download() {
if( ! function_exists( 'WC' ) ) {
return;
}
if( ! isset( $_GET['cart-pdf'] ) ) {
return;
}
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
if( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'cart-pdf' ) ) {
wc_add_notice( __( 'Invalid nonce. Unable to process PDF for download.', 'wc_cart_pdf' ), 'error' );
return;
}
$dompdf = new \Dompdf\Dompdf();
$content = $css = '';
$cart_table = wc_locate_template( 'cart-table.php', '/woocommerce/wc-cart-pdf/', __DIR__ . '/templates/' );
$css = wc_locate_template( 'pdf-styles.php', '/woocommerce/wc-cart-pdf/', __DIR__ . '/templates/' );
do_action( 'wc_cart_pdf_before_process' );
if( file_exists( $cart_table ) ) {
ob_start();
include $cart_table;
$content = ob_get_clean();
}
if( file_exists( $css ) ) {
ob_start();
include $css;
$css = apply_filters( 'woocommerce_email_styles', ob_get_clean() );
}
$dompdf->loadHtml( '<style>' . $css . '</style>' . $content );
$dompdf->setPaper( 'A4', 'portrait' );
$dompdf->render();
$dompdf->stream(
apply_filters( 'wc_cart_pdf_filename', 'WC_Cart-' . date( 'Ymd' ) . bin2hex( openssl_random_pseudo_bytes( 5 ) ) ) . '.pdf',
/**
* 'compress' => 1 or 0 - apply content stream compression, this is on (1) by default
* 'Attachment' => 1 or 0 - if 1, force the browser to open a download dialog, on (1) by default
*/
apply_filters( 'wc_cart_pdf_stream_options', array( 'compress' => 1, 'Attachment' => 1 ) )
);
exit;
}
add_action( 'template_redirect', 'wc_cart_pdf_process_download' );
if( ! function_exists( 'wc_cart_pdf_button' ) ) {
/**
* Renders the download cart as PDF button
*
* #return void
*/
function wc_cart_pdf_button() {
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
?>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
<?php
}
}
add_action( 'woocommerce_proceed_to_checkout', 'wc_cart_pdf_button', 21 );
This might be the incorrect part of the functions in the plugin, but I hop I got it right.
The wc_cart_pdf_process_download() function isn't really relevant. The comment states that it "Generates the PDF for download". What it's doing is responding when the user visits the PDF link by generating the requested PDF file. The important function is the one beneath that, wc_cart_pdf_button().
Now that we know the function we're interested in, what's next? In your question, you suggested editing the plugin files however it's important to avoid doing that. Editing your plugin files is a sure-fire way to guarantee the changes you make get overwritten the next time you update.
You have a couple of options:
Create a mini feature plugin.
Add the code to the bottom of your (hopefully child) theme's functions.php file.
The first option would be the recommended approach but that's going to take us well beyond the scope of the question. Placing the code in a child theme's functions.php file will be adequate for getting you up and running.
Okay, so now we know what the code we want to modify is and where we're going to store those modifications. Let's break down the actual code:
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
This checks two things, are we on the cart page and does the cart contain items? If either is false, we're going to bail out early. You're on the checkout page, not the cart page, so even if this function were to be called, it wouldn't make it past this conditional.
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
If those two previous checks passed, generate the button output.
add_action( 'woocommerce_proceed_to_checkout', 'wc_cart_pdf_button', 21 );
This executes the code on the woocommerce_proceed_to_checkout hook which fires after the cart totals on the cart page. The same action is used by the checkout button itself.
We need to write our own function that displays that same output on the checkout page. Without knowing where you'd like the button to appear, I can't suggest which action to use. I'm using woocommerce_checkout_order_review with a priority that'll put it between the order table and the payment options. If you need to reposition it, you'll have to go through those hooks and find somewhere that feels appropriate.
You did mention in your question that this is necessary because you have your cart and checkout pages combined. You may require a completely different hook, again there's no way for me to know based on your question alone.
Here's the final code:
function stackoverflow_wc_checkout_pdf_button() {
// We're on the checkout page based on the action.
// Highly unlikely we need the is_empty() check but it can't hurt if you may find yourself reusing elsewhere.
if ( WC()->cart->is_empty() ) {
return;
} ?>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
<?php
}
add_action( 'woocommerce_checkout_order_review', 'stackoverflow_wc_checkout_pdf_button', 15 );

Redirect to home if the cart is empty in Woocommerce

I am looking for and can not find anywhere as I can redirect the empty woocommerce cart to the homepage. I only find redirects that go to the store.
This is what I find, but I do not need to redirect to the home:
add_action("template_redirect", 'redirection_function');
function redirection_function(){
global $woocommerce;
if( is_cart() && WC()->cart->cart_contents_count == 0){
wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
}
}
Context link: If cart is empty, the cart page will redirect to shop page in WooCommerce?
To redirect to home if cart is empty you will use a similar code:
add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
if( WC()->cart->is_empty() && is_cart() ){
wp_safe_redirect( esc_url( home_url( '/' ) ) );
exit;
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
But if cart is emptied through ajax, this code will enable a redirection to home until the page will be reloaded.
For those finding this via search (like me) the code that Loic provided works with one small change if you're wanting the empty cart to redirect back to the homepage:
add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
if( WC()->cart->is_empty() && ( ! is_front_page() || is_cart() ) ){
wp_safe_redirect( esc_url( home_url( '/' ) ) );
exit;
}
}

Wordpress: map existing page content instead of a copy

I have a page with several subpages in wordpress. The page should display the same content as the second subpage. When I edit the page, there is an option to copy the content from the subpage into it, but then I would have to maintain two instances of the text. Is there a possibility to map existing page content into a page? I'm using Wordpress.com so I cannot edit any files/links on the server.
You could use a shortcode that fetches the content of a page specified. In functions.php:
add_action( 'init', 'so20477735_init', 11 );
function so20477735_init()
{
add_shortcode( 'duplicate_page', 'so20477735_shortcode_callback' );
}
function so20477735_shortcode_callback( $atts, $content = null )
{
extract( shortcode_atts( array(
'page_id' => 0
), $atts ) );
$page_data = get_page( $page_id );
$return = '';
if( ! is_null( $page_data ) )
$return .= $page_data->post_content;
return $return;
}
Example usage:
[duplicate_page page_id="12"]
EDIT
I missed the fact you're using wp.com. You should be able to do something similar with the display posts shortcode:
http://en.support.wordpress.com/display-posts-shortcode/

Resources