<?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.
Related
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' );
I am using the code generated based on Add a custom multi-select field to admin product options settings in Woocommerce
// Display a multiselect field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_handles_product_field' );
function display_handles_product_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="handles_product"><?php _e( 'Handles Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" id="handles_product_ids" name="_handles_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_handles_product_ids' );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_handles_product_field_value', 10, 1 );
function save_handles_product_field_value( $product ){
$data = isset( $_POST['_handles_product_ids'] ) ? array_map( 'intval', (array) $_POST['_handles_product_ids'] ) : array();
$product->update_meta_data( '_handles_product_ids', $data );
}
This code adds a custom search box and adds related products in the admin area when creating or editing a product.
I also created a new tab on the product page to show the added products.
/* New Handles Product Tab */
add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' );
function new_handles_product_tab( $tabs ) {
/* Add new tab */
$tabs['new_handles_product_tab'] = array(
'title' => __( 'Handles Product', 'woocommerce' ),
'priority' => 50,
'callback' => 'new_handles_product_tab_content'
);
return $tabs;
}
/* Display content in new tab */
function new_handles_product_tab_content() {
// Content
}
Tell me how to add these products to the new tab correctly? And is my code correct?
I will be glad for your help!
Using [products] shortcode with ids argument, the function displaying the product handles in a custom product tab will be:
function new_handles_product_tab_content() {
global $product;
$product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles
if( ! empty($product_ids) )
$product_ids = implode(',', $product_ids);
echo do_shortcode( "[products ids='$product_ids']" ); // Using [products] shortcode for display.
}
Untested it should work.
I have an icon (instead of text) in my add to cart button. I added in the add-to-cart.php file an icon class to the add-to-cart anchor, as you can see here:
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 fa fa-cart-plus">%s</a>'
and I would like to change his color in case the item is already in cart.
I have a function to get an add-to-cart button without any text, and as well I get the items id that are in cart:
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->id ) {
return __('', 'woocommerce');
}
}
return __( '', 'woocommerce' );
which is great, but for some reason I can't think of a way to use this new information with changing the icon style. I've tried to 'echo' a new style, but I don't know how to relate to the product id in css (or in jquery). Any ideas?
*There might be a different way to do this. When an item is added to cart, the add-to-cart anchor has a new class- 'added', so I can easily customize it with css, and it works, but the 'added' class is there only when the item is being added. after refreshing the page it doesn't exist, therefore the customize I've done doesn't affect anymore.
You should need to make some changes in the template loop/add-to-cart.php directly has you have already done, replacing them by:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
// Your icon class is now here
$add_class = ' fa fa-cart-plus';
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item )
// If the product is in cart
if( $product->get_id() == $cart_item['product_id'] ) {
$add_class .= ' is-added'; // We add an additional class
break;
}
$add_to_cart_text = '';
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->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ) . $add_class,
$add_to_cart_text
),
$product );
This will add an additional is-added to your button, that you will be able to target with your CSS to make a color change. This is tested and works.
You will not need anymore your function woo_archive_custom_cart_button_text()…
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.
WP: 3.71
Theme: Jupiter (artbees) v.3.02
URL: http://thefastlearners.com/store/
Hi all,
I have some Woocommerce products that are external linking out to amazon or other sites.
My goal is to link these products to their external pages directly from the store page, without the detailpage!
Already tried the visibility option extension which didn’t change a thing.
Here is the original content-product.php excerpt:
`
$mk_add_to_cart = 'id ) ).'" class="add_to_cart_button">'. apply_filters( 'out_of_stock_add_to_cart_text', ( 'READ MORE', 'woocommerce' ) ).'';
$out_of_stock_badge = ''.( 'OUT OF STOCK', 'woocommerce' ).'';
}
else { ?>
switch ( $product->product_type ) {
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' ) );
$icon_class = 'mk-moon-search-3';
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' ) );
$icon_class = 'mk-moon-cart-plus';
break;
}
if ( $product->product_type != 'external' ) {
$mk_add_to_cart = '<i class="'.$icon_class.'"></i>'. $label.'';
}
else {
$mk_add_to_cart = '';
}
}`
Which I changed to
´<?php
$mk_add_to_cart = '**;';
$out_of_stock_badge = '<span class="mk-out-stock">'.__( 'OUT OF STOCK', 'woocommerce' ).'</span>';
}
else { ?>
switch ( $product->product_type ) {
case "external" :
$link = apply_filters( 'external_add_to_cart_url', esc_url( $product_url ) );
$label = apply_filters( 'external_add_to_cart_text', __('Read More', 'woocommerce') );
$icon_class = 'mk-moon-search-3';
break;
default :
$link = apply_filters( 'add_to_cart_url', esc_url( $product_url ) );
$label = apply_filters( 'add_to_cart_text', __( 'ADD TO CART', 'woocommerce' ) );
$icon_class = 'mk-moon-cart-plus';
break;
}
if ( $product->product_type != 'external' ) {
$mk_add_to_cart = '<i class="'.$icon_class.'"></i>'. $label.'';
}
else {
$mk_add_to_cart = '';
}`
Without any results except that I don’t get any button to add anything.
from the external.php template I already figured out that the command should be something like
<?php echo esc_url( $product_url ); ?>
but I don’t get where to put/link it.
Would be wonderful if you can help me out here. Once I got that working I can change the links of the title and image myself.
Thanks,
Mattis
You should try $product->product_url instead of plain $product_url.
That worked for me on the content-product.php page.