【问题标题】:Cart Discount when applied to the cart doesn't show the correct amount in Order Total using Woocommerce in wordpress应用到购物车时的购物车折扣未在 wordpress 中使用 Woocommerce 的 Order Total 中显示正确的金额
【发布时间】:2014-03-22 13:11:43
【问题描述】:

我正在创建一个新的 woocommerce 插件,它将在购物车页面顶部提供应用折扣的选项。

现在,当购物车中有产品时,用户将单击“应用折扣”按钮,当单击时,我的自定义插件会触发操作,该插件会将购物车折扣添加到该特定购物车订单。

到目前为止,它运行良好,并且还显示应用了购物车折扣。下面是截图

正如您从屏幕截图中看到的那样,Order Total 显示的数字计算错误。 以下是我的自定义 woocommerce 插件文件中的代码。

提交按钮时会调用以下操作

if(!empty($_POST['apply_discount_woo'])){
                    add_action('woocommerce_calculate_totals',array(&$this,'cart_order_total_action'));
                }

及功能码:

public function cart_order_total_action(){
                if ( is_user_logged_in() ){
                    global $woocommerce;
                    global $current_user;
                    global $wpdb;
                    $u_id = $current_user->ID;
                    $table_name = $wpdb->prefix."woocommerce_customer_reward_ms";
                    $thetable2  = $wpdb->prefix . "woocommerce_customer_reward_cart_ms";
                    $table_name3 = $wpdb->prefix."woocommerce_customer_reward_points_log_ms";
                    $data       = $wpdb->get_row("SELECT * from $table_name where id=$u_id");
                    $data2      = $wpdb->get_row("SELECT * from $thetable2");
                    /* Order Id goes here */
                    $orders=array();//order ids
                    $args = array(
                        'numberposts'     => -1,
                        'meta_key'        => '_customer_user',
                        'meta_value'      => $current_user->ID,
                        'post_type'       => 'shop_order',
                        'post_status'     => 'publish',
                        'tax_query'=>array(
                                array(
                                    'taxonomy'  =>'shop_order_status',
                                    'field'     => 'slug',
                                    'terms'     =>'on-hold'
                                    )
                        )  
                    );
                    $posts=get_posts($args);
                    //get the post ids as order ids
                    $orders=wp_list_pluck( $posts, 'ID' );
                    $order = $orders[0];
                    /* Order Id ends here */
                    if($data){
                        $user_points = $data->points;
                        $points_set  = $data2->woo_pts_set;
                        print_r($woocommerce->cart->applied_coupons);
                        echo $woocommerce->cart->discount_cart;
                        echo $woocommerce->cart->get_cart_total();
                        /* the line below adds the cart discount to the Cart */
                        $woocommerce->cart->discount_cart = $data2->woo_discount_set;
                    }else{
                        echo 'You dont have any woo points yet.!!';
                    }
                }
            }

如何在添加购物车折扣时更新购物车总数?

【问题讨论】:

  • 或者还有其他方式,可以添加购物车的Discount吗???

标签: php wordpress woocommerce


【解决方案1】:

经过数小时的研究和调试一些 Woocommerce 文档。我发现有两种类型的折扣可以应用于购物车

1- 购物车折扣 2- 订单折扣

如果我错了,请纠正我,但折扣总是适用于使用优惠券。我在这里尝试的是向购物车提供自定义折扣,例如 $5,而 Woocommere 并未将这个折扣视为实际折扣。此外,此自定义折扣不会反映购物车页面和结帐页面上的实际购物车总数。 为此,您最终将不得不更改您的 woocommerce 类文件,我不建议您在创建独立的自定义 Woocommerce 插件时这样做。

但我想出了另一种应用折扣的方法,即借助优惠券。我创建了一张自定义优惠券,可以为购物车提供 5 美元的折扣。就是这样,购物车总数识别了折扣并向我展示了正确的总数以及应用的折扣。

结帐页面也填充了正确的总数。

在处理完订单后,它向我显示应用了正确的折扣。

只是想与大家分享。因为我觉得这是一种非常有效的解决方案,而且无需更改任何 woocommerce 核心文件。

希望对大家也有用。

谢谢。

【讨论】:

  • 那么我的想法对您解决问题有帮助吗?
  • @AttilaFulop - 在您发布答案之前,我已经想出了解决方案。谢谢你,我非常感谢你的努力。
【解决方案2】:

也许不是 100% 您希望如何实现您的目标,但您可以即时生成优惠券:

$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // 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' );

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 2013-11-22
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2021-10-30
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多