php立即购买怎样写代码,如何给 WooCommerce 增加【立即购买】按钮,纯代码实现。...

一直都想给数字商城添加上【立即购买】按钮,其实 WooCommerce 的【加入购物车】也就是立即购买按钮,刚给客户做站点开发,需要加上这个购买,于是就又找了下,就按我们想要纯代码实现。

大致实现的效果如下:

9d175115c0b710dbdaa5718b61b1a783.png

后面也花了几分钟给我们商城实现了此功能:

cf4efce22821d8d5394b43124bd24ff2.png

我们需要贴上一段代码,添加到你主题的 functions.php 文件里。

直接贴上代码,添加到你主题的 functions.php 文件里。

// 添加立即结账按钮

function add_content_after_addtocart() {

// get the current post/product ID

$current_product_id = get_the_ID();

// get the product based on the ID

$product = wc_get_product( $current_product_id );

// get the "Checkout Page" URL

$checkout_url = WC()->cart->get_checkout_url();

// run only on simple products

if( $product->is_type( 'simple' ) ){

echo 'Checkout';

}

}

add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );

添加上去之后发现样式有些不好看,两个按钮都挨着的,不好看,添加段 css:

button.single_add_to_cart_button.button.alt {

float: left;

margin: 20px 20px 20px 0;

}

然后就 OK 了,按钮就有了。

文章没看懂?代码不会用?需要帮助你可以去论坛提问自助服务台

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值