首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce上的自定义优惠券错误-“未登录错误:调用null.上的成员函数get_cart()”

WooCommerce上的自定义优惠券错误-“未登录错误:调用null.上的成员函数get_cart()”
EN

Stack Overflow用户
提问于 2020-06-06 11:47:29
回答 1查看 771关注 0票数 1

我试图在WooCommerce中创建一个自定义优惠券代码,该代码实际上将购物车中每一项的价格折扣为10。它通过计算带有“main”标记的购物车中的商品数量,然后计算每一种商品的总价格,并由此计算$discount变量中所需的折扣。见下文代码:

代码语言:javascript
复制
$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行产生了一个致命错误:

Uncaught :在/homepages/9/d392621230/htdocs/clickandbuilds/WOOCOMMERCEExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(213)中调用null上的成员函数get_cart():eval() d代码:4堆栈跟踪:#0 /homepages/9/d392621230/htdocs/clickandbuilds/WOOCOMMERCEExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(213):eval() #1 /homepages/9/d392621230/htdocs/clickandbuilds/WOOCOMMERCEExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(263):Code_Snippets_Edit_Menu->test_code(Object(Code_Snippet)) #2 /homepages/9/d392621230/htdocs/clickandbuilds/WOOCOMMERCEExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(122):Code_Snippets_Edit_Menu->save_posted_snippet() #3 /首页/9/d392621230/htdocs/clickandbuilds/WOOCOMMERCEExperiment/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(99):代码段_编辑_菜单->process_actions() #4 /homepages/9/d392621230/htdocs/clickandbuilds/W

我看这件事已经有一段时间了,似乎找不到解决办法。如果有人能解决这个问题,我将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-06-06 13:47:11

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

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

然后将新的优惠券代码应用于您的购物车。

代码应该如下所示:

代码语言:javascript
复制
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();
        }
    }

} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62231260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档