• Resolved OC WordPress Web Designer

    (@oc-wordpress-web-designer)


    So I added a second add to cart button to a variable product.

    https://www.stagingdomain.xyz/product/sap-tools/

    I created a duplicate variable product add to cart button php file: single-product/add-to-cart/variation-add-to-cart-button2.php

    —–> The goal is to make the second add to cart button add the products to the cart with everything at $0 regardless of price. It will act as a “quote button” because I already have “$0” text changed to “Quote”.

    How can I accomplish that?

    ———-

    Edit variation-add-to-cart-button2.php? Maybe use some form of: return $price * 0;

    or

    apply filter on click of button two?

    // Variable
    add_filter(‘woocommerce_product_variation_get_regular_price’, ‘custom_price’, 99, 2 );
    add_filter(‘woocommerce_product_variation_get_price’, ‘custom_price’ , 99, 2 );

    // Variations (of a variable product)
    add_filter(‘woocommerce_variation_prices_price’, ‘custom_variation_price’, 99, 3 );
    add_filter(‘woocommerce_variation_prices_regular_price’, ‘custom_variation_price’, 99, 3 );

    function custom_price( $price, $product ) {
    // Delete product cached price (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 0; // X3 for testing
    }

    function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price (if needed)
    wc_delete_product_transients($variation->get_id());

    return $price * 0; // X3 for testing
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    Or can I set a hidden field on button 2 and change the price to 0 using that?

    <input type=”hidden” name=”custom_price” value=”0″ />

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    I found this link that creates a plugin the overrides the price of all products added to the cart.

    https://webkul.com/blog/woocommerce-set-custom-product-price-when-adding-to-cart/

    How can I apply this to only when the second add to cart button is used?

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    Solved!

    
    
    /**
     * Change Price to $0. Added variation-add-to-cart-button2.php. to woocommerce/includes/wc-template-functions.php and added .btnfree to button class
     */
    
    add_action( 'wp_footer', 'hiddenproductfield' );
    function hiddenproductfield() {
        global $product;
    
        
        ?>
            <script type="text/javascript">
                (function($){
                    $('button.btnfree').click( function(){
    
                        $('<input>').attr({type: 'hidden', name: 'quotesystem', value: 'quotesystem'}).appendTo('form');
    
                    });
                })(jQuery);
            </script>
        <?php
    }
    
    function add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    	// Has our option been selected?
    	if( ! empty( $_POST['quotesystem'] ) ) {
    		$product = wc_get_product( $product_id );
    		$price = $product->get_price();
    		// Store the overall price for the product, including the cost of the warranty
    		$cart_item_data['quote_price'] = $price * 0;
    	}
    	return $cart_item_data;
    }
    add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 3 );
    
    function before_calculate_totals( $cart_obj ) {
      if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
      }
      // Iterate through each cart item
      foreach( $cart_obj->get_cart() as $key=>$value ) {
        if( isset( $value['quote_price'] ) ) {
          $price = $value['quote_price'];
          $value['data']->set_price( ( $price ) );
        }
      }
    }
    add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals', 10, 1 );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change Price Of All Product to $0 On Click Add To Cart Button’ is closed to new replies.