cart and checkout doesn't work in woocommerce - wordpress

I include woocommerce/template in my theme with this name : 'woocommerce' and also use add_theme_support() in my functions.php to introduce it .
but when user add a product to cart and wants to see the cart , user redirect to index.php
what should I do to fix this ?

#Ali Please share your code so I can have better idea.
OR
Add Theme Support Code to functions.php file
function mytheme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );
Created folder "woocommerce" into your theme folder.
Copy all files from "wp-content/plugins/woocommerce/templates/" to this folder.
Add below code to functions.php file.
function my_custom_add_to_cart_redirect( $url ) {
$url = get_permalink( get_option( 'woocommerce_checkout_page_id' ) );
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
Try this.

finally I fix it. it was so stupid.
I just forgot to add page.php in my theme
<?php get_header(); if (have_posts()) {while (have_posts({the_content();}}get_footer();
because cart shortcut need to display in a page .

Related

Custom Theme Single Product Page Wordpress/Woocommerce

I am building my custom theme and my single.php displays woocommerce single product page with
if ( have_posts() ) {
while( have_posts() ) {
the_post();
the_content();
}
}
Why is the page missing - title, reviews & breadcrumbs?
Woocommerce has its own templates for their pages. A single product should be showing on single-product.php. You would create a woocommerce folder inside your custom theme and copy the directories and files you want to edit over to your theme.
How to override single-product.php
If you want to custimize the single-product.php, you would create a woocommerce folder in your theme and copy the single-product.php over into it. So the directory would be yourtheme/woocommerce/templates/single-product.php. This way you have all the actions hooking in the functionality you are missing.
Here are the docs:
https://woocommerce.com/document/template-structure/
function mytheme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );
Please add this one in the functions.php file
I was missing
add_theme_support('woocommerce');
in my functions.php file

Custom post template in a plugin erase theme's one

i work with sportpress on Wordpress. I have a custom plugin that add a lot of function to it.
I created a template for the custom post-type of sportpress. They are in my plugin but i don't find how to make them prioritary on theme. The theme isn't a custom'one, so i don't want to delete or modify anything in it.
I have tried several things but theme's file is everytime superior.
Things tried :
naming the file 'single-....php',
filter : add_filter( 'template_include', 'toornament_template' ),
shortcode in theme file (works but not what i want...),
Thanks a lot...
EDIT :
here is what i tried exaclty, it works with another type of post that has no theme template, but not for the one that has single-post template in the theme...
add_filter( 'template_include', 'player_template', 1 );
function player_template( $template ) {
if ( is_singular( 'sp_player' ) && file_exists( plugin_dir_path(__FILE__) . 'templates/player-tpl.php' ) ){
$template = plugin_dir_path(__FILE__) . 'templates/player-tpl.php';
}
return $template;
};

Change Wordpress theme programatically for logged in users

I need to change the wordpress website theme for logged in users. I figured this code:
require_once (ABSPATH . WPINC . '/pluggable.php');
add_filter( 'template', 'change_theme_template' );
add_filter( 'stylesheet', 'change_theme_template' );
function change_theme_template( $template ) {
if ( is_user_logged_in() ) :\
$template = 'twentyfifteen';
endif;
return $template;
}
But it works only for parent themes, it does not work for child theme. I can't find a function to make it work with children. Is there a way to do it? Thank you

WP woocommerce change "Proceed to checkout" buttons URL

I want to change to URL of the "Proceed to checkout" button. I wanna do this to to check if there's a need for my product, so my customers shouldn't be able to really buy it. Therefore I want to direct them not to the checkout but to a customized page of mine.
Thanks a lot.
Josh
It should be available at below path
wp-content/plugins/woocommerce/templates/cart/proceed-to-checkout-button.php
the best way is the woocommerce hooks
Try to use this code (put it in your functions.php theme file).
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$url = "your checkout url ";
return $url;
}
You can customize it by adding some conditions to change the checkout url
for example:
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$allowed_countries = array('FR');
$customer_country = WC()->customer->get_default_country();
if( !in_array( $customer_country , $allowed_countries ) ) {
$url = wc_get_page_permalink( 'other-checkout' );
}
return $url;
}
Change Proceed To Checkout Text In WooCommerce
* Change Proceed To Checkout Text in WooCommerce
* Place this in your Functions.php file
This is the new method, for version 2.3.8 of WooCommerce and forward.
<?php
function woocommerce_button_proceed_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}

Open WooCommerce External Products in New Tab

I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
what you're currently doing is wrong... get_product_url is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...
One solution is given by #Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file.. woocommerce\templates\single-product\add-to-cart\external.php. You can find instructions inside it.
Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's functions.php.
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'rei_external_add_to_cart', 30 );
function rei_external_add_to_cart(){
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
$product_url = $product->add_to_cart_url();
$button_text = $product->single_add_to_cart_text();
do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<p class="cart">
<?php echo esc_html( $button_text ); ?>
</p>
<?php do_action( 'woocommerce_after_add_to_cart_button' );
}
Here is how you add target="_blank" to the links on archive pages:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path:
woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.

Resources