Woocommerce 以自定义价格添加到购物车 [英] Woocommerce add to cart with custom price

查看:29
本文介绍了Woocommerce 以自定义价格添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多将带有客户价格的商品添加到 WC 购物车的示例,但没有一个是动态添加的.我正在尝试使用接收 POST 变量的短代码函数....

I've seen many examples of adding an item to the WC cart with a customer price, but none dynamically. I am trying to do in a shortcode function that receives a POST variables....

if (isset($_POST['wmnf_add_donation'])) {
    global $woocommerce;
    $cart_object = $woocommerce->cart;
    $custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
    $target_product_id = 65986;
    $cart_object->add_to_cart($target_product_id, "1");
    foreach ( $cart_object->cart_contents as $key => $value ) {
        if ( $value['product_id'] == $target_product_id ) {
            $value['data']->price = $custom_price;
        }
    }
}

这当然会将项目添加到购物车,但价格为零,我意识到我需要以某种方式将此数组保存回 WC 购物车数据.这种方法是否可行,还是只能通过过滤器或动作挂钩来完成?如果是这样,我如何将更改后的数组保存回购物车内容或使其能够添加具有发布价格的项目?非常感谢任何指导.

This adds the item to the cart of course, but the price is zero and I realize I need to somehow save this array back to the WC cart data. Is this method even possible or can it only be done via a filter or action hook? If so, how can I save the changed array back to the cart contents or make it work to add the one item with its posted price? Any guidance greatly appreciated.

感谢 doublesharp 的回答,我无法按照描述的那样工作,因为表单使用我的短代码发布到页面上,其中包含我的表单,而不是直接发布到购物车.$_POST 没有被看到,产品最终为零.我确实找到了另一种方法,但是在使用 wp_redirect 时遇到了问题.我将上面的短代码更改为:

Thanks for that answer doublesharp, I was not able to get it to work as described because the form was posting to the page with my shortcode, which has my form, instead of posting directly to the cart. The $_POST was not being seen and the product ended up zero. I did find another approach, but having a problem using wp_redirect. I changed the above shortcode to this:

if (isset($_POST['wmnf_add_donation'])) {
    global $woocommerce;
    $custom_price = ($_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0);
    $target_product_id = 65986;
    $_SESSION['donation_amount'] = $custom_price;
    $woocommerce->cart->add_to_cart($target_product_id, "1");
    wp_redirect( site_url() . '/gifts/swag-bag/');
}

然后我在functions.php中添加了以下过滤器:

Then I added the following filters to functions.php:

add_filter('woocommerce_get_price','donation_price', 10, 2);
add_filter('woocommerce_get_regular_price','donation_price', 10, 2);
add_filter('woocommerce_get_sale_price','donation_price', 10, 2);
function donation_price($price, $productd){
     if($productd->id == '65986'){
        $price = $_SESSION['donation_amount'];
     }
     return $price;
}

除非 wp_redirect 被注释掉,否则这不起作用,因此,不重定向.以上重定向到购物车,但它是空的.如果我注释掉 wp_redirect 行,然后手动转到购物车,则该产品以我的自定义价格存在.实际上,如果可能的话,我想应用自定义价格并直接重定向到结帐页面而不是购物车?

This does not work except when wp_redirect is commented out, hence, not redirecting. The above redirects to the cart, but its empty. If I comment out the wp_redirect line and then manually go to the cart, the product is there with my custom price. Actually, I would like to apply a custom price and redirect directly to the checkout page instead of the cart, if possible?

推荐答案

您可以使用 woocommerce_before_calculate_totals 动作钩子来修改购物车的内容,包括产品价格.

You can use the woocommerce_before_calculate_totals action hook to modify the contents of the cart, including the product prices.

add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
    // loop through the cart_contents
    foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
        // you will need to determine the product id you want to modify, only when the "donation_amount" is passed
        if ( $item['id'] == 65986 && isset( $_POST['donation_amount'] ) ){
            // custom price from POST
            $custom_price = $_POST['donation_amount'] > 0 ? $_POST['donation_amount'] : 0;

            // save to the cart data
            $item['data']->price = $custom_price;
            // new versions of WooCommerce may require (instead of line above)...
            // $item['data']->set_price($custom_price);
        }
    }
}

这篇关于Woocommerce 以自定义价格添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆