Wordpress button with dynamic url - php

I have added a custom button to my woocomerce product pages using the following code.
add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
global $product;
echo 'Extra Button';
}
I would like to build the buttons url dynamically using the
get_option('myplugin_option_name')
I'm hoping this will be possible.
'myplugin_option_name' is a custom set value via a simple plugin that adds the field to the admin options. The plugin works and I can display whatever is set in the backend on the front end using a simple :
<?php echo get_option('myplugin_option_name'); ?>
The question is how do I get the value from myplugin_option_name to be added to the button url?
So for example if 'myplugin_option_name' = buy the button url should be generated as such :
http://sample.com/buy/product_id
Any help in the right direction would be appreciated.
Thank you!

add_action( 'woocommerce_single_product_summary', 'my_extra_button_on_product_page', 30 );
function my_extra_button_on_product_page() {
global $product;
$url_part = get_option('myplugin_option_name');
$id = $product->get_id();
$url = home_url("/".$url_part."/".$id);
echo 'Extra Button';
}

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 to hook an ACF field into the bottom of the "after_shop_loop" section of a Woocommerce category page after pagination

I found the correct hook of where I want the field to show which is below
add_action( 'woocommerce_after_shop_loop', 'after_shop_loop', 1 );
This code works exactly how'd I'd like mine to work but with an ACF text area field. Ideally, I'd like it to show under the pagination. This hook is currently showing above the pagination.
add_action( 'woocommerce_after_shop_loop', 'after_shop_loop', 1 );
function after_shop_loop() {
echo "<div class='best-seller'>
<h3>If you need help or have a question for Customer Service, please visit the <a href='#'>Help Section.</a></h3>
</div";
}
I'd like to create a function to bring in an ACF text area that I've added to the backend for all brands that are coming from WooCommerce taxonomy pages "Brand".
This is what I have so far with no luck. Is there something I'm doing wrong or something special you have to do to get ACF to show on Taxonomy pages? Any help is appreciated. Thanks in advance...
add_action( 'woocommerce_after_shop_loop', 'after_shop_loop', 1 );
function custom_after_shop_loop() {
$brand_content = get_field('brand_bottom');
if( !empty($brand_content) ) {
echo '<div class="brandbottom">' . $brand_content . '</div>';
}
}
Screenshots from my custom field.
Missing "custom_" in the name of your function and you have to get the current taxonomy term. Tested and works
add_action( 'woocommerce_after_shop_loop', 'custom_after_shop_loop', 1 );
function custom_after_shop_loop() {
$term = get_queried_object();
$brand_content = get_field('brand_bottom', $term);
if( !empty($brand_content) ) {
echo '<div class="brandbottom">' . $brand_content . '</div>';
}
}

WooCommerce "CONTACT" button when out of stock

Trying to achieve something that should be simple, but I've tried 3 approaches with multiple code variations and I just can't make it work. I'm trying to create a button that will appear in place of the "ADD TO CART" button on single product pages when the item is out of stock. Clicking the button will fire a popup contact form.
Is creating an add action in functions the right way to go, or should I replace the normal button with an if statement? I've tried both, so help with coding either would be greatly appreciated.
You can either hook into woocommerce_loop_add_to_cart_args using a filter in your functions.php or edit the template file directly by pulling it into your theme. Either way will require a bit of PHP.
If doing it in your functions.php, it would look something like this (untested but should send you down the right path):
<?php
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_out_of_stock_button' );
function my_out_of_stock_button( $args ){
global $product;
if( $product && !$product->is_in_stock() ){
return 'Contact us';
}
return $args;
}
I don't know what your button code should actually look like or what other information you need to capture, but this is how you could override the "Add to Cart" button and replace it if out of stock.
UPDATE
LoicTheAztec brought up a great point - the filter provided only affects the button on the archive, category, tag overview pages - not the individual product pages. There are no hooks for the individual product page buttons BUT you can copy the templates to your theme and override them.
You'll want to look at the files in templates/single-product/add-to-cart. Use a similar if statement as above:
#simple.php
<?php if ( $product->is_in_stock() ) : ?>
// Standard WooCommerce code
<?php else: ?>
// Your button code
<?php endif; ?>
Just add below code in functions.php file of your enabled theme reference
add_action('woocommerce_after_shop_loop_item', 'themelocation_change_outofstock_to_contact_us', 1);
// for shop page
function themelocation_change_outofstock_to_contact_us() {
global $product;
if (!$product->is_in_stock()) {
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
//change the link to your contact us page
echo ' Contact Us ';
}
}
// for single page
add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability($availability, $_product) {
// Change In Stock Text
if ($_product->is_in_stock()) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if (!$_product->is_in_stock()) {
$availability['availability'] = __(' Contact Us ', 'woocommerce');
}
return $availability;
}
I was looking for a way to show a contact button on bespoke products and
#ahwychkchih solution works great. One issue I had though is that schema markup will show as out of stock for those products which is not the case for beskpoke products is just they can't be purchased straight away so I've added this to force in_stock markup for my products. I'm aware that this solution would affect all products so you can always add a product id filter if needed
// Force In Stock schema markup
function fix_my_product_offers_schema ($markup_offer, $product) {
if (!$product->is_in_stock()) {
$markup_offer['availability'] = 'https://schema.org/InStock';
}
return $markup_offer;
}
add_filter('woocommerce_structured_data_product_offer', 'fix_my_product_offers_schema', 1, 2);

Displaying Advanced Custom Field (ACF) value in a WooCommerce hook

I want to display the value from an Advanced Custom Field (ACF) in Woocommerce and I am using this code in this function hooked:
add_action( 'woocommerce_single_product_summary', 'charting', 20 );
function charting() {
if( get_field('size_chart') ) {
echo '<p>size guide</p>';
}
return;
}
But it is not working, it is displaying the custom field value above the href (size guide) and the href is empty like this:
size guide
Your problem is that you can't use echo with ACF the_field('my_field') , because when using the_field('my_field') is just like using echo get_field('my_field'), so you are trying to echo an echo. Instead use get_field('my_field') this way in your code:
add_action( 'woocommerce_single_product_summary', 'charting', 20 );
function charting() {
if( !empty( get_field('size_chart') ) ) { // if your custom field is not empty…
echo '<p>size guide</p>';
}
return;
}
After, I have add empty() function in your condition…
You can also try to return it instead of echo:
return '<p>size guide</p>';
Reference:
ACF the_field
ACF get_field
I used this code and it worked well in local but when i upload function file in server it doesn,t work and it gives server error 500, so I had to remove this code again

Only Add Filter / Function on Certain Pages

I have want to add the following code to my functions.php in WordPress:
add_filter( 'gform_submit_button_4', 'form_submit_button', 10, 2 );
function form_submit_button( $button, $form ) {
return "<button class='button' id='gform_submit_button_{$form['id']}'>Get a Test Drive</button>";
}
But, I only want to apply it on certain pages, I've tried two avenues: a if statement using is_page & wrapping the gravity forms shortcode in a div with a class and targeting only .class #gform_submit_button_4 so far I haven't been able to get either to work.
Any help would be greatly appreciated.
Thanks
Willem
Try to use get_queried_object function,
<?php
function form_submit_button( $button, $form ) {
return "<button class='button' id='gform_submit_button_{$form['id']}'>Get a Test Drive</button>";
}
$queried_object = get_queried_object();
if ( $queried_object ) {
$pageId = $queried_object->ID;
if($pageId == 'someid') {
add_filter( 'gform_submit_button_4', 'form_submit_button', 10, 2 );
}
}
?>
In gravity form add a field of page slug with type hidden from back-end option of gravity form.
1) You can create field from back-end and assign it a value with JS
2) Gravity form give you an option to send url with form you can parse page slug from that url
add_filter( 'gform_submit_button_4', 'form_submit_button', 10, 2 );
function form_submit_button( $button, $form ) {
// please print $form and check you get your current page slug variable e.g $form['my_page_slug']
// create an array that have your new button slugs e.g
$pages_slug_array = array('about','contact-us');
if(in_array($page_slug_arry, $form['my_page_slug'])){
return $button = "<button class='button' id='gform_submit_button_{$form['id']}'>Get a Test Drive</button>";
}
}
Hope you got that

Categories