Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active April 11, 2023 13:51
Show Gist options
  • Select an option

  • Save Acephalia/6d63e5cc4e8edf975367a249249fa76d to your computer and use it in GitHub Desktop.

Select an option

Save Acephalia/6d63e5cc4e8edf975367a249249fa76d to your computer and use it in GitHub Desktop.
Woocommerce Direct Checkout
//Skip Cart & Redirect To Checkout
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );
function custom_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
// Add quantity field to checkout page
add_action( 'woocommerce_checkout_before_order_review', 'add_quantity_field_to_checkout' );
function add_quantity_field_to_checkout() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
?>
<div class="quantity">
<label class="screen-reader-text" for="quantity_<?php echo $cart_item_key; ?>"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></label>
<input type="number" id="quantity_<?php echo $cart_item_key; ?>" class="input-text qty text" step="1" min="1" name="cart[<?php echo $cart_item_key; ?>][qty]" value="<?php echo $cart_item['quantity']; ?>" title="<?php esc_attr_e( 'Qty', 'woocommerce' ); ?>" size="4" inputmode="numeric" />
</div>
<?php
}
}
// Update cart item quantity on checkout update
add_action( 'woocommerce_checkout_update_order_review', 'update_cart_item_quantity_on_checkout' );
function update_cart_item_quantity_on_checkout( $post_data ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( isset( $post_data['cart'][$cart_item_key]['qty'] ) ) {
$quantity = wc_stock_amount( sanitize_text_field( $post_data['cart'][$cart_item_key]['qty'] ) );
WC()->cart->set_quantity( $cart_item_key, $quantity, false );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment