- This topic has 42 replies, 3 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
December 5, 2022 at 9:37 am #2450562
David
StaffCustomer SupportGlad to be of help
December 11, 2022 at 1:00 am #2457588Georgi
Okay, I found a moment to try it.
I added the PHP snippet you wrote to me to functions.php
Is that correct?
December 11, 2022 at 8:10 am #2457963David
StaffCustomer SupportIn the functions.php in your Child Theme. Yes 🙂
This doc explains more on adding PHP:
December 12, 2022 at 10:35 am #2459435Georgi
Thanks!
December 12, 2022 at 10:58 am #2459466David
StaffCustomer SupportYou’re welcome !
December 16, 2022 at 12:02 pm #2464533Georgi
The scripts are tracking purchases but do you know why I’m not getting the dynamic values of the orders, instead I get the same value for every conversion?
December 17, 2022 at 5:18 am #2464996David
StaffCustomer SupportThat unfortunately is not something i can assist with. But i found this woo doc regarding this which you may find of use”
https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page/
December 17, 2022 at 11:09 am #2465368Georgi
Sorry to bother again, did I put my script correctly in the code?
/** * Add custom tracking code to the thank-you page */ add_action( 'woocommerce_thankyou', 'my_custom_tracking' ); function my_custom_tracking( $order_id ) { // Lets grab the order $order = wc_get_order( $order_id ); <!-- Event snippet for Покупка от сайта conversion page --> <script> gtag('event', 'conversion', { 'send_to': 'AW-exampleID', 'value': 1.0, 'currency': 'BGN', 'transaction_id': '' }); </script> // This is the order total $order->get_total(); // This is how to grab line items from the order $line_items = $order->get_items(); // This loops over line items foreach ( $line_items as $item ) { // This will be a product $product = $order->get_product_from_item( $item ); // This is the products SKU $sku = $product->get_sku(); // This is the qty purchased $qty = $item['qty']; // Line item total cost including taxes and rounded $total = $order->get_line_total( $item, true, true ); // Line item subtotal (before discounts) $subtotal = $order->get_line_subtotal( $item, true, true ); } }December 19, 2022 at 4:37 am #2466786David
StaffCustomer SupportNo, you need to separate the function in to two parts, a. getting the values and b. printing out the script
For example:/** * Add custom tracking code to the thank-you page */ add_action( 'woocommerce_thankyou', 'my_custom_tracking' ); function my_custom_tracking( $order_id ) { // Get the order $order = wc_get_order( $order_id ); // get the order total and stotre it in $order_total $order_total = $order->get_total(); // Close the PHP so we can print some HTML in the hook // inisde that we will echo the $order_total ?> <!-- Event snippet for Покупка от сайта conversion page --> <script> gtag('event', 'conversion', { 'send_to': 'AW-exampleID', 'value': <?php echo esc_attr( $order_total ); ?>, 'currency': 'BGN', 'transaction_id': '' }); </script> <!-- End of Event snippet for Покупка от сайта conversion page --> <?php }December 20, 2022 at 6:12 am #2468108Georgi
Thanks! The way you wrote it should work?
December 20, 2022 at 7:38 am #2468198David
StaffCustomer SupportI believe so – i haven’t used that hook before for that purpose but its the way i would think to do it 🙂
December 20, 2022 at 10:31 am #2468499Georgi
Okay, I will try it and report to you so you can know 🙂
December 20, 2022 at 11:12 am #2468535David
StaffCustomer SupportOk 🙂
December 20, 2022 at 12:19 pm #2468606Georgi
Just a quick question since I’m still not 100% aware of the Hook abilities.
I’ve this code in my functions.php that sets minimum order amount for checkout
//Set a minimum order amount for checkout add_action( 'woocommerce_checkout_process', 'bbloomer_wc_minimum_order_amount' ); add_action( 'woocommerce_before_cart', 'bbloomer_wc_minimum_order_amount' ); function bbloomer_wc_minimum_order_amount() { $minimum = 10; // change this to your minimum order amount if ( WC()->cart->subtotal < $minimum ) { if( is_cart() ) { wc_print_notice( sprintf( 'Нужно е да добавите продукти за минимум %s за да извършите поръчката си. Сегашната стойност на продуктите Ви е %s' , wc_price( $minimum ), wc_price( WC()->cart->subtotal ) ), 'error' ); } else { wc_add_notice( sprintf( 'Нужно е да добавите продукти за минимум %s за да извършите поръчката си. Сегашната стойност на продуктите Ви е %s' , wc_price( $minimum ), wc_price( WC()->cart->subtotal ) ), 'error' ); } } }In order for it to work in a hook, I need to set a rule for the display to be on the checout page and also select “Execute PHP”?
December 20, 2022 at 1:37 pm #2468670Ying
StaffCustomer SupportThis code should be added to functions.php, it should not be added to a hook element.
-
AuthorPosts
- You must be logged in to reply to this topic.