将特定的woo-commerce产品“添加到购物车”按钮更改为“立即购买”

时间:2018-03-23 13:19:18

标签: wordpress woocommerce hook-woocommerce

我想将特定产品(product_id = 13528)“添加到购物车”按钮更改为“立即购买”。点击“立即购买”按钮应跳过购物车并跳转到结帐页面。

请帮忙!

对不起所有的拼写错误。

1 个答案:

答案 0 :(得分:2)

这将重定向到特定产品的结帐:

add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout($url) {
    global $woocommerce;
    if ( isset( $_POST['add-to-cart'] ) ) {
        $product_id = (int)$_POST['add-to-cart'];
        if($product_id == 13528){
            $url = $woocommerce->cart->get_checkout_url();
        }
    }
    return $url;
}

这将更改添加到购物车文字:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_cart_button_text' ); 
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_cart_button_text' ); 

function custom_cart_button_text($text) {
    global $woocommerce;
    global $post;
    $post_id = $post->ID;
    if($post_id == 13528){
        $text = __( 'Buy Now', 'woocommerce' );
    }
    return $text;
}