How to skip cart page on woocomerce for certain products only?

后端 未结 3 1119
忘掉有多难
忘掉有多难 2021-01-19 07:16

I added this to my functions.php file :

add_filter (\'woocommerce_add_to_cart_redirect\', \'woo_redirect_to_checkout\');
function woo_redirect_to_checkout()          


        
3条回答
  •  悲&欢浪女
    2021-01-19 07:47

    I wrote a little plugin for this, sharing it here. The plugin adds a small checkbox to the product metabox, so you can specify which products should trigger the automatic skip to checkout. Basically using the same woocommerce_add_to_cart_redirect filter as in the other answers, but providing the admin backend option to determine which products trigger the redirection.

    ';
    
        // Suggested Price
        echo woocommerce_wp_checkbox( array(
            'id' => '_redirect_to_checkout',
            'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
            'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
            )
        );
    
        echo '
    '; } add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' ); /* * Save extra meta info */ function kia_process_wc_meta_box( $post_id, $post ) { if ( isset( $_POST['_redirect_to_checkout'] ) ) { update_post_meta( $post_id, '_redirect_to_checkout', 'yes' ); } else { update_post_meta( $post_id, '_redirect_to_checkout', 'no' ); } } add_action( 'woocommerce_process_product_meta', 'kia_process_wc_meta_box', 1, 2 ); /* * Redirect to checkout */ function kia_add_to_cart_redirect( $url ){ // If product is one of our special types if ( is_numeric( $_REQUEST['add-to-cart'] ) && kia_maybe_redirect_cart( (int) $_REQUEST['add-to-cart'] ) ) { // Remove default cart message WC()->clear_messages(); // Redirect to checkout $url = WC()->cart->get_checkout_url(); } return $url; } add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect' ); /* * check if an item has custom field */ function kia_maybe_redirect_cart( $product_id ){ if ( 'yes' == get_post_meta( $product_id, '_redirect_to_checkout', true ) ){ return TRUE; } else { return false; } }

    Updating WooCommerce 3.0+

    ';
    
        // Suggested Price
        echo woocommerce_wp_checkbox( array(
            'id' => '_redirect_to_checkout',
            'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
            'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
            )
        );
    
        echo '
'; } add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' ); /** * Save extra meta info * * @param WC_Product $product */ function kia_process_wc_meta_box( $product ) { if ( isset( $_POST['_redirect_to_checkout'] ) ) { $product->update_meta_data( '_redirect_to_checkout', 'yes' ); } else { $product->update_meta_data( '_redirect_to_checkout', 'no' ); } } add_action( 'woocommerce_admin_process_product_object', 'kia_process_wc_meta_box' ); /** * Redirect to checkout * * @param WC_Product $product */ function kia_add_to_cart_redirect( $url, $product ) { // If product is one of our special products. if ( kia_maybe_redirect_cart( $product ) ) { // Remove default cart message. wc_clear_notices(); // Redirect to checkout. $url = wc_get_checkout_url(); } return $url; } add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect', 10, 2 ); /** * Check if an item has custom field. * * @param WC_Product $product */ function kia_maybe_redirect_cart( $product ) { return wc_string_to_bool( $product instanceof WC_Product && $product->get_meta( '_redirect_to_checkout', true ) ); }

https://gist.github.com/helgatheviking/f76b97d7d19813538e32b8f5f2dae6ec

提交回复
热议问题