所有分类
  • 所有分类
  • WP插件Pro
  • HTML模板
  • 微信小程序模板
  • 源码

给wordpress商城增加一键购买按钮不使用插件

要在WordPress商店中添加一键购买按钮,您需要修改负责显示产品页面的代码。具体来说,您需要向页面添加按钮元素,然后使用JavaScript处理按钮单击事件。
假设您正在使用WooCommerce作为电子商务平台,您可以通过创建子主题并覆盖“single-product.php”文件来修改产品页面模板。在此文件中,您可以添加按钮元素和必要的JavaScript代码。

一下是修改single-product.php的具体代码:

<?php
/**
* The template for displaying product content within loops
*
* This template can be overridden by copying it to yourtheme/woocommerce/content-single-product.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.6.0
*/

defined( 'ABSPATH' ) || exit;

/**
* Hook: woocommerce_before_single_product.
*
* @hooked wc_print_notices - 10
*/
do_action( 'woocommerce_before_single_product' );

if ( post_password_required() ) {
echo get_the_password_form(); // WPCS: XSS ok.
return;
}
?>

<div id="product-<?php the_ID(); ?>" <?php wc_product_class( '', $product ); ?>>
<?php
/**
* Hook: woocommerce_before_single_product_summary.
*
* @hooked woocommerce_show_product_sale_flash - 10
* @hooked woocommerce_show_product_images - 20
*/
do_action( 'woocommerce_before_single_product_summary' );
?>

<div class="summary entry-summary">
<?php
/**
* Hook: woocommerce_single_product_summary.
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce

当然以上代码对于代码小白来说还是有一些困难。实际上我们使用一些钩子函数就可以了

什么是钩子函数?有兴趣的可以了解下

wordpress提供了许多钩子函数,钩子函数使开发人员可以在WordPress的核心代码中添加自定义代码。钩子函数是一种机制,允许开发人员在WordPress的核心代码中添加自定义代码,而无需修改核心代码。这使得开发人员可以轻松地扩展WordPress的功能,而无需担心将来的WordPress更新会破坏他们的代码。

WordPress的钩子函数分为两种类型:动作和过滤器。动作是一种钩子函数,它允许开发人员在WordPress的核心代码中添加自定义代码,以响应特定的事件。例如,当用户登录WordPress时,可以使用动作钩子函数来添加自定义代码,以便在用户登录时执行某些操作。

过滤器是一种钩子函数,它允许开发人员修改WordPress的核心代码中的数据。例如,当WordPress显示文章内容时,可以使用过滤器钩子函数来修改文章内容,以便添加自定义HTML或其他内容。

以下是一个使用动作钩子函数的示例:

function my_custom_function() {
// Add custom code here
}
add_action( 'wp_login', 'my_custom_function' );

在这个例子中,我们使用add_action函数将my_custom_function函数添加到wp_login动作钩子函数中。这意味着当用户登录WordPress时,my_custom_function函数将被调用。

以下是一个使用过滤器钩子函数的示例:

function my_custom_function( $content ) {
// Modify $content here
return $content;
}
add_filter( 'the_content', 'my_custom_function' );

接下来我们来说具体的解决办法关于,给wordpress商城增加一键购买按钮不使用插件,将一下函数放到主题的functions.php文件中。

function action_woocommerce_after_add_to_cart_button() {
global $product;

// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Run only on simple products
if ( $product->is_type( 'simple' ) ) {
// Get product ID
$product_id = $product->get_id();

// Get permalink
$permalink = $product->get_permalink();

// Output url
echo '< a href="' . $permalink . '?product_id=' . $product_id . '&redirect_checkout=true" class="mongo_single_add_to_cart_button button">'. __ ( 'Checkout', 'woocommerce' ) . '</ a>';
}
}
}
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_button', 10 );

// Redirect
function action_template_redirect() {
// Determines whether the current request is for an administrative interface page
if ( is_admin() ) return;

// Returns true when viewing a single product
if ( ! is_product() ) return;

// Get params
if ( isset( $_GET['product_id'] ) && isset( $_GET['redirect_checkout'] ) ) {
// Get param 1
$product_id = $_GET['product_id'];

// Get param 2
$boolean = $_GET['redirect_checkout'];

// WC Cart
if ( WC()->cart ) {
// 1. Empty cart
WC()->cart->empty_cart();

// 2. Add to cart
WC()->cart->add_to_cart( $product_id );

// 3. Redirect
// When true
if ( $boolean ) {
// Gets the url to the checkout page
$checkout_url = wc_get_checkout_url();

// Performs a safe (local) redirect
wp_safe_redirect( $checkout_url );
exit;
}
}
}
}
add_action( 'template_redirect', 'action_template_redirect' );

仔细我们一看会发现,好像只有单产品中使用,一个产品有多个SKU中,是无法实现这个功能;原因在于wordpress的如果选好多属于再添加一键购买,这刚好是add to cart添加购物车功能,这样做也会与add to  cart 按钮冲突,因此不可实现。

原文链接:https://www.itaoda.cn/blog/2948.html,转载请注明出处。

0
使用和安装有任何问题
请加客服QQ:1442071397 或wechat:pufei889


免责声明

本站所发布的部分内容自网络,该部分内容限用于学习和研究目,有版权问题的,下载后的24个小时之内,从您的电脑中彻底删除。且不得将用于商业或者非法用途,否则,一切后果请用户自负,与本站无关。

评论0

请先
没有账号?注册  忘记密码?

社交账号快速登录

× 发送
微信扫一扫

温性提示

演示站服务器在国外,网站打开速度有点慢,请耐心等待

多功能、多行业外贸商城 wordpress商城WOOCOMMERCE

23+
行业模板