I have a Woocommerce store, I was looking to add another add to cart button named "Try now" to a particular category(not to all the categories). When the user clicks on "try now", it gets redirected to the custom checkout page.
On custom checkout page, try-now products can be added up-to 5 products max, total is fixed as 500 for all the try now products and payment only by card.
I have found the following codes.
Code to replace add to cart button
function remove_loop_button(){
if (in_array( 10, $product->category_ids))
remove_action(‘woocommerce_after_shop_loop_item’,‘woocommerce_template_loop_add_to_cart’, 10);
}
add_action(‘init’,’remove_loop_button’);
add_action(‘woocommerce_after_shop_loop_item’,’replace_add_to_cart’);
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo ‘<a href=”‘.esc_attr($link).'”>Try Now</a>’;
}
And also this code to customize checkout page from Make 2 different WooCommerce checkout pages?
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
global $woocommerce;
//assuming only one product in cart
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
}
switch ($_product->id) {
case '666': //product id for product 1
//do stuff with the checkout fields
break;
case '999': //product id for product 2
//do stuff with the checkout fields
break;
}
return $fields;
}
I know it's quite complicated, Can any one please suggest me where to start? Or any hint will be appreciated. Thanks.
Related
I'm trying to do a special logic for my custom plugin. If the user has added a specific product type in their cart, in the checkout page there must be radio inputs that determine whether the user wants the specific product type to be shipped or stored in vault. I've done everything for the frontend part (creating the radio inputs, built the JavaScript logic to remove from the DOM what's not necessary and so on...) but I now need to programatically remove the shipping from the order and remove the "Shipping" row inside the order preview in the checkout page. I tried the following filter
add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_shipping_labels', 10, 2 );
function remove_shipping_labels( $label, $method ) {
return '';
}
But it's removing just the label text "Free Shipping" but not the entire shipping row inside the order preview in the checkout page. How can I programatically remove the shipping availability from an order through AJAX and update the user interface inside the checkout page?
function hide_shipping_based_on_prod_type( $rates ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach ( $items as $item => $values ) {
$product_id = $values['data']->get_id();
$product_type = WC_Product_Factory::get_product_type( $values['data']->get_id() );
if ( 'simple' === $product_type ) { //check product types of woocommerce to add more conditions
unset( $rates['free_shipping:1'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_prod_type', 100 );
add_action(
'after_setup_theme',
function() {
WC_Cache_Helper::get_transient_version( 'shipping', true );
}
);
Currently in woocommerce, add to cart button disabled, if the stock status is out of stock. I add new stock status with the label Discontinued product by using woocommerce_product_stock_status_options, now I am looking for a way to treat this product like it is out of stock.
Since I believe it is better to separate between a product that is not produced anymore and a product that produced and available in another store but it's not in stock.
You can use the following to disable add to cart button based on a custom stock status (where you will replace custom_status_slug by your custom status slug):
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $purchasable;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I propose a solution that displays the stock status on the product page but not the 'add to cart' button
add_filter('woocommerce_product_is_in_stock', 'filter_is_in_stock_callback', 10, 2 );
function filter_is_in_stock_callback( $stock, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $stock;
}
Thanks for the first answer :)
Everything I can find on removing the WooComm Add To Cart button will remove not just the add to cart button but also the pricing/variations, aka the whole add to cart area.
My goal is to enable/disable the ability to purchase a product with a checkbox/selector on the product info page. BUT I still have to be able to see the product variation pricing and the variation drop down menu.
This is important. The pricing shown under the product title, for a variation, will be something like $20.00 - $40.00 and not until you select the variation choice will it show the price next to the add to cart button.
So far I have things working wherein I can remove the add to cart area — variations and all — conditionally on my custom field, but I have no idea how to hide/disable click/remove just the add to cart button and allow variations to still be chosen with the variation price displayed.
function remove_add_to_cart(){
if(get_post_meta(get_the_ID(), 'woo_callforinfo', true)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
} add_action('woocommerce_single_product_summary','remove_add_to_cart');
Here's what I did. The conditional IF statement is because I have a RETAIL shop with variable products that I don't want affected.
function remove_add_to_cart(){
if ( has_term( 'wholesale', 'product_tag' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
add_action('woocommerce_single_variation','remove_add_to_cart');
Added this to get rid of the 'Sorry..' message if price is not set.
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text,
$untranslated_text, $domain )
{
if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
$translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}
Just add the following code in your functions.php and you will find button hidden
I don't know whether my solution is perfect. But it works. Normally if is_purchasable is returned to the filter woocommerce_is_purchasable, the ‘Add to Cart’ button is displayed, and if false is returned the button is hidden.
So, you just need to add the following:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
// Write code to access custom field value in this function
// let $custom_value be the value from checkbox
return ($custom_value == false ? false : $is_purchasable);
}
No incompatibility issues would creep up.
Right now I am trying to get it so that whenever someone gets to my site's product page their cart is automatically emptied.
I am using a woocommerce product addon called "Product & Checkout Options for WooCommerce" that allows me to use radiobuttons/checkboxes for my products, I don't know if that will alter any of the code.
I've tried php code such as this but it hasn't worked:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_in_cart' , 10, 1);
function only_one_in_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
unset($cart_item_data['product_meta']);
return true;
}
It's better to add the hook on your single product page, do it with the action woocommerce_before_single_product:
add_action( 'woocommerce_before_single_product', `only_one_in );
function only_one_in_cart() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
This will empty your cart each time you visit the page, if it's late, then you can add the function into the wp_head hook and validate if you are in the product page by is_product():
add_action( 'wp_head', `only_one_in );
function only_one_in_cart() {
if ( is_product() ){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
I am using woocommerce plugin in a wordpress site to sell some products. There are two types of products. some products will have add to cart button, but some will have call to order button instead of add to cart. But Both type of products will show the price and discounted price.
I am using some code to change the text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
return __( 'Call to order', 'woocommerce' );
}
that works. But I when I am using this code to change the url or say stop the button to add to cart that is not working.
add_filter( 'woocommerce_product_add_to_cart_url', 'woo_more_info_link' );
function woo_more_info_link( $link ) {
global $product; // switches link in all cases, i.e. in plugins
$link = get_permalink( $product->id );
return $link;
}
I actually want that the add to cart button should not working and should say only call to order.
Can anybody tell me how to stop add to cart from working.