Created
March 28, 2024 07:19
-
-
Save tdmrhn/f2b162471f89e47ec317a1fbb3e275f5 to your computer and use it in GitHub Desktop.
Blocksy 2 Add Quantity + Add to Card button to Product Cards via Content Blocks Element
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
<?php | |
// Add this to Content Blocks > Hook > No Condition or Location | |
// Call it through Customizer > Product Archives > Card Options > Enable Content Block Element > Select the Hook name in drop down | |
if ( is_product() || is_shop() || is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) { | |
global $product; | |
if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() && $product->is_in_stock() ) { | |
echo '<div class="dhn-atc-wrap">'; | |
woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) ); | |
echo '<a href="' . esc_url( $product->add_to_cart_url() ) . '" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="' . esc_attr( $product->get_id() ) . '" aria-label="' . esc_attr( sprintf( __( 'Add to cart: %s', 'woocommerce' ), $product->get_name() ) ) . '" aria-describedby="" rel="nofollow">' . esc_html( $product->single_add_to_cart_text() ) . '</a>'; | |
echo '</div>'; | |
} | |
add_action( 'wp_footer', function () { | |
wc_enqueue_js( 'jQuery( "body" ).on( "click", ".quantity input", function() { return false; });jQuery( "body" ).on( "change input", ".quantity .qty", function() {var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );add_to_cart_button.attr( "data-quantity", jQuery( this ).val() );add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );});' ); | |
}); | |
} | |
?> | |
// Also add this to Customizer > Additional CSS; | |
.dhn-atc-wrap {display: flex;gap:10px;margin:10px auto;--quantity-height:40px;--quantity-width:90px;}.dhn-atc-wrap a{flex-grow:1}.dhn-atc-wrap .added_to_cart{display:none} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment