Last active
July 27, 2020 15:11
-
-
Save crawford252/103fd96f0ef74940e8ec17d10664f80f to your computer and use it in GitHub Desktop.
Divi FilterGrid - WooCommerce Add to Cart Button w/quantity Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DFG CUSTOM CONTENT FILTER | |
function dpdfg_after_read_more_custom_content($content, $props){ | |
// Apply this only to Divi FilterGrid modules with class "products" set in the module's Advanced tab | |
if (isset($props['module_class']) && $props['module_class'] === 'products') { | |
global $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 et_pb_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' : '', | |
) ) ); | |
// Embedding the quantity field to Ajax add to cart button | |
$html = sprintf( '%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>', | |
woocommerce_quantity_input( array(), $product, false ), | |
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' ), | |
esc_html( $product->add_to_cart_text() ) | |
); | |
} | |
if(!empty($html)) { | |
return $html; | |
} | |
} | |
} | |
add_filter('dpdfg_after_read_more', 'dpdfg_after_read_more_custom_content', 10, 2); | |
function dfg_quantity_fields(){ | |
?> | |
<script type='text/javascript'> | |
jQuery(function($){ | |
// Update data-quantity | |
$('.dp-dfg-item').on('click input', 'input.qty', function() { | |
$(this).parent().parent().find('a.ajax_add_to_cart').attr('data-quantity', $(this).val()); | |
// (optional) Removing other previous "view cart" buttons | |
$(".added_to_cart").remove(); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'wp_footer' , 'dfg_quantity_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment