sách gpt4 ai đã đi

wordpress - 具有自定义折扣的 Woocommerce 自定义优惠券类型始终返回 0;

In lại 作者:行者123 更新时间:2023-12-04 16:54:43 33 4
mua khóa gpt4 Nike

我想做的是使用 Hook 在 woocommerce 中创建具有自定义编程折扣的自定义优惠券类型。这是我的代码的样子

//add new coupon type called "custom_discount"
function custom_discount_type( $discount_types ) {
$discount_types['custom_discount'] =__( 'custom discount', 'woocommerce' );
return $discount_types;
}

// add the hooks
add_filter( 'woocommerce_coupon_discount_types', 'custom_discount_type',10, 1);

//function to get coupon amount for "custom_discount"
function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon) {
if ($coupon->type == 'custom_discount'){ //if $coupon->type == 'fixed_cart' or 'percent' or 'fixed_product' or 'percent_product' The code Works
$discount = 85;
return $discount;
} khác {
return $discount;
}
}
//add hook to coupon amount hook
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5);

优惠券始终返回 0(零)折扣金额。我错过了什么吗?感谢您的帮助 =)

1 Câu trả lời

好吧,这个话题让我走上了制作自己的优惠券类型的道路,所以我想我应该回答这个问题以帮助其他人。

我试用了您的代码,想知道为什么任何普通优惠券在删除按钮前都显示“-£xx”,但自定义优惠券类型只是空的,没有减号,没有井号。结果证明这是 WooCommerce 如何验证购物车产品优惠券的问题。如果优惠券有效,无论购物车中的商品是否有效,它都会激活。

您的优惠券需要对产品有效且有效,或者对整个购物车有效。但是,当 Woocommerce 检查您的产品是否对产品有效时,它只会检查优惠券类型是 fixed_product 还是“percent_product”。如果是其他任何东西,它会使用现有数据运行 woocommerce_coupon_is_valid_for_product Hook ,但默认为 false,因此您的 woocommerce_coupon_get_discount_amount Hook 不会触发。

因此您需要连接到 woocommerce_coupon_is_valid_for_product 并验证产品以确保其有效。在我的例子中,我只是复制了默认的有效函数并将 $cái này 更改为 $coupon.

// Check if coupon is valid for product
add_filter('woocommerce_coupon_is_valid_for_product', 'woocommerce_coupon_is_valid_for_product', 10, 4);

function woocommerce_coupon_is_valid_for_product($valid, $product, $coupon, $values){
if ( ! $coupon->is_type( array( 'bogof' ) ) ) {
return $valid;
}

$product_cats = wp_get_post_terms( $product->id, 'product_cat', array( "fields" => "ids" ) );

var_dump( $coupon->product_ids );

// Specific products get the discount
if ( sizeof( $coupon->product_ids ) > 0 ) {
if ( in_array( $product->id, $coupon->product_ids ) || ( isset( $product->variation_id ) && in_array( $product->variation_id, $coupon->product_ids ) ) || in_array( $product->get_parent(), $coupon->product_ids ) ) {
$valid = true;
}
}

// Category discounts
if ( sizeof( $coupon->product_categories ) > 0 ) {
if ( sizeof( array_intersect( $product_cats, $coupon->product_categories ) ) > 0 ) {
$valid = true;
}
}

if ( ! sizeof( $coupon->product_ids ) && ! sizeof( $coupon->product_categories ) ) {
// No product ids - all items discounted
$valid = true;
}

// Specific product ID's excluded from the discount
if ( sizeof( $coupon->exclude_product_ids ) > 0 ) {
if ( in_array( $product->id, $coupon->exclude_product_ids ) || ( isset( $product->variation_id ) && in_array( $product->variation_id, $coupon->exclude_product_ids ) ) || in_array( $product->get_parent(), $coupon->exclude_product_ids ) ) {
$valid = false;
}
}

// Specific categories excluded from the discount
if ( sizeof( $coupon->exclude_product_categories ) > 0 ) {
if ( sizeof( array_intersect( $product_cats, $coupon->exclude_product_categories ) ) > 0 ) {
$valid = false;
}
}

// Sale Items excluded from discount
if ( $coupon->exclude_sale_items == 'yes' ) {
$product_ids_on_sale = wc_get_product_ids_on_sale();

if ( isset( $product->variation_id ) ) {
if ( in_array( $product->variation_id, $product_ids_on_sale, true ) ) {
$valid = false;
}
} elseif ( in_array( $product->id, $product_ids_on_sale, true ) ) {
$valid = false;
}
}

return $valid;
}

关于wordpress - 具有自定义折扣的 Woocommerce 自定义优惠券类型始终返回 0;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32109396/

33 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com