【问题标题】:Woocommerce - Add To Cart and Buy Now buttons on Product PagesWoocommerce - 产品页面上的添加到购物车和立即购买按钮
【发布时间】:2015-01-15 01:19:26
【问题描述】:

我正在尝试在产品页面上的 Woocommerce 中添加一个立即购买按钮,因此有两个按钮:

  1. 加入购物车
  2. 立即购买(这会将产品添加到购物车并重定向到结帐)

我仍然希望添加到购物车以正常使用。

我怎样才能做到这一点?非常感谢。

http://wordpress.org/extend/plugins/woocommerce/

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我通过找到这篇博文http://samplacette.com/skip-shopping-cart-in-woocommerce/ 设法解决了这个问题。

    如果其他人发现他们在努力实现这一点,我就是这样做的(可能不是最好的解决方案,但它对我有用):

    我将以下文本复制到我的主题functions.php中

    /** * Set cart item quantity action *
    * Only works for simple products (with integer IDs, no colors etc) *
    * @access public
    * @return void */
    function woocommerce_set_cart_qty_action() { 
        global $woocommerce;
        foreach ($_REQUEST as $key => $quantity) {
        // only allow integer quantities
        if (! is_numeric($quantity)) continue;
    
            // attempt to extract product ID from query string key
            $update_directive_bits = preg_split('/^set-cart-qty_/', $key);
            if (count($update_directive_bits) >= 2 and is_numeric($update_directive_bits[1])) {
                $product_id = (int) $update_directive_bits[1]; 
                $cart_id = $woocommerce->cart->generate_cart_id($product_id);
                // See if this product and its options is already in the cart
                $cart_item_key = $woocommerce->cart->find_product_in_cart( $cart_id ); 
                // If cart_item_key is set, the item is already in the cart
                if ( $cart_item_key ) {
                    $woocommerce->cart->set_quantity($cart_item_key, $quantity);
                } else {
                    // Add the product to the cart 
                    $woocommerce->cart->add_to_cart($product_id, $quantity);
                }
            }
        }
    }
    
    add_action( 'init', 'woocommerce_set_cart_qty_action' );
    

    然后我修改了 theme/woocommerce/single-product/add-to-cart/simple.php(请确保您没有修改插件文件,因此请复制并粘贴到您的主题文件到 woocommerce 文件夹中)到以下(请注意,我已从代码中删除了我的数量输入,因此如果您需要它,请确保您重新编写代码以使其正常工作):

    <form class="cart single-product" method="post" enctype='multipart/form-data'>
        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
        <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
        <button type="submit" class="single_add_to_cart_button button alt cart-buttton add-to-cart"><?php echo $product->single_add_to_cart_text(); ?></button>
        <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
    </form>
    
    <form class="cart single-product" method="post" enctype='multipart/form-data' action="/checkout?set-cart-qty_<?php echo $product->id;?>=1">
        <button type="submit"  class="single_add_to_cart_button button alt cart-buttton buy-now">Buy Now</button>
        <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
    </form>
    

    我在现有的“添加到购物车”按钮旁边添加了另一个按钮,但将表单分开。博客文章提到您可以添加一个超链接,但就我需要自定义页面的方式而言,上述方法对我有用(有点冗长)

    来自博客:

    使用说明:

    使用查询字符串参数创建一个超链接,如下所示: ?set-cart-qty_= 您的产品的数字 ID 在哪里(例如“167”),以及您希望在用户购物车中设置的数量(很可能只是“1”)。

    发送用户结帐的示例 URL,购物车中只有一件 ID 为“167”的产品:

    http://my.website.com/checkout?set-cart-qty_167=1

    我希望以上内容可以帮助任何与我有类似问题的人。

    【讨论】:

      【解决方案2】:

      经过大量搜索,我很惊讶这不是标准的东西。 这是我的解决方案:

      要么使用像“woocommerce_single_product_summary”这样的钩子

      或将 wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php 复制到您的子主题中,例如: wp-content/themes/child-theme/woocommerce/single-product/add-to-cart/simple.php

      编辑文件并在您希望按钮出现的位置添加以下代码:

      <div class="express-checkout-wrapper">
                  <a id="dc_express_checkout" href="/checkout/?add-to-cart=<?php echo get_the_ID(); ?>">
                      Purchase
                  </a>
              </div>
      

      现在唯一的问题是按钮将带您结帐并添加正确的产品,但如果您更改了数量却没有正确的数量,所以我在我的 custom.js 文件中使用了 js,该文件在页脚中排队:

      // Add and change quantity to express checkout button when the quantity is updated
      if($('.cart .qty').length){
          var originalUrl = $('#dc_express_checkout').attr('href');
          $('.cart .qty').change(function(){
              var url = originalUrl + '&quantity=' + $(this).val();
              $('#dc_express_checkout').attr('href', url);
          });
      }
      

      您可以更改网址:

      href="/checkout/?add-to-cart=<?php echo get_the_ID(); ?>"
      

      到:

      href="/cart/?add-to-cart=<?php echo get_the_ID(); ?>"
      

      如果您希望按钮指向购物车而不是结帐页面。

      【讨论】:

        猜你喜欢
        • 2015-05-24
        • 2016-06-20
        • 2018-03-05
        • 2019-02-20
        • 1970-01-01
        • 2021-07-05
        • 2021-10-13
        • 1970-01-01
        • 2014-06-06
        相关资源
        最近更新 更多