Adding custom text with a link via PHP function in Woocommerce - php

I'm trying to add additional text woocommerce_before_checkout_form in functions.php via Editor but I see following error:
"Your PHP code changes were rolled back due to an error on line 0 of
file Unknown. Please fix and try saving again. Exception thrown
without a stack frame"
function bonus() {
echo "<div id='bonus'>Don't you have a coupon?Click here to get 5% OFF</div>";
}
add_action( 'woocommerce_before_checkout_form', 'bonus' );
When I add only text without ahref everything works.
I want to add to this ahref redirect to: https://mywebsite.com/checkout/?apply_coupon=promo5

Try this.
You need to change <a href="https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5"> to <a href='https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5'>
function bonus() {
echo "<div id='bonus'>Don\'t you have a coupon?<a href='https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5'>Click here to get 5% OFF</a></div>";
}
add_action( 'woocommerce_before_checkout_form', 'bonus' );

You have used double quote within double quote that is the reason error is coming.
Try below code it will work.
function bonus() {
echo "<div id='bonus'>Don't you have a coupon?<a href='https://mywebsite.com/checkout/?apply_coupon=promo5?apply_coupon=promo5'>Click here to get 5% OFF</a></div>";
}
add_action( 'woocommerce_before_checkout_form', 'bonus' );

Related

How to add a custom shortcode below the product table on woocommerce checkout page

Problem:
I need to add a shortcode [wc_sc_available_coupons] below the product table on the checkout page.
I added the following code in functions.php
The problem is the shortcode displays at the very bottom of the checkout form.
I changed the number 10 to 120 but still the same.
Would you please let me know how to add the shortcode below the product table (=above the payment)?
Code I tried:
add_action( 'woocommerce_after_checkout_form', 'wnd_checkout_code', 10 );
function wnd_checkout_code( ) {
echo do_shortcode('[wc_sc_available_coupons]');
}
Thank you.
Would woocommerce_checkout_after_customer_details hook work for you? So your code would be something like this:
add_action( 'woocommerce_checkout_after_customer_details', 'wnd_checkout_code' );
function wnd_checkout_code( )
{
echo do_shortcode('[wc_sc_available_coupons]');
}
If not then you could try other hooks such as woocommerce_checkout_before_order_review or you could try this woocommerce_before_order_notes as well.
This one is right before the payment:
add_action( 'woocommerce_review_order_before_payment', 'wnd_checkout_code' );
function wnd_checkout_code( )
{
echo do_shortcode('[wc_sc_available_coupons]');
}
Use the action:
add_action("woocommerce_after_cart_table", $action)
OR
add_action("woocommerce_before_cart_collaterals", $action)
Result
https://imgur.com/a/zQVNedb
In general for WooCommerce you can find hook info here:
https://woocommerce.github.io/code-reference/hooks/hooks.html
and for your template:
https://woocommerce.github.io/code-reference/files/woocommerce-templates-cart-cart.html#source-view.159
You can see information on the actions allowed by looking at the source code in woocommerce/templates/ and checking the do_action() functions.

How do you update or override a non-pluggable function located in woocommerce wc-template-functions

I am building a theme in woocommerce and I have a need to make a small change to the html output by the woocommerce_output_all_notices() in wc-template-functions.php.
The existing function itself is like so:
function woocommerce_output_all_notices() {
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
What I'd like to do is simply add an id or data attribute and a <span> inside the div, but without editing the code directly (which I clearly don't want to do) I am stumped as it doesn't appear to be a pluggable function and I don't see a way that I can use a hook for this one.
Removing the action that executes the above function should work.
remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
then add a new action that returns what you need
function custom_woocommerce_output_all_notices() {
// Change your code here.
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
add_action( 'woocommerce_before_single_product', 'custom_woocommerce_output_all_notices', 10 );

How to add custom subject line using hooks in contact form 7 (wordpress)?

I am trying to set custom message on subject textbox after form load. So that I can set the custom values like customerID=1 and then wanted to place some if conditions by checking if cutomerID == 1 then add subject line "Hello john" and if customerID==2 then add subject line "hello james" on the form textbox
like here:
So in my wordpress site I wanted to add hook to customise subject line based on the condition but dont know which hook is appropriate to do this?
add_action( 'wpcf7_form_autocomplete', 'my_change_subject' );
function my_change_subject( $wf7 ) {
if($_GET['customerid'] == 1) {
<script>$("input[name='subject']").val('hello john');</script>
}
} ?>
Something like above but what hook I should use to do this?
I am also adding hooks site reference here: contact form7 hook reference site
Not sure if anyone comes to this issue before but I found simple solution;)
add_action( 'wpcf7_form_autocomplete', 'my_change_subject' );
function my_change_subject( $wf7 ) {
?>
<script>
jQuery(document).ready(function(){
//console.log('coming here...<?php echo $_GET['cid'];?>');
document.getElementsByName('your-subject')[0].value = "Customer Enquiry: <?php echo $_GET['cid'];?>";
});
</script>
<?php
}
} ?>

add image beside woocommerce product description in tab

I am trying to place an image next to the product description in the tabs of woocommerce. The code I am trying to use is this:
add_action('woocommerce_template_product_description', 'badge', 25);
function badge() {
echo '<div class="badge"><img src="URL" style="width:175px;height:175px;"></div>';
}
I have not had any luck in getting it to display, and am thinking that the 'woocommerce_template_product_description' maybe the problem - I'm sure if I am targeting the product description correctly. I think tabs are increasing the complexity of the solution.
Using the code below the image displays next to the product summary.
add_action('woocommerce_single_product_summary', 'badge', 25);
function badge() {
echo '<div class="badge"><img src="URL" style="width:175px;height:175px;"></div>';
}
I would appreciate some help is solving this problem.
After much head scratching I think I might have figured it out. This code seems to work:
add_filter( 'woocommerce_product_tabs', 'magva_badge', 98 );
function magva_badge( $tabs ) {
$tabs['description']['callback'] = 'woo_custom_description_tab_content'; // Custom description callback
return $tabs;
}
function woo_custom_description_tab_content() {
woocommerce_product_description_tab(); echo '<div class="magva_badge"><img src="https://www.magva.com/wp-content/uploads/2015/10/made_in_ireland.png" style="width:175px;height:175px;"></div>';
}

reCAPTCHA doesn't render when placed in certain places on the page

I'm trying to add a reCAPTCHA to WooCommerce Checkout on WordPress. When I use the following snippet the captcha loads and works correctly:
add_action( 'woocommerce_review_order_before_payment', 'woocommerce_checkout_recaptcha_field' );
function woocommerce_checkout_recaptcha_field( ) {
echo '<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXX"></div>';
}
However as soon as I move it next to the terms and conditions checkbox:
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'woocommerce_checkout_recaptcha_field' );
function woocommerce_checkout_recaptcha_field( ) {
echo '<div class="g-recaptcha" data-sitekey="XXXXXXXXXXXX"></div>';
}
The captcha stops loading. I can see the captcha beginning to load as the page makes a gap for it but then after a split second it disappears again.
Anyone got any ideas?
Thanks

Categories