如何通过输入字段更改购物车中的商品价格?

问题描述 投票:0回答:2
javascript php wordpress woocommerce hook-woocommerce
2个回答
1
投票

你需要拿到产品,然后影响价格,然后保存

 function add_custom_price($cart) {
     if (is_admin() && !defined('DOING_AJAX'))
         return;

     if (did_action('woocommerce_before_calculate_totals') >= 2)
         return;

     if (isset($_POST['price']) && $_POST['id']) {
         $price = intval($_POST['price']);
         $id = intval($_POST['id']);

         // Get product
         $product = wc_get_product($id);
         $product->set_regular_price($price);
         $product->save();
         // delete the relevant product transient
         wc_delete_product_transients( $id );
     }
 }

0
投票

我想使用这个模组。 我将代码的第一部分放在 cart.php 模板文件中,最后一部分放在 function.php 中;但我不知道我必须将代码的第二部分粘贴到哪里。

我是 PHP 菜鸟 请问有人可以帮助我吗?

© www.soinside.com 2019 - 2024. All rights reserved.