WordPress woocommerce 添加立即购买按钮
思韵闪耀
2024-06-23
0
/*添加buy now*/
add_action('woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
add_action('woocommerce_after_add_to_cart_form', 'buy_now_submit_form');
add_filter('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
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_get_checkout_url();
    if (in_array($product->get_type(), ['simple', 'variable'])) {
        $buy_now_button = '<button type="submit" style="clear:both;margin-top:10px;width:100%;height:45px;" name="add-to-cart" class="single_add_to_cart_button button alt" value="' . $current_product_id .'" id="buy_now_button">Buy Now</button>
                           <input type="hidden" name="is_buy_now" id="is_buy_now" value="0" />
                          ';
        echo $buy_now_button;
    }
}
/**
 * 控制跳转
 */
function buy_now_submit_form() {
    ?>
    <script>
        jQuery(document).ready(function(){
            // listen if someone clicks 'Buy Now' button
            jQuery('#buy_now_button').click(function(){
                // set value to 1
                jQuery('#is_buy_now').val('1');
                //submit the form
                jQuery('form.cart').submit();
            });
        });
    </script>
    <?php
}
/**
 * 商品加入购物车后,返回要跳转的地址
 * @param $redirect_url
 * @return string
 */
function redirect_to_checkout($redirect_url) {
    if (isset($_REQUEST['is_buy_now']) && $_REQUEST['is_buy_now']) {
        $redirect_url = wc_get_checkout_url();
    }
    return $redirect_url;
}



【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

热门资讯

推荐一款企业级应用软件Odoo 推荐一款企业级应用软件Odoo:集ERP、CRM、Shop、网站于一身 Odoo是什么? 如果用一句...
OCS Inventory 主... OCS Inventory 主从系统 OCS Inventory 是一个协助你盘点 Linux 及 ...
Web前端框架汇总 Web前端框架汇总 在做web开发的时候难免遇到一个问题,那就是,选择什么样的框架。下面把前端的框架...
免费开源建站程序来了!12款强... 1.Drupal Drupal是一个开源的内容管理框架(CMF) 平台,它由内容管理系统(CMS)和...
最全的ASP.NET开源CMS... 最全的ASP.NET开源CMS汇总 国内: 1.SiteServerCMSSiteServer CM...
最佳的 7 款 .NET 开发... DotNetNuke DotNetNuke(简称DNN)是一个免费的、开源的、可扩展的内容管理系统,...
GLPI优异的IT资源管理软件 GLPI是法语GESTIONNAIRE LIBRE DE PARC INFORMATIQUE的缩写,...
Bugzilla Bugzilla 是一个开源的缺陷跟踪系统(Bug-Tracking System),它可以管理软件...
持续集成 之 Jenkins Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作
WordPress wooco... /*添加buy now*/add_action('woocommerce_after_ad...