My simple question is that how I can make this code working
esc_url( WC()->cart->get_cart_url->get_remove_url( $cart_item_key ) ),
the above i have tried and the current code is below
esc_url( $woocommerce->cart->get_remove_url( $cart_item_key ) ),
so instead my current url : example.com/?removed_item=1
should be something like example.com/checkout/?removed_item=1
Thanks for any suggestion
Try the below code. code will goes in your active theme functions.php file.
add_filter( 'woocommerce_get_remove_url', 'change_item_remove_url_to_checkout', 10, 1 );
function change_item_remove_url_to_checkout( $remove_url ) {
$cart_page_url = wc_get_page_permalink( 'cart' );
$replacement_url = wc_get_page_permalink( 'checkout' ); // Shop page
$remove_url = str_replace( $cart_page_url, $replacement_url, $remove_url );
return $remove_url;
}
Related
so I have been struggling on this problem.
I wanna change the variable add to cart Button to an Icon. This is what my code does
And what I wanna achieve:
I am using Elementor and the products are in a slider from Ultimate Addons for Elementor, which I customized for my needs. Link of the doc (https://ultimateelementor.com/docs/filters-actions-for-woocommerce-products)
function price_after( $product_id, $settings ) {
global $product;
$sprache = $product->get_attribute('sprache');
if( ! empty( $sprache ) ){
woocommerce_variable_add_to_cart();
//$link['url'] = apply_filters( 'woocommerce_variable_add_to_cart', get_permalink( $product->id ) );
/*$link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Warenkorb', 'woocommerce' ) );*/
} else {
if ( $product->is_in_stock() ) {
echo '<div class="add_to_cart_wishlist">';
echo "<button class='button product_type_simple add_to_cart_button ajax_add_to_cart' href='https://[siteurl]/?add-to-cart=".$product_id."'data-product_id='".$product_id."'>". file_get_contents('[siteurl]/wp-content/uploads/2021/06/cart-1-1-1.svg')."</button>";
echo do_shortcode("[ti_wishlists_addtowishlist]");
echo "</div>";
} }} add_action('uael_woo_products_price_after', 'price_after', 10, 2);
And this is what I use to change the text of the add to cart button
function filter_woocommerce_product_add_to_cart_text( $add_to_cart_text, $product ) {
$add_to_cart_text = 'Test';
return $add_to_cart_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );
I even deleted the esc_html in the template folder of woocommerce...
Now I dont know to do else, I need your help!!!
Thank you in advance!
Is there a way I can still add/remove a product to my cart without the page url changing?
I have custom add to cart button on my custom Wordpress theme but as I add product to cart the url changes to http://localhost/mytheme/?add-to-cart=15. Is there a way I can avoid it and still add product to the cart?
<?php global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( 'Add product',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->get_type() ),
esc_html( $product->add_to_cart_text() )
),
$product ); ?>
WooCommerce has a hook woocommerce_add_to_cart_redirect which is called when you add a product to your cart.
There are two arguments which you get $url where it is redirecting and $product which was added to your cart.
add_filter( 'woocommerce_add_to_cart_redirect', 'bks_add_to_cart_redirect', 10, 2 );
function bks_add_to_cart_redirect( $url, $product ) {
if ( $product && is_a( $product, 'WC_Product' ) ) {
// Do any change or URL you want to $url.
}
return $url;
}
You can change the logic and redirect it back to the product page by changing $url inside if like so.
$url = esc_url($product->get_permalink());
You can add paramters to it.
$url = esc_url( add_query_arg('name', 'value', $product->get_permalink() ) );
-- OR --
Just redirect it to a custom page.
$url = esc_url( home_url() . '/custom_page' );
Been trying to find something about this, but the solutions I found so far here do not work.
I need, in the single product page, to display the add to cart button like this:
ADD TO CART - JUST $PRICE
It needs to be done from the functions.php file in the child theme.
Thanks a lot!
To handle the display of the product price in add-to-cart button for all product prices on shop, other archives pages and single product pages, without any need of overriding templates, using dedicated Woocommerce filter hooks:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
// Variable products
if( $product->is_type('variable') ) {
// shop and archives
if( ! is_product() ){
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
return $button_text . ' - From ' . strip_tags( $product_price );
}
// Single product pages
else {
return $button_text;
}
}
// All other product types
else {
$product_price = wc_price( wc_get_price_to_display( $product ) );
return $button_text . ' - Just ' . strip_tags( $product_price );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
On shop page:
On single product pages:
I would suggest you override the WooCommerce template from the child theme, the file name is add-to-cart.php and can be found at woocommerce > loop.
At the bottom add the following code, it will be displayed only in product single.
if (is_single()) {
echo sprintf(
'<span>%s %s</span>',
esc_attr__('JUST', 'woocommerce'),
esc_attr($product->price)
);
}
Edit your woocommerce/loop/add-to-cart.php template file like this:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>ADD TO CART - JUST %s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
$product->get_price()
),
$product, $args );
For the single product pages do basically the same in:
woocommerce/single-product/add-to-cart/simple.php
And any other template you use in the add-to-cart directory.
Note: Don't edit the plugin file itself but override the template in your (child) theme so it won't be overwritten when the plugin gets updated.
I tried using this code below to change the URL of the word "Shop" but it seems to do nothing:
add_filter( 'woocommerce_page_title', 'woo_shop_page_title');
function woo_shop_page_title( $page_title ) {
if( 'Shop' == $page_title) {
return "My new title";
}
}
I don't want users to go to the main "Shop" page and setup many categories.
Unfortunately I have not been able to find a way to remove or change the URL of that "Shop" breadcrumb. The official docs only mention how to change the "Home" URL, not "Shop".
You're help is appreciated.
To rename any breadcrumb item and change its link, use woocommerce_get_breadcrumb filter hook this way:
add_filter( 'woocommerce_get_breadcrumb', 'custom_get_breadcrumb', 20, 2 );
function custom_get_breadcrumb( $crumbs, $breadcrumb ){
if( ! is_shop() ) return $crumbs; // Only shop page
// The Crump item to target
$target = __( 'Shop', 'woocommerce' );
foreach($crumbs as $key => $crumb){
if( $target === $crumb[0] ){
// 1. Change name
$crumbs[$key][0] = __( 'Name', 'woocommerce' );
// 2. Change URL (you can also use get_permalink( $id ) with the post Id
$crumbs[$key][1] = home_url( '/my-link/' );
}
}
return $crumbs;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Please check this code in on home replace Start chenag in woocommerce
function woocommerce_breadcrumb( $args = array() ) {
$args = wp_parse_args( $args, apply_filters( 'woocommerce_breadcrumb_defaults', array(
'delimiter' => ' / ',
'wrap_before' => '',
'wrap_after' => '',
'before' => '',
'after' => '',
'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
) ) );
$breadcrumbs = new WC_Breadcrumb();
$args['home'] = "Start"; // home replace start set
if ( ! empty( $args['home'] ) ) {
$breadcrumbs->add_crumb( $args['home'], apply_filters( 'woocommerce_breadcrumb_home_url', home_url() ) );
}
$args['breadcrumb'] = $breadcrumbs->generate();
The 'shop' breadcrumb is coming from you 'Shop' pages'. Simply change your shop page slug to whatever you want, or if it is another page altogether then in the woocommerce settings you set the shop page to your new page. The url will always be the page's slug.
I am overriding the AJAX function to add a product using WooCommerce so I can send attributes to the cart as well (which doesn't work out of the box).
I followed the instructions on Update WooCommerce Cart After Adding Variable Product Via AJAX - which does what I want - but it doesn't work completely.
Everything is good when I am logged out. But when I log into Wordpress the PHP override doesn't work/gets ignored. I get no errors and no product is added to the cart. I tried many different approaches, but nothing helps.
function ajax_add_to_cart_script() {
$vars = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );
wp_enqueue_script( 'wc-variation-add-to-cart', get_template_directory_uri() . '/js/jquery.add-to-cart.js', array( 'jquery' ), null, true);
wp_localize_script( 'wc-variation-add-to-cart', 'WC_VARIATION_ADD_TO_CART', $vars );
}
add_action( 'wp_enqueue_scripts', 'ajax_add_to_cart_script' );
add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );
function so_27270880_add_variation_to_cart() {
ob_start();
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
$quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
$variation_id = isset( $_POST['variation_id'] ) ? absint( $_POST['variation_id'] ) : '';
$variations = ! empty( $_POST['variation'] ) ? (array) $_POST['variation'] : '';
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) ) {
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
wc_add_to_cart_message( $product_id );
}
// Return fragments
WC_AJAX::get_refreshed_fragments();
} else {
// If there was an error adding to the cart, redirect to the product page to show any errors
$data = array(
'error' => true,
'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
);
wp_send_json( $data );
}
die();
}
I am making sure that add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' ); is added before the snippet to make sure it gets fired, which works when I am logged out - but not when I am logged in.
Anyone got any clue why?
Ping #helgatheviking
An update, it seems to work if I add the following to my functions.php
add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );
add_action( 'wp_ajax_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );
Meaning, that one is for logged in admins and one is for normal users. Not sure why I need both because every example I have seen with this kind of extension/override only mentions the wp_ajax_nopriv_$function
Hope this helps anyone who have the same problem as me. And if anyone could explain to me why this is needed, I would be happy to hear it.