Wordpress Gravity form - Link to Add to Cart - php

I'm using Gravity Forms plugin in Wordpress. In their documentation, it is mentioned that in order to append javascript on form button, I need to use the following code:
add_filter( 'gform_submit_button', 'add_onclick', 10, 2 );
function add_onclick( $button, $form ) {
$dom = new DOMDocument();
$dom->loadHTML( $button );
$input = $dom->getElementsByTagName( 'input' )->item(0);
$onclick = $input->getAttribute( 'onclick' );
$onclick .= " addAdditionalAction('Additional Action');";
$input->setAttribute( 'onclick', $onclick );
return $dom->saveHtml( $input );
}
Now, I want to add Woocommerce Add to Cart code to the said button. From another stackoverflow question I got the following code:
global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )
),
$product );
I want to append the php script code in place of javascript code above. Please help how can I do the same.

Related

How to fix quantity input field from affecting other quantity input fields on single product page woocommerce?

We have added the code below to our functions.php file in Woocommerce. The goal was to replace the default Woocommerce "Add to Cart" button with a quantity selector and a cart icon that would act as the "Add to Cart" button. So far it has worked on every page as needed except for the single product page.
On our product page we have a quantity selector and an "add to cart" button that affects the product listed and below it is a list of 4 related products that each have their own quantity selector.
Here is an example
The issue that we are having is that if you change any of the quantities, all other quantities are changed as well.
/*Adds Quantity to product archive*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 );
function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
// Get the necessary classes
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
// Adding embeding <form> tag and the quantity field
$html = sprintf( '%s%s%s%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>%s',
'<form class="cart">',
'<div class="form_cart_first_half">',
woocommerce_quantity_input( array(), $product, false ),
'</div>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
'<button type="submit" class="button alt" id="pa_add_to_cart_button">' . sprintf('<i class="fas fa-shopping-cart" id="pa_add_to_cart_button_icon" style="font-size:24px; display: inline-block;"></i>') . '</button>',
'</form>'
);
}
return $html;
}
If anyone has any suggestions on how to fix this I would appreciate it.
Thank you for your time.

Is there a way I can make woocommerce_loop_add_to_cart_link not change url address but still add product to cart?

Is there a way I can still add/remove a product to my cart without the page url changing?
I have custom add to cart button on my custom Wordpress theme but as I add product to cart the url changes to http://localhost/mytheme/?add-to-cart=15. Is there a way I can avoid it and still add product to the cart?
<?php global $product;
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( 'Add product',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->get_type() ),
esc_html( $product->add_to_cart_text() )
),
$product ); ?>
WooCommerce has a hook woocommerce_add_to_cart_redirect which is called when you add a product to your cart.
There are two arguments which you get $url where it is redirecting and $product which was added to your cart.
add_filter( 'woocommerce_add_to_cart_redirect', 'bks_add_to_cart_redirect', 10, 2 );
function bks_add_to_cart_redirect( $url, $product ) {
if ( $product && is_a( $product, 'WC_Product' ) ) {
// Do any change or URL you want to $url.
}
return $url;
}
You can change the logic and redirect it back to the product page by changing $url inside if like so.
$url = esc_url($product->get_permalink());
You can add paramters to it.
$url = esc_url( add_query_arg('name', 'value', $product->get_permalink() ) );
-- OR --
Just redirect it to a custom page.
$url = esc_url( home_url() . '/custom_page' );

Open External WooCommerce Products in New Tabs - Adding Attribute target="_blank"

I have managed to add attribute target="_blank" to my external product pages but I can't seem to change the links on the parent (grouped product) pages.
I was able to do this by modifying external.php and just adding the tag to the actual link itself.
<p class="cart">
<?php sdnetwork(); sdcondition(); parent_permalink_button(); ?><img src="/wp-content/themes/wootique-child/images/icons/new_tab_icon.gif" alt="Opens in New Tab"> <?php echo esc_html( $button_text ); ?>
</p>
How can I change the links on the parent page of grouped products to add this attribute, my first thought was to modify grouped.php but the link is generated differently.
<?php woocommerce_template_loop_add_to_cart(); ?>
How can I possibly add my tag to the link that is generated above? I've thought about using a hook but I need some help.
EDIT:
Just wondering if I could use jQuery like so.....
jQuery(document).ready(function($) {
$(".button.product_type_external").each(function() {
$(this).find("a").attr("target", "_blank");
});
});
The problem is most of the links are hidden when the page is loaded and I'm worried this may take up alot of resources or will it? Quite new to jQuery.
http://mobilereactor.co.uk/shop/mobile-phones/sony-xperia-z5-compact-coral-deals/
EDIT Solved thanks to cale_b:
add_filter( 'woocommerce_loop_add_to_cart_link', 'add_target_blank', 10, 2 );
function add_target_blank( $link, $product ){
global $post;
$product = get_product( $post->ID );
if( $product->is_type( 'external' ) ){
// I simply added target="_blank" in the line below
$link = sprintf( '<a rel="nofollow" href="%s" target="_blank" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
return $link;
} else {
// I simply remove target="_blank" in the line below
$link = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
return $link;
}
}
Here is a cleaner way to add target="_blank" to add_to_cart links to open them in new tab:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
If you trace the code, that function does some things, and then loads the template loop/add-to-cart.php.
If you open loop/add-to-cart.php, you'll find code that should look something like this:
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
To modify the link, use the filter (woocommerce_loop_add_to_cart_link).
Note: never modify the WooCommerce templates directly in the plugin folder. Use their excellent Template Structure to copy the template to your theme and modify it there, otherwise your changes will get lost the next time you update WooCommerce.
Final note: tracing the code is easy and fun to do when you use a good IDE. I for one am a huge fan of PHPStorm (https://www.jetbrains.com/phpstorm/).
EDIT
To only add it for external products, then you would take a different approach.
You would leverage the filter, and as you mention in the comments, write a function in your functions.php that does something like this:
add_filter('woocommerce_loop_add_to_cart_link', 'my_external_product_links', 10, 2);
function my_external_product_links( $link, $product ) {
// Set up the $target variable to contain the correct text depending on the product
$target = ( 'external' == $product->product_type ) ? 'target="_blank"' : '';
// Use the code from the core function here, but with our modification to include target
echo sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() ),
$target
);
}
In the code above, pay close attention to the new %s in the sprintf statement:
// Watch for this --->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->--->-->--->--->-vv
'<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s" %s>

Change "add to cart" text to the price of the product

I want to change the text of my "add-to-cart" button in woocommerce to the current price of that product. With the code below I can change the text. This works fine.
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text');
function woo_custom_cart_button_text($button_text) {
$button_text -> 'hello!';
return __($button_text, 'woocommerce' );
}
But now I want to change that text in the price of the product. I've tried the following code, replacing line 3 with:
$button_text->get_price_html();
But that gives me an error. Any idea's how to solve this problem?
You need to pass the $product to your function, by setting the number of accepted arguments to 2 in apply_filters().
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text', 10, 2);
function woo_custom_cart_button_text($button_text, $product) {
return $product->get_price_html();
}
However, this results in a problem because the template that displays the link is using esc_html() to wrap the resulting text which causes this hot mess (because the price html has html tags in it)
Therefore, I think it might be better to filter the link itself:
add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_custom_cart_button_link', 10, 2);
function woo_custom_cart_button_link($button_text, $product) {
return sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
$product->get_price_html()
);
}
Try...
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
$button_text = get_price_html();
return __( $button_text, 'woocommerce' );
}
You may need to assign $button_text with the assignment opperator (=).
Or...
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
global $post;
$product = get_product( $post->ID );
$button_text = $product->get_price_html();
return __( $button_text, 'woocommerce' );
}

Woocommerce - Displaying variations inside the loop

<?php
switch ( $product->product_type ) {
case "variable" :
$link = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$label = apply_filters( 'variable_add_to_cart_text', __('Select options', 'woocommerce') );
break;
case "grouped" :
$link = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
$label = apply_filters( 'grouped_add_to_cart_text', __('View options', 'woocommerce') );
break;
case "external" :
$link = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
$label = apply_filters( 'external_add_to_cart_text', __('Read More', 'woocommerce') );
break;
default :
$link = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
$label = apply_filters( 'add_to_cart_text', __('Add to cart', 'woocommerce') );
break;
}
printf('%s', $link, $product->id, $product->product_type, $label);
?>
I'm trying to get variations to display inside the loop so customers can add variable products to their cart from the shop page (please see screenshot below)...
http://cl.ly/image/42401k0X0X2I
I know I need to include the function-
get_available_variations();
And i'm pretty sure this already returns an array, it's just putting that array into a select dropdown + listing the variations (S,M,L,XL) and having a link to add that variation to the basket.
Cheers!
I found your post while trying to solve the same problem. I finally found...
function woocommerce_variable_add_to_cart() {
global $product;
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Load the template
woocommerce_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
}
}
in
woocommerce-template.php
This works for me in loop/add-to-cart.php
switch ( $product->product_type ) {
case "variable" :
$link = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$label = woocommerce_variable_add_to_cart();
break;
Let me know if this helps :)
The variations dropdown template file for single post pages is located here:
woocommerce\templates\single-product\add-to-cart\variable.php
Which requires the following script to pass the product variable information:
<script type="text/javascript">
var product_variations_<?php echo $post->ID; ?> = <?php echo json_encode( $available_variations ) ?>;
</script>
as well as the following hidden field:
<input type="hidden" name="variation_id" value="" /> - where the value is the variation ID
I hope that is a start others can help build upon.
I found on Remi Corson's blog a simple way to do that.
Change the href value of the cart button with the following:
http://example.com/cart/?add-to-cart=[PRODUCT_ID]&variation_id=[VARIATION_ID]&attribute_pa_[ATTRIBUTE_SLUG]=[ATTRIBUTE_SLUG_VALUE]
Example:
http://example.com/cart/?add-to-cart=123&variation_id=456&attribute_pa_colour=black
With the get_available_variations(); function is easy to get the variation values. For the product ID, you can use get_the_ID(); function.

Categories