gpt4 book ai didi

php - 在 Woocommerce 中以编程方式应用优惠券

转载 作者:IT王子 更新时间:2023-10-29 01:16:32 26 4
gpt4 key购买 nike

在 Woocommerce 中,如果购物车中的重量超过 100 磅,我正在尝试找到一种方法对整个客户的订单应用 10% 的折扣。我正在努力实现这一目标。对于下一步,我正在寻找一种方法,通过 functions.php 通过操作/ Hook 以编程方式应用优惠券代码。

看来我可以使用函数 woocommerce_ajax_apply_coupon 来执行此操作 ( http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html ),但我不确定如何使用它。

到目前为止,我已经修改了 cart.php 以获得购物车中所有产品的总重量,我已经创建了一个应用折扣的优惠券(如果手动输入),并且我已经向函数添加了一些代码。 php 来检查重量并向用户显示消息。

编辑:部分代码已删除,完整代码包含在下面的解决方案中。


感谢 Freney 的指导。这是在满足条件时成功应用折扣券并在不再满足时将其删除的工作最终结果:

/* Mod: 10% Discount for weight greater than 100 lbs 
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart:
global $total_weight;
$total_weight = $woocommerce->cart->cart_contents_weight;
*/
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100');
function discount_when_weight_greater_than_100( ) {
global $woocommerce;
global $total_weight;
if( $total_weight > 100 ) {
$coupon_code = '999';
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has been Applied!</strong> Your total order weight is <strong>' . $total_weight . '</strong> lbs.</div>';
}
}

/* Mod: Remove 10% Discount for weight less than or equal to 100 lbs */
add_action('woocommerce_before_cart_table', 'remove_coupon_if_weight_100_or_less');
function remove_coupon_if_weight_100_or_less( ) {
global $woocommerce;
global $total_weight;
if( $total_weight <= 100 ) {
$coupon_code = '999';
$woocommerce->cart->get_applied_coupons();
if (!$woocommerce->cart->remove_coupons( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
$woocommerce->cart->calculate_totals();
}
}

最佳答案

首先,创建折扣券(通过 http://docs.woothemes.com/document/create-a-coupon-programatically/ ):

$coupon_code = 'UNIQUECODE'; // Code - perhaps generate this from the user ID + the order ID
$amount = '10'; // Amount
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product

$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);

$new_coupon_id = wp_insert_post( $coupon );

// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

然后将该优惠券应用于您的订单:

if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code )))
$woocommerce->show_messages();

最后一个函数返回一个 BOOL 值:如果折扣成功则为 TRUE,如果由于多种原因之一而失败则为 FALSE。

关于php - 在 Woocommerce 中以编程方式应用优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15744689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com