xiangdongsheng

1.添加 “立即购买” 按钮

  添加立即购买按钮有很多种方式,

  直接修改woocommerce模板文件,但是这种方受版本更新影响

  在当前使用的主题里面修改,不受版本更新影响,但是主题很多,得把每一个主题模板文件修改

  以插件的方式添加购买按钮

  我这里使用的是第三种

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="background-color:green" 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; }

  

分类:

技术点:

相关文章:

  • 2021-04-22
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2021-04-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案