Skip to content

Instantly share code, notes, and snippets.

@homu9
Last active November 11, 2022 02:25
Show Gist options
  • Save homu9/7420a4963bfc100230e5ffbc975f1457 to your computer and use it in GitHub Desktop.
Save homu9/7420a4963bfc100230e5ffbc975f1457 to your computer and use it in GitHub Desktop.
Add 'Buy Now' button to WooCommerce Single Product Page
function custom_wc_add_buy_now_button_single_before() {
echo '<div id="cart-action-wrapper">';
}
add_action( 'woocommerce_before_add_to_cart_button', 'custom_wc_add_buy_now_button_single_before' );
function custom_wc_add_buy_now_button_single_after()
{
global $product;
$current_product_id = get_the_ID();
$product = wc_get_product( $current_product_id );
$checkout_url = WC()->cart->get_checkout_url();
if( $product->is_type( 'simple' ) ){
echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="buy-now button">Buy Now</a></div>';
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'custom_wc_add_buy_now_button_single_after' );
$primary-color: #39ba78;
$button-hover-color: #3a3a3a;
#cart-action-wrapper {
align-items: flex-start;
display: flex;
.single_add_to_cart_button {
border: 1px solid $primary-color;
&:hover {
border-color: $button-hover-color;
}
}
.buy-now {
background-color: #fff;
border: 1px solid $primary-color;
color: $primary-color;
line-height: 1em;
margin-left: 1em;
padding: 10px 20px;
&:hover {
background-color: $primary-color;
color: #fff;
}
}
}
@media (max-width: 479.98px) {
#cart-action-wrapper {
flex-direction: column;
.buy-now {
margin: 0 0 1em 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment