Skip to content

Instantly share code, notes, and snippets.

@Nicscott01
Last active November 2, 2023 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicscott01/3284a8731d3dd918463579f77b43a81a to your computer and use it in GitHub Desktop.
Save Nicscott01/3284a8731d3dd918463579f77b43a81a to your computer and use it in GitHub Desktop.
Better Way to Do WooCommerce Smart Coupon Add to Cart/Select Options button
add_action( 'init', function() {
//Saftey check
if ( !class_exists( 'WC_SC_Display_Coupons' ) ) {
return;
}
$WC_SC_Display_Coupons = WC_SC_Display_Coupons::get_instance();
remove_action( 'woocommerce_after_shop_loop_item', array( $WC_SC_Display_Coupons, 'remove_add_to_cart_button_from_shop_page' ) );
});
add_filter( 'woocommerce_loop_add_to_cart_link', function( $html, $product, $args ) {
global $product;
//Saftey check
if ( !class_exists( 'WC_SC_Display_Coupons' ) ) {
return;
}
$WC_SC_Display_Coupons = WC_SC_Display_Coupons::get_instance();
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
if ( $WC_SC_Display_Coupons->is_wc_gte_30() ) {
$product_id = ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0;
} else {
$product_id = ( ! empty( $product->id ) ) ? $product->id : 0;
}
$coupons = $WC_SC_Display_Coupons->get_coupon_titles( array( 'product_object' => $product ) );
if ( ! empty( $coupons ) && $WC_SC_Display_Coupons->is_coupon_amount_pick_from_product_price( $coupons ) && ! ( $product->get_price() > 0 ) ) {
//Use the WooCommerce markup as a starting point
$html = sprintf(
'<a href="%s" class="%s">%s</a>',
esc_url( $product->get_permalink() ), //Use the permalink instead of $product->add_to_cart_url()
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
esc_html( get_option( 'sc_gift_certificate_shop_loop_button_text', __( 'Select options', 'woocommerce-smart-coupons' ) ) )
);
}
return $html;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment