我要做的是创建自定义优惠券类型,使用钩子在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;
} else {
return $discount;
}
}
//add hook to coupon amount hook
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5);
优惠券总是以0(零)折扣金额返回。我错过了什么。 谢谢你的帮助=)
答案 0 :(得分:5)
好的,所以这个话题让我走上了制作自己的优惠券类型的道路,所以我想我应该回答它来帮助别人。
我尝试了你的代码,并想知道为什么任何普通的优惠券在删除按钮之前显示“ - £xx”,但自定义优惠券类型只是lank,没有减号,没有英镑符号空。原来这是WooCommerce如何验证购物车产品优惠券的问题。如果优惠券有效,无论购物车中的商品是否有效,它都会生效。
您的优惠券必须对产品有效且有效,或对整个购物车有效。但是,当Woocommerce检查您的产品是否对产品有效时,它只检查优惠券类型是fixed_product
还是'percent_product'。如果它是其他任何东西它运行woocommerce_coupon_is_valid_for_product
挂钩与现有数据,但默认为false,所以你的woocommerce_coupon_get_discount_amount
挂钩不会触发。
因此,您需要挂钩woocommerce_coupon_is_valid_for_product
并验证产品以确保其有效。在我的情况下,我只是复制了默认的有效函数,并将$this
更改为$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;
}
答案 1 :(得分:0)
这个问题可能有点旧,但有两种主要的优惠券类型,您应该根据您添加的优惠券添加另一个过滤器。 woocommerce_cart_coupon_types
的过滤条件适用于购物车折扣,woocommerce_product_coupon_types
适用于产品折扣。
当您希望WooCommerce自动验证product / category_in和product / category_exclude字段等时,您想要使用的是第二个woocommerce_product_coupon_types
。
您仍然可以使用Chris提交的答案进行任何手动验证,而不是根据需要使用上述过滤器,但请注意,有两个验证功能可供使用/过滤:$coupon->is_valid_for_cart()
和{{1} }。