Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Custom add to cart button for WooCommerce.
- * This version is compatible with gtm4wp plugin.
- */
- add_shortcode( 'gtm4wp_add_to_cart', 'gtm4wp_add_to_cart_shortcode' );
- function gtm4wp_add_to_cart_shortcode( $atts ) {
- $a = shortcode_atts( array(
- 'id' => '',
- 'quantity' => 1,
- 'text' => __( 'Add to cart', 'woocommerce' )
- ), $atts );
- if( ! $a['id'] ) {
- return "{ error: missing shortcode arguments }";
- }
- global $product;
- $product = new WC_Product( intval( $a['id'] ) );
- ob_start();
- ?>
- <form class="cart" action="<?php echo esc_url( wc_get_cart_url() . "?add-to-cart={$product->id}&quantity={$a['quantity']}" ); ?>" method="post" enctype="multipart/form-data">
- <button type="submit" name="add-to-cart" value="<?php echo intval( $a['id'] ); ?>" class="single_add_to_cart_button button alt">
- <?php esc_html_e( $a['text'] ); ?>
- </button>
- <input type="hidden" name="quantity" value="<?php echo intval( $a['quantity'] ); ?>">
- <?php echo do_action( 'woocommerce_after_add_to_cart_button' ); ?>
- </form>
- <?php
- return ob_get_clean();
- }
Add Comment
Please, Sign In to add comment