I am using Woocommerce Product Add-on plugin to allow visitor to select multiple options on simple product. As the product is simple product Woocommerce display an Add to cart button on products page view instead of Choose option button linking on Product detail page where visitor can choose options.
I am looking to create a function that will display Choose option button on product by ID.
Here is my starting code.
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button' );
function change_product_button() {
global $product;
$id = $product->id;
if ($id == 91){
echo '' . __('View Product', 'woocommerce') . '';
} else {
// display usual add to cart button
}
}
Or maybe we can force product by id as non purchasable to simulate a variable product.
This is a working solution
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button', 10, 2 );
function change_product_button($html, $product) {
$values = array(190, 91);
$id = $product->id;
if(in_array($id, $values)){
$html= '' . __('Choose an option', 'woocommerce') . '';
} else {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
Related
Is it possible to check if the product is in the cart, and then show (+ or -)? And if it is not in the basket showing Add to basket button.
I use the following line of codes, But it shows both plus and minus and add to cart button.
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
I am looking for a solution to only show add to cart button. if it is in the cart then show plus and minus.
to everyone !
Someone can help me with my proyect, Is very simple but I have one thing that makes me crazy.
I have this:
every checkbox have a value (additional value price)
I need change the price wen I checked in one, two or all checkbox and save this options to send it to the checkout and email....And show this options to order information in the back-end.
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
$item_name .= $value3.'<div class="lineList">';
$value2 = $cart_item['data']->get_meta('_preciosCata');
if( $value1 = $cart_item['data']->get_meta('_varArrayTitle') ) {
for ($i=0;$i<count($value1); $i++) {
//$item_name .= '<div class="lineOne lorem"><input type="checkbox" name="cart[%s][lorem]" id="addPrice" value="' . $value2[$i] . '"> <span class="custom-field"> ' . $value1[$i] . ' </span></div>';
$item_name .= sprintf( '<div class="lineOne lorem"><input type="checkbox" id="_checkbox' . $i . '" name="_checkbox' . $i . '" value="' . $value2[$i] . '" class="checkedBox url text" /> <span class="custom-field">' . $value3.$value1[$i] . ' </span></div>', $cart_item_key, esc_attr( $values['url'] ) );
}
}
$item_name .= '</div>';
return $item_name;
}
I update the car here but dont save the value:
add_action( 'wp_footer', 'bbloomer_cart_refresh_update_qty' );
function bbloomer_cart_refresh_update_qty() {
if (is_cart()) {
?>
<script>
jQuery('div.woocommerce').on('change', '.checkedBox', function(){
jQuery("[name='update_cart']").prop("disabled", false);
jQuery("[name='update_cart']").trigger("click");
//alert($(this).val());
});
</script>
<?php
}
}
I need help please
I have a custom product list on a specific category page.
It looks like this:
Product list category page with missing quantity selection and add to cart button at end.
I have been using the below in functions.php and it has worked fine. Since the last update of WooCommerce it no longer works. The add to cart buttons and quantity fields are not showing anymore.
I get no errors and the fields where the qty and button html goes are blank when I check the html for the page.
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
// Only for this product category
if ($wk_cat_value == 1 && is_product_category())
{
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() )
{
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
}
This line is a checkbox I added to the category page to specify if the current category is to use the custom template:
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
Template version Woocommerce files were 2.0.0.
Updated versions are 3.4.0.
Any suggestions greatly appreciated.
Found a solution:
function wk_add_to_cart() {
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
// $wk_cat_value is a flag added to this specific category to decide whether or not the add to cart should be added
$wk_cat_value = get_term_meta($term_id, 'wh_meta_cat_template', true);
global $product;
if ($wk_cat_value == 1 && is_product_category()) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
echo '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
echo woocommerce_quantity_input( array(), $product, false );
echo '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
echo '</form>';
}
}
}
add_action('woocommerce_after_shop_loop_item','wk_add_to_cart');
Hope that helps.
I try to adding a function to display view product detail button beside add to cart . but not working please help me !
add_action('woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );
function add_a_custom_button() {
global $product;
if( $product->is_type('variable') || $product->is_type('grouped') ) return;
echo '<div style="margin-bottom:10px;">
<a class="button custom-button" href="' . esc_attr( $product->get_permalink() ) . '">' . __('VIEW PRODUCT') . '</a>
</div>';
}
HERE IS IMAGE
How can I add the counter beside the cart button on my products page (not the details product page), and it would appear like in the picture bellow
Can someone tell me which plugin should I use or any other suggestion?
Thank you in advanced!
To display quantity input fields for simple products within your shop archive pages, you can add the following code to your active theme’s functions.php file.
/**
* Code should be placed in your theme functions.php file.
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
I've used this for a project I was working on not too long ago. This does what you need and is quite easy to follow: https://gist.github.com/JeroenSormani/a3325bdbca57f59690c1#file-woocommerce-archive-page-quantity-field-php
So you simply add this to functions.php and refresh... You may need to do:
.archive .quantity {
display: inline-block;
}
so it displays correctly.