英文:
Product page custom checkbox enable a percentage discount on WooCommerce cart subtotal
问题
以下是代码部分的翻译:
目标很简单,在 **WooCommerce 产品页面** 上添加一个复选框。如果用户选中复选框,则将在购物车小计上应用 5% 的折扣。
我已经检查了代码,多次确认,但我就是无法弄清楚为什么会出现“致命错误”。
**这是代码:**
add_action( 'woocommerce_after_add_to_cart_button', 'ls_automatic_discount_checbox', 10 );
function ls_automatic_discount_checbox() {
echo '<p><label><input type="checkbox" name="apply_automatic_discount" value="1"/> 应用 5% 的折扣到您的小计</label></p>';
}
add_action( 'woocommerce_cart_calculate_fees', 'ls_apply_discount_on_checkbox_check', 10, 7 );
function ls_apply_discount_on_checkbox_check( $cart, $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// 确保复选框被选中
if ( isset( $_POST['apply_automatic_discount'] ) && $_POST['apply_automatic_discount'] == 1 ) {
// 如果是这样,检查小计的折扣百分比
$percent = 5;
// 如果一切正常 - 给予折扣
if ( isset ( $percent ) && $percent > 0 ) {
// 客户将获得小计的 5% 折扣
$discount = $cart->cart_contents_total * $percent / 100;
$cart->add_fee( __('5% OFF', 'woocommerce' ) . " (" . $percent . "%)", -$discount);
}
}
}
英文:
The goal is simple, add a checkbox to the WooCommerce Product Page. If the user checks the checkbox, a 5% discount is applied on cart subtotal.
I have checked and doubled checked the code like ten times, I just cannot figure out why it gives me the "Fatal Error
".
This is the code:
add_action( 'woocommerce_after_add_to_cart_button', 'ls_automatic_discount_checbox', 10 );
function ls_automatic_discount_checbox() {
echo '<p><label><input type="checkbox" name="apply_automatic_discount" value="1"/> Apply 5% discount to your subtotal</label></p>';
}
add_action( 'woocommerce_cart_calculate_fees', 'ls_apply_discount_on_checkbox_check', 10, 7 );
function ls_apply_discount_on_checkbox_check( $cart, $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// makes sure that the checkbox is ticked
if ( isset( $_POST[ 'apply_automatic_discount' ] ) && $_POST[ 'apply_automatic_discount' ] == 1 ) {
// if it is, check what the percentaged discount on the subtotal is
$percent = 5;
// if all checks out - give the discount
if ( isset ( $percent ) && $percent > 0 ) {
// customer gets a discount of 5% of the SUBTOTAL
$discount = $cart->cart_contents_total * $percent / 100;
$cart->add_fee( __('5% OFF', 'woocommerce' ) . " (" . $percent . "%)", -$discount);
}
}
}
答案1
得分: 2
以下是您的代码中需要修复的问题和错误:
- 首先,您需要将复选框的值保存为自定义购物车项目数据。
woocommerce_cart_calculate_fees
钩子只有一个参数:$cart
(WC_Cart
对象)。这是主要的错误问题。- 您可以将
woocommerce_after_add_to_cart_button
钩子(在添加到购物车按钮后显示复选框)替换为woocommerce_before_add_to_cart_button
,以在之前显示复选框,这更符合逻辑。
代码如下:
add_action('woocommerce_after_add_to_cart_button', 'ls_automatic_discount_checbox', 10);
function ls_automatic_discount_checbox() {
printf('<p class="auto-discount"><label><input type="checkbox" name="auto_discount" value="1"/> %s</label></p>',
__('Apply 5% discount to your subtotal', 'woocommerce'));
}
// 添加自定义购物车项目数据
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_value_to_cart_item_data', 10, 2);
function add_custom_field_value_to_cart_item_data($cart_item_data, $product_id) {
$cart_item_data['auto_discount'] = isset($_POST['auto_discount']) ? '1' : '0';
return $cart_item_data;
}
现在,您可以全局或按产品添加折扣(已选中复选框的产品):
A/. 全局(对购物车小计):
add_action('woocommerce_cart_calculate_fees', 'auto_applied_product_discount', 10);
function auto_applied_product_discount($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
$auto_discount = false; // 初始化
// 遍历购物车项目
foreach ($cart->get_cart() as $cart_item) {
if (isset($cart_item['auto_discount']) && $cart_item['auto_discount']) {
$auto_discount = true;
break; // 停止循环
}
}
if ($auto_discount) {
$percent = 5;
$total = $cart->cart_contents_total;
$cart->add_fee(__('5% OFF', 'woocommerce') . " (" . $percent . "%)", -($total * $percent / 100));
}
}
B/. 按产品小计(已选中复选框的产品):
add_action('woocommerce_cart_calculate_fees', 'auto_applied_product_discount', 10);
function auto_applied_product_discount($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
$total = 0; // 初始化
// 遍历购物车项目
foreach ($cart->get_cart() as $cart_item) {
if (isset($cart_item['auto_discount']) && $cart_item['auto_discount']) {
$total += $cart_item['line_total'];
}
}
if ($total > 0) {
$percent = 5;
$cart->add_fee(__('5% OFF', 'woocommerce') . " (" . $percent . "%)", -($total * $percent / 100));
}
}
将此代码放在您子主题的 functions.php
文件中(或插件中)。已测试并正常工作。
英文:
There are some missing things and mistakes in your code:
- 1st, you need to save the checkbox value as custom cart item data.
- the hook
woocommerce_cart_calculate_fees
has only one argument:$cart
(the WC_Cart object). That was the main error issue. - You could replace
woocommerce_after_add_to_cart_button
hook (that displays the checkbox after add to cart button) withwoocommerce_before_add_to_cart_button
, to display the checkbox before, which is more logical.
The code:
add_action( 'woocommerce_after_add_to_cart_button', 'ls_automatic_discount_checbox', 10 );
function ls_automatic_discount_checbox() {
printf( '<p class="auto-discount"><label><input type="checkbox" name="auto_discount" value="1"/> %s</label></p>',
__('Apply 5% discount to your subtotal', 'woocommerce') );
}
// Add custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_value_to_cart_item_data', 10, 2);
function add_custom_field_value_to_cart_item_data($cart_item_data, $product_id) {
$cart_item_data['auto_discount'] = isset($_POST['auto_discount']) ? '1' : '0';
return $cart_item_data;
}
Now you can add a discount globally or by product (where the checkbox has been checked):
A/. Globally (on the cart subtotal):
add_action( 'woocommerce_cart_calculate_fees', 'auto_applied_product_discount', 10 );
function auto_applied_product_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$auto_discount = false; // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if ( isset($cart_item['auto_discount']) && $cart_item['auto_discount'] ) {
$auto_discount = true;
break; // Stop the loop
}
}
if ( $auto_discount ) {
$percent = 5;
$total = $cart->cart_contents_total;
$cart->add_fee( __('5% OFF', 'woocommerce' ) . " (" . $percent . "%)", -($total * $percent / 100));
}
}
B/. By product subtotal (where the checkbox has been checked):
add_action( 'woocommerce_cart_calculate_fees', 'auto_applied_product_discount', 10 );
function auto_applied_product_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$total = 0; // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if ( isset($cart_item['auto_discount']) && $cart_item['auto_discount'] ) {
$total += $cart_item['line_total'];
}
}
if ( $total > 0 ) {
$percent = 5;
$cart->add_fee( __('5% OFF', 'woocommerce' ) . " (" . $percent . "%)", -($total * $percent / 100));
}
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论