Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 具有自定义折扣的Woocommerce自定义优惠券类型始终返回0;_Wordpress_Woocommerce - Fatal编程技术网

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

Wordpress 具有自定义折扣的Woocommerce自定义优惠券类型始终返回0;,wordpress,woocommerce,Wordpress,Woocommerce,我试图做的是使用钩子在woocommerce中创建带有自定义编程折扣的自定义优惠券类型。这就是我的代码的样子 //add new coupon type called "custom_discount" function custom_discount_type( $discount_types ) { $discount_types['custom_discount'] =__( 'custom discount', 'woocommerce' ); return $disco

我试图做的是使用钩子在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(零)折扣金额返回。我错过了什么吗。
谢谢你的帮助=)

好的,这个话题引导我开始制作自己的优惠券类型,所以我想我应该回答它来帮助别人

我试过你的代码,想知道为什么任何普通优惠券在删除按钮前显示“-xx”,而定制优惠券的类型只是空白的,没有负号,没有磅号,而且是空的。原来这是WooCommerce如何在购物车产品上验证优惠券的问题。如果优惠券有效,则无论购物车中的商品是否有效,它都将激活

您的优惠券必须对产品有效,或者对整个购物车有效。但是,当Woocommerce检查您的产品是否对该产品有效时,它只检查优惠券类型是
固定产品
还是“百分比产品”。如果是其他内容,则运行
woocommerce\u优惠券\u对产品有效\u
钩住现有数据,但默认为false,因此您的
woocommerce\u优惠券\u获得\u折扣\u金额
钩子将不会触发

因此,您需要连接到产品的
woocommerce\u优惠券\u是否有效\u
,并验证产品以确保其有效。在我的例子中,我只是复制了默认的有效函数,并将
$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;
}

这个问题可能有点老了,但是有两种主要的优惠券类型,您应该根据正在添加的优惠券添加另一个过滤器。
woocommerce\u cart\u优惠券类型的过滤器
用于购物车折扣,而
woocommerce\u product\u优惠券类型
用于产品折扣

当您希望woocommerce自动验证中的产品/类别和产品/类别排除字段等时,您要使用的是第二个
woocommerce\u产品\u优惠券类型

您仍然可以使用Chris提交的答案进行任何手动验证,而无需按要求使用上述过滤器,但是,请注意,有两个验证功能可供使用/过滤:
$touch->is\u valid\u for_cart()
$touch->is\u valid\u for_product()