给 WooCommerce 增加立即购买按钮,纯代码实现。

给 WooCommerce 增加立即购买按钮,纯代码实现。


正在查看 10 个帖子:1-10 (共 15 个帖子)
  • 作者
    帖子
  • 风间
    参与者
    • 文章数量: 670
    @fengjian
    楼主

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

    大致实现的效果如下:

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

    风间
    参与者
    • 文章数量: 670
    @fengjian
    楼主

    直接贴上代码,添加到你主题的 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了,按钮就有了。

    诗语
    管理员
    • 文章数量: 5,866
    @feibisi
    此回复已被标记为私有,🔒 仅楼主及管理员可见。
    bigrich
    参与者
    • 文章数量: 14
    @bigrich

    厉害,可是我加了代码之后,网站就错误了,能麻烦风间老大详细的说下加在哪,怎么加吗?

     

    bigrich
    参与者
    • 文章数量: 14
    @bigrich

    我是修改的薇晓朵中文子主题下的 functions.php文件

    风间
    参与者
    • 文章数量: 670
    @fengjian
    楼主

    上面的代码分两段,PHP 代码是添加到 你主题的 functions.php 文件里,

    CSS 代码是添加到前台=自定义 CSS 或者,styl.css 文件里面。

    bigrich
    参与者
    • 文章数量: 14
    @bigrich

    老大,操作姿势不对?

    诗语
    管理员
    • 文章数量: 5,866
    @feibisi

    代码我们商城用的也是这个,应该没问题,仔细检查下。

    
    // 添加立即结账按钮
    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' );
    

    bigrich
    参与者
    • 文章数量: 14
    @bigrich

    我是完全复制过来的,现在不会出现乱码,但完全没有反应。:(

    bigrich
    参与者
    • 文章数量: 14
    @bigrich

    已经调试成功了,谢谢各位老大的耐心解答,不过我的按钮位置参数和你们的略有一点点差异。

正在查看 10 个帖子:1-10 (共 15 个帖子)
  • 哎呀,回复话题必需登录。

未找到对应子版块?请直接在 [疑难杂症] 版块中发帖提问。

收藏纪念品


有奖活动

参与论坛社区问答,有机会获赠文派瓦普(Wapuu.com)手办模型。

话题信息

商业系统