I have a quick question. I have Woo Commerce Composite Products and the following code can be found in woocommerce-composite-products/includes/class-wc-product-composite.php on lines 1783-1801:

/**
 * Get the add to cart button text for the single page.
 *
 * @return string
 */
public function single_add_to_cart_text() {

	$text = __( 'UPDATE PRICE', 'woocommerce' );

	if ( isset( $_GET[ 'update-composite' ] ) ) {
		$updating_cart_key = wc_clean( $_GET[ 'update-composite' ] );

		if ( isset( WC()->cart->cart_contents[ $updating_cart_key ] ) ) {
			$text = __( 'Update Your Package', 'woocommerce-composite-products' );
		}
	}

	return apply_filters( 'woocommerce_product_single_add_to_cart_text', $text, $this );
}

I have changed one line by hard coding it as follows:

$text = __( 'GET PRICE', 'woocommerce' );

I wanted to do this in the functions file, but it didn't work. I saw the article here https://docs.woocommerce.com/document/change-add-to-cart-button-text/ and tried this but this didn't work because it ends up ignoring all the code in lines 1792-1798. I also tried to just add lines 1792-1798 into this function woo_custom_cart_button_text() from the article and that didn't work. How would I make this change in the functions file? I don't see any example code on this filter on how I would make such a change. I also need to make other woo commerce changes that would require more advanced php skills beyond what woo commerce documentation can show me. What do I do? Can anyone help?

    Looks like the text is passed into whatever function you create to handle the woocommerce_product_single_add_to_cart_text filter. Give this a try:

    function updateCartTest($text, $wcObj){
    	return 'GET PRICE';
    }
    
    add_filter('woocommerce_product_single_add_to_cart_text', 'updateCartText', 0, 2);

      $text = __( 'UPDATE PRICE', 'woocommerce' );

      Sheesh ... and here I thought I was bad at naming functions ... :p

        dalecosp;11057931 wrote:

        Sheesh ... and here I thought I was bad at naming functions ... :p

        Fairly common convention to use an underscore, two underscores, or an underscore followed by some designated typically capital letter, as the name of a convenience function/macro/whatever for text to be translated via gettext or perhaps some other translation system though.

          laserlight;11057937 wrote:

          Fairly common convention to use an underscore, two underscores, or an underscore followed by some designated typically capital letter, as the name of a convenience function/macro/whatever for text to be translated via gettext or perhaps some other translation system though.

          Thanks for the educational snippet (really!) 🙂

          I don't claim to know everything or even close, just that it seems funny that there are a boatload of people saying "PSR this" and "PSR that" and "vars must be {CamelCase, lowerCamelCase, snake_case, HuNotate, ...} ad nauseum " and then you come across something else.

          Sometimes it seems to make the Quest for World Peace look fairly simple by comparison 😉

          Do you suppose Facebook (as an example of a Ridiculously Large Shop) has someone in QA whose job is simply to make sure everything follows their naming conventions...?

            dalecosp;11057943 wrote:

            I don't claim to know everything or even close, just that it seems funny that there are a boatload of people saying "PSR this" and "PSR that" and "vars must be {CamelCase, lowerCamelCase, snake_case, HuNotate, ...} ad nauseum " and then you come across something else.

            Heck, I still prefix my object's private properties with an underscore. I know it's frowned upon and I've had several co-workers rib me about it, but old habits are hard to break...

              Thank you so much for the quick response and commentary guys. This place is awesome and I have so many more questions.

              So, I inputted the code but unfortunately, I get the following error which displays in place of the button:

              Warning
              call_user_func_array() expects parameter 1 to be a valid callback, function 'updateCartText' not found or invalid function name in /nas/content/staging/ucco/wp-includes/plugin.php

              Any ideas on how to fix. So, basically, the button with intially say Get Price, and when someone clicks it, it takes them to the shopping cart where there is a link to allow them to go back to the composite product order "wizard", which will then say "Update Your Package". So my main objective here is to take the proper wordpress protocol of not hacking core files and use a filter isolated in my child theme's functions.php file with this code. And I want to make sure that the quote form will not always say Get Cart, but will change to Update Your Package if they click on the link that goes back to the wizard to make changes which would then say "Update Your Package". So it works when I hack the code and just change the text from 'Update Price' to 'Get Price' , but doesn't work when I put the code suggested here in the functions file.

                I also reversed the order to the following and pretty much get the same error (I always wondered why you would call a filter which referenced a function that didn't get declared until in subsequent lines, but thats the way that its suggested like in the woocommerce article I referenced in my original post

                add_filter('woocommerce_product_single_add_to_cart_text', 'updateCartText', 0, 2);
                function updateCartTest($text, $wcObj){
                return 'GET PRICE';
                }

                  Ok, so actually the error was getting generated because the function specified didn't match the function declared (updateCartTest vs updateCartText) which fixed the error, but now the button just keeps the label "Get Price" and doesn't revert back to "Update Your Package". So basically i am getting the same results as when I add the code in my original post from here (https://docs.woocommerce.com/document/change-add-to-cart-button-text/). It still seems to be ignoring all the following code which changes the button when i go back to the order wizard back to "Update Your Package"

                  if ( isset( $GET[ 'update-composite' ] ) ) {
                  $updating_cart_key = wc_clean( $
                  GET[ 'update-composite' ] );

                  if ( isset( WC()->cart->cart_contents[ $updating_cart_key ] ) ) {
                  $text = __( 'Update Your Package', 'woocommerce-composite-products' );
                  }
                  }

                    Try dumping the contents of $_GET and WC()->cart->cart_contents. Make sure you've got the data you think you do. I've only used Woo a couple times in the past so I'm not terribly familiar with how the plugin does it's stuff, unfortunately.

                      Hi thanks for the suggestion. I have no idea what I am doing here. This is what I got.. not sure what to do with it though:

                      var_dump(WC()->cart->cart_contents); yields NULL

                      var_dump($_GET); yields:

                      array(17) { ["wccp_component_selection"]=> array(7) { [1470927819]=> string(3) "990" [1471592174]=> string(4) "8987" [1473784995]=> string(4) "9117" [1475593443]=> string(4) "8992" [1471618778]=> string(4) "8998" [1471856866]=> string(4) "9002" [1471877229]=> string(4) "9010" } ["wccp_component_quantity"]=> array(7) { [1470927819]=> string(1) "1" [1471592174]=> string(1) "1" [1473784995]=> string(1) "1" [1475593443]=> string(1) "1" [1471618778]=> string(1) "1" [1471856866]=> string(1) "1" [1471877229]=> string(1) "1" } ["update-composite"]=> string(32) "a61888f7c1b2461b155f00383f5c9567" ["component_1471592174_bundle_quantity_8954"]=> string(1) "0" ["component_1471592174_bundle_quantity_8955"]=> string(1) "0" ["component_1471592174_bundle_quantity_8989"]=> string(1) "0" ["component_1473784995_bundle_quantity_8996"]=> string(1) "0" ["component_1473784995_bundle_quantity_8997"]=> string(1) "0" ["component_1475593443_bundle_quantity_8932"]=> string(1) "0" ["component_1475593443_bundle_quantity_8993"]=> string(1) "0" ["component_1471618778_bundle_quantity_8999"]=> string(1) "0" ["component_1471618778_bundle_quantity_9000"]=> string(1) "0" ["component_1471856866_bundle_quantity_9003"]=> string(1) "0" ["component_1471856866_bundle_quantity_9004"]=> string(1) "0" ["component_1471877229_bundle_quantity_9011"]=> string(1) "0" ["component_1471877229_bundle_quantity_9012"]=> string(1) "0" ["component_1471877229_bundle_quantity_9172"]=> string(1) "0" }

                        If you're following the instructions on the page you linked to, where are you getting the WC() function? I don't see a reference to that anywhere. It looks like the global WooCommerce object has a WC_Cart property, so you could try that.

                        if ( isset( $_GET[ 'update-composite' ] ) ) {
                        	$updating_cart_key = wc_clean( $_GET[ 'update-composite' ] );
                        	global $woocommerce;
                        
                        if ( isset( $woocommerce->cart->cart_contents[ $updating_cart_key ] ) ) {
                        	$text = __( 'Update Your Package', 'woocommerce-composite-products' );
                        }
                        }

                        As I said earlier, I've not used WC much in production, so this is just a guess.

                          Hi, I hope anybody to solve my issue, maybe a conflict problem, I have a fresh news WordPress install with woocommerce plugin and test types, I've only created a new custom post type with types and in the admin on woocommerce product tab don't work anymore, the browser console throw an error:

                          Uncaught TypeError: Cannot read property 'split' of null WC-enhanced-select. min. j's? ver=2. 6.4:1

                          That error appears only when types plugin is active, all lawsuits installed versions.

                          Please Anyone can help me? Thanks

                            loisrymer;11058071 wrote:

                            browser console throw an error:

                            Uncaught TypeError:

                            Hello, welcome to PHPBuilder. Incidentally, you've just accomplished a "post hijacking", which is often frowned upon a bit in many internet forums, including this one.

                            As for your question, there are a couple of clues in your post that would lead us to know that this is a JavaScript error and not a PHP one. I'd recommend posting a new question in the "Clientside" forum here at PHPBuilder, or heading over to our sister site Developer.com...

                              Write a Reply...