提问者:小点点

WooCommerce上的自定义优惠券错误-“未捕获的错误:在NULL上调用成员函数get_cart()……”


我试图使用PHP在WooCommerce中创建一个自定义优惠券代码,它可以将标签名为'main'的购物车中的每件商品的价格打折到10。 它通过计算购物车中标记有'main'的商品数量,然后计算每个商品的总价,并由此计算$discount变量中所需的折扣。 请参阅以下代码:

$tag_name = 'main';
$tag_count = 0; 

foreach(WC()->cart->get_cart() as $cart_item)  {
    if( has_term( $tag_name, 'product_tag', $cart_item['product_id'])){
        $tag_count += $cart_item['quantity'];
        $price = $cart_item['data']->get_price();
        $float_value_price += floatval($price); 
    }
}

$discount = $float_value_price - (10*$tag_count);
$discount_string = number_format($discount, 2);


$coupon_code = 'testing'; // Code
$amount = $discount_string; // Amount
$discount_type = 'fixed_cart'; // 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', '' );
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' );



尝试激活此代码时,收到以下错误消息:

您试图保存的代码段在第4行产生了一个致命错误:

未捕获的错误:在/homepages/9/D392621230/htdocs/clickandbuilds/WOCommerceExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(213):eval()d代码:4堆栈跟踪:#0/homepages/9/D392621230/htdocs/clickandbuilds/WOCommerceExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(213):eval() 392621230/htdocs/clickandbuilds/w


我已经研究了一段时间,但似乎找不到解决方案。 如果有人能解决这件事,我将不胜感激。 谢了。


共1个答案

匿名用户

您可以用更简单的方法实现同样的结果。 您可以在WooCommerce仪表板中创建优惠券代码,将其设置为固定产品折扣。

然后用您的代码找到所需的产品及其ID。 然后,您可以使用set_product_ids()将这些ID传递给先前创建的优惠券。 不要忘记使用save()保存新的元信息。

然后只需应用新的优惠券代码到您的购物车。

代码如下所示:

add_action( 'woocommerce_before_cart', 'cw_coupons_matched' );
function cw_coupons_matched() { 
    global $woocommerce;
    $coupon_code = 'your_discount_code';

    # Get product ID's
    foreach( WC()->cart->get_cart() as $cart_item ){
        var_dump($cart_item['data']->name);
        if (strpos($cart_item['data']->name, 'string_to_search') !== false) { 
            $product_ids[] = $cart_item['product_id'];
        }
    }

    # Adding product ID's to coupon
    $c = new WC_Coupon($coupon_code);
    $c->set_product_ids($product_ids);
    $c->save();

    # Adding discount coupon to cart
    if ( $woocommerce->cart->has_discount( $cw_coupon ) ) return;
    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
        if( in_array( $values['product_id'], $product_ids ) ) {
            $woocommerce->cart->add_discount( $coupon_code );
            wc_print_notices();
        }
    }

}