Fiz*_*z S 5 php wordpress woocommerce
我试图在产品页面上的 Woocommerce 中添加一个立即购买按钮,因此有两个按钮:
我仍然想添加到购物车以照常运行。
我怎样才能做到这一点?非常感谢。
我通过找到这篇博客文章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' );
Run Code Online (Sandbox Code Playgroud)
然后我修改了主题/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>
Run Code Online (Sandbox Code Playgroud)
我在现有的“添加到购物车”按钮旁边添加了另一个按钮,但将表单分开。博客文章提到你可以添加一个超链接,但上面的内容对我需要自定义页面的方式有用(稍微啰嗦)
来自博客:
使用说明:
创建一个带有查询字符串参数的超链接,如下所示: ?set-cart-qty_= 您的产品的数字 ID 在哪里(类似于“167”),以及您希望在用户购物车中设置的数量(很可能是这个)将只是“1”)。
示例 URL 将用户发送到购物车中仅包含一件 ID 为“167”的产品结账:
我希望以上内容可以帮助任何与我遇到类似问题的人。
| 归档时间: |
|
| 查看次数: |
12327 次 |
| 最近记录: |