标签: WooCommerce, 立即购买
- 该话题包含 14 个回复,4 人参与,最后由
诗语 更新于 5 年、 11 月前 。
-
作者帖子
-
一直都想给数字商城添加上 【立即购买】 按钮,其实 WooCommerce 的 【加入购物车】 也就是立即购买按钮,刚给客户做站点开发,需要加上这个购买,于是就又找了下,就按我们想要纯代码实现。
大致实现的效果如下:
后面也花了几分钟给我们商城实现了此功能:
直接贴上代码,添加到你主题的 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 '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="single_add_to_cart_button button alt">Checkout</a>'; } } 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 了,按钮就有了。
此回复已被标记为私有,🔒 仅楼主及管理员可见。厉害,可是我加了代码之后,网站就错误了,能麻烦风间老大详细的说下加在哪,怎么加吗?
我是修改的薇晓朵中文子主题下的 functions.php 文件
上面的代码分两段,PHP 代码是添加到 你主题的 functions.php 文件里,
CSS 代码是添加到前台=自定义 CSS 或者,styl.css 文件里面。
老大,操作姿势不对?
代码我们商城用的也是这个,应该没问题,仔细检查下。
// 添加立即结账按钮 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 '<div class="cart-button"><a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="single_add_to_cart_button button alt"> 立即购买</a></div>'; } } add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
我是完全复制过来的,现在不会出现乱码,但完全没有反应。:(
已经调试成功了,谢谢各位老大的耐心解答,不过我的按钮位置参数和你们的略有一点点差异。
-
作者帖子
- 哎呀,回复话题必需登录。