This is the code in the file loop/add-to-cart.php
I tried to edit the code, when I click on the add to cart button, nothing is added anyway
<?php
if (!defined('ABSPATH')) {
exit;
}
global $product;
if ( $product->is_in_stock() ) {
echo apply_filters(
'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
sprintf(
'<div class="add-to-cart-container product-buttons-wrapper mt-auto"><a href="%s" data-quantity="%s" class="%s product_type_%s single_add_to_cart_button button button-primary d-none %s" %s> %s</a>',
esc_url($product->add_to_cart_url()),
esc_attr(isset($args['quantity']) ? $args['quantity'] : 1),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
esc_attr($product->get_type()),
$product->get_type() == 'simple' ? 'ajax_add_to_cart' : '',
isset($args['attributes']) ? wc_implode_html_attributes($args['attributes']) : '',
esc_html($product->add_to_cart_text())
),
$product,
$args
);?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr($product->get_id()); ?>" data-quantity="1" data-product_id="<?php echo esc_attr($product->get_id()); ?>" class="single_add_to_cart_button alt button button-primary"><?php echo esc_html($product->single_add_to_cart_text()); ?></button>
<button class="button button-secondary" data-bs-toggle="modal" data-bs-target="#modal-request">Заказать</button></div>
<?php } else { ?>
<div class="product-buttons-wrapper">
<button class="button button-secondary" data-bs-toggle="modal" data-bs-target="#modal-request">Заказать</button>
</div>
<?php }
Tried online solutions, nothing worked.
Related
I spent my day trying to understand how can I set a conditional button for my variable products based on stock. I followed and searched all tips, discussions, etc... But nothing is OK.
The idea is :
if no variation selected : Pick your style
if variation is available : add to cart
if the variation is not available or out of stock : disable the button "add to cart" and change text to "sold out"
if all variations are out of stock : disable "add to cart" and change text to "sold out"
I've tried different snippets, I'm quite a beginner... I also tried dynamically changing text inside the button with jQuery, because I have the following class : wc-variation-is-unavailable when a variation is out of stock.. The thing I succeeded was to make the button grey :D But It still clickable..
This is the last snippet I tried, but it changes the button label for all variations :/
function ace_custom_change_singe_add_to_cart_text( $text, $product ) {
global $product;
$text = __( 'Add to basket' );
$stock = $product->get_stock_quantity();
if ( $product->is_type( 'variable' )) {
if ($stock > 1) {
$text = __( 'Pick your style' );
}
elseif ($stock == 0) {
$text = __( 'Sold Out' );
}
}
if ( $product->is_type( 'grouped' )) {
$text = __( 'View components' );
}
// Set a button text for a specific category
$category = get_term_by( 'slug', 'wine', 'product_cat' );
if ( $category && in_array( $category->term_id, $product->get_category_ids() ) ) {
$text = __( 'Pick this bottle' );
}
return $text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'ace_custom_change_singe_add_to_cart_text', 10, 2 );
This is in my variable.php file :
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
<?php
do_action( 'woocommerce_before_single_variation' );
do_action( 'woocommerce_single_variation' );
?>
<div class="variations_button">
<?php if (StockieSettings::get('woocommerce_add_to_cart_ajax', 'global')) { ?>
<a id="dyId" class="single_add_to_cart_button btn alt btn-loading-disabled">
<i class="icon ion ion-left">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="12px" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 12 16" xml:space="preserve">
<path class="st0" d="M9,4V3c0-1.7-1.3-3-3-3S3,1.3,3,3v1H0v10c0,1.1,0.9,2,2,2h8c1.1,0,2-0.9,2-2V4H9z M4,3c0-1.1,0.9-2,2-2
s2,0.9,2,2v1H4V3z"/>
</svg>
</i>
<?php echo (esc_html( $product->single_add_to_cart_text()) ); ?>
</a>
<?php } else { ?>
<button type="submit" class="single_add_to_cart_button btn alt btn-loading-disabled">
<i class="icon ion ion-left">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="12px" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 12 16" xml:space="preserve">
<path class="st0" d="M9,4V3c0-1.7-1.3-3-3-3S3,1.3,3,3v1H0v10c0,1.1,0.9,2,2,2h8c1.1,0,2-0.9,2-2V4H9z M4,3c0-1.1,0.9-2,2-2
s2,0.9,2,2v1H4V3z"/>
</svg>
</i>
<?php echo esc_html( $product->single_add_to_cart_text() ); ?>
</button>
<?php } ?>
</div>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="" />
<?php do_action( 'woocommerce_after_single_variation' ); ?>
</div>
If someone can help, I will be really grateful.
I am using do_shortcode hook that is preventing "Add to cart" to show non-paid members.
I have used the following code and the "Add to cart" doesn't seem to work.
else{
echo do_shortcode( '[ihc-hide-content ihc_mb_type="show" ihc_mb_who="1" ihc_mb_template="1" ]' . '<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>' . '[/ihc-hide-content]" );
');
It's being displayed as :
Output screenshot
Try this code
else{
echo do_shortcode(
'[ihc-hide-content ihc_mb_type="show" ihc_mb_who="1" ihc_mb_template="1" ]' .
'<button
type="submit"
name="add-to-cart"
value="
'.<?php
esc_attr( $product->get_id() )
?>.'"
class="single_add_to_cart_button button alt">
'.<?php
esc_html( $product->single_add_to_cart_text() )
?>.'
</button>' .
'[/ihc-hide-content]" );
');
UPDATE
else{
echo do_shortcode(
'[ihc-hide-content ihc_mb_type="show" ihc_mb_who="1" ihc_mb_template="1" ]' .
'<button
type="submit"
name="add-to-cart"
value="
'.
esc_attr( $product->get_id() )
.'"
class="single_add_to_cart_button button alt">
'.
esc_html( $product->single_add_to_cart_text() )
.'
</button>' .
'[/ihc-hide-content]" );
');
In WooCommerce 3.0+, I have created some tabs using js and in each tab contains products from different categories. I have managed to modify add-to-cart link for simple products where ones the addtocart button is clicked it goes to the next tab without refreshing and the product is being added successfully to the cart.
if ( has_term( 'jeans-discount', 'product_cat', $post ) ) {
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" data-target="2" href="javascript:void(0); %s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="custom %s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
}
However I am not able to modify the addtocart button for variable products in variation-add-to-cart.php template file:
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
For simple product the addtocart link shows as href="javascript:void(0); /wordpress/woo-slider/?add-to-cart=73".
Is there a way i can do this for variable products addtocart link as well?
For WooCommerce version 3.0+ you will override variation-add-to-cart.php template this way:
<?php
/**
* Single variation cart button
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php
/**
* #since 3.0.0.
*/
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );
/**
* #since 3.0.0.
*/
do_action( 'woocommerce_after_add_to_cart_quantity' );
// Set HERE your targeted product category
if ( has_term( 'jeans-discount', 'product_cat', $product->get_id() ) ) {
?>
<button type="submit" data-target="2" class="single_add_to_cart_button button alt" onclick="javascript:void(0);"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } else { // Other product categories
?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } ?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
I can't test it for real (as I don't have a system of tabs enabled), but this is successful working on my test server without error problems.
For WooCommerce 2.6.x you will override variation-add-to-cart.php template this way:
<?php
/**
* Single variation cart button
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
// Set HERE your targeted product category
if ( has_term( 'jeans-discount', 'product_cat', $product->id ) ) {
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php if ( ! $product->is_sold_individually() ) : ?>
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<?php endif; ?>
<button type="submit" data-target="2" class="single_add_to_cart_button button alt" onclick="javascript:void(0);"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } else { // Other product categories
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php if ( ! $product->is_sold_individually() ) : ?>
<?php woocommerce_quantity_input( array( 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ); ?>
<?php endif; ?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php } ?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
I'm using a Wordpress plugin called EventOn Tickets which offers ticket purchases. Woocommerce Addon Fields display on regular products but not on the EventOn tickets. I'm looking for help to have the Woocommerce Addon Fields added to the Eventon Tickets checkout and be functional.
Here is the problem code that only displays the "Add to Cart" button with a quantity. Using standard Woocommerce Variable Products it works but that doesn't allow me to display the options as I would like and I like the data entry with Woocommerce Product Addons.
<p itemprop="price" class="price tx_price_line"><?php echo eventon_get_custom_language($opt, 'evoTX_002ff','Price').': '. $product->get_price_html(); ?></p>
<form class='tx_orderonline_single' data-producttype='single' method="post" enctype='multipart/form-data'>
<div class='tx_orderonline_add_cart'>
<?php
// calculate correct max capacity
$max_quantity = ($capacity_of_this_repeat!='none') ? $capacity_of_this_repeat:
($product->backorders_allowed() ? '' : $product->get_stock_quantity());
if ( ! $product->is_sold_individually() )
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $max_quantity, $product )
), $product );
?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button data-product_id='<?php echo $woo_product_id;?>' id='cart_btn' class="evoAddToCart evcal_btn single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', eventon_get_custom_language($opt, 'evoTX_002','Add to Cart'), $product->product_type); ?></button>
<div class="clear"></div>
</div>
</form>
Product addons are displayed on the woocommerce_before_add_to_cart_button hook (see the constructor of the Product_Addon_Display class). So as long as you have a do_action( 'woocommerce_before_add_to_cart_button' ); action hook then the addons should display.
Making sure that your form has the standard WooCommerce hooks will help it support more WooCommerce extensions.
<p itemprop="price" class="price tx_price_line"><?php echo eventon_get_custom_language($opt, 'evoTX_002ff','Price').': '. $product->get_price_html(); ?></p>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class='tx_orderonline_single' data-producttype='single' method="post" enctype='multipart/form-data'>
<div class='tx_orderonline_add_cart'>
<?php
// calculate correct max capacity
$max_quantity = ($capacity_of_this_repeat!='none') ? $capacity_of_this_repeat:
($product->backorders_allowed() ? '' : $product->get_stock_quantity());
if ( ! $product->is_sold_individually() )
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $max_quantity, $product )
), $product );
?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<button data-product_id='<?php echo $woo_product_id;?>' id='cart_btn' class="evoAddToCart evcal_btn single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', eventon_get_custom_language($opt, 'evoTX_002','Add to Cart'), $product->product_type); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<div class="clear"></div>
</div>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
I need to add the woocommerce cart button to one of my pages and was wondering if someone could help with the code required to call the cart button. Here is the current code:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="product-image1">
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
<?php echo $product->get_image(); ?>
</a>
</div>
<div id="product-description-container">
<ul>
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
<li><h4><?php echo $product->get_title(); ?></h4></li></a>
<li><?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt )?></li>
<li><h6><?php echo $product->get_price_html(); ?> **MISSING CODE TO ADD TO CART BUTTON HERE**</h6></li>
</ul>
</div>
<?php endwhile; // end of the loop. ?>
I think may be you need following code.
Add this code to your place = (**MISSING CODE TO ADD TO CART BUTTON HERE**).
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
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 );
Hope this will help.