PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] "Add to Cart" functionality instead of "Select Options" button behavior for variable products

 January 30, 2022     hook-woocommerce, php, woocommerce, wordpress     No comments   

Issue

Woocommerce has two button types in the product loop

  • "add to cart" for adding simple products to cart
  • "select options" if a simple product becomes a variable one or a subscription option is added.

My setting:

All Woocommerce products are configured as simple products and subscription products at the same time using the following plugins

  • "Woocommerce Subscription"
  • "All Products for Woocommerce Subscription". Default is set to simple product.

Now the buttons on all products change from "add to cart" to “select options”. That is Woocommerce default behavior.


My Question:

How can I keep the "add to cart" button and its functionality of adding the simple product to cart despite having a variable product?

Logic behind it: Users are given the opportunity to make a choice on checkout and therefore add to cart functionality instead of redirect to single product page for making a choice is desired.


Solution

You could use: (Explanation via comment tags added in the code)

function filter_woocommerce_loop_add_to_cart_link( $args, $product ) {
    // Shop page & product type = simple
    if ( is_shop() && $product->product_type === 'simple' ) {
        // Get product ID, sku & add to cart url
        $product_id = $product->get_id();
        $product_sku = $product->get_sku();
        $product_url = $product->add_to_cart_url();

        // Quantity & text
        $quantity = isset( $args['quantity'] ) ? $args['quantity'] : 1;
        $text = $product->add_to_cart_text();

        $args = '<a rel="nofollow" href="' . $product_url . '" data-quantity="' . $quantity . '" data-product_id="' . $product_id . '" data-product_sku="' . $product_sku . '" class="button product_type_simple add_to_cart_button ajax_add_to_cart add-to-cart" aria-label="Add to cart"><em>' . $text . '</em></a>';
    }
    
    return $args; 
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_woocommerce_loop_add_to_cart_link', 10, 2 );


Answered By - 7uc1f3r
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing