普通网友 2016-02-18 20:11
浏览 67

使用AJAX,Woocommerce Wordpress更改产品价格

I'm working with a custom product where customers can type their custom text that will be added to cart. Depending on the size of the text, a different price will be set, the logic is done and I can see the new price in the product object.

It seems that I can change the product object with the new price but I get status 500 back from admin-ajax.php when I do $woocommerce->setup_product_data($product_id).

I have found several topics but none of them seems to work in my case. I'm not able to update the cart with the new price.

Here is my ajax function in functions.php:

// Adjust new price
function applyNewPrice() {
    global $woocommerce;

    // From JS
    $product_id = (int) $_POST['id']; 
    // From JS
    $price = (float) $_POST['price'];
    $product_data = get_post($product_id);
    // Code returning status 500 here...
    $product = $woocommerce->setup_product_data($product_data); 
    $product->set_price($price);

    update_post_meta($product_id,'_price',$price);
    update_post_meta($product_id,'_regular_price',$price);

    $woocommerce->clear_product_transients( $product_id );

}

add_action('wp_ajax_applyNewPrice', 'applyNewPrice');
add_action('wp_ajax_nopriv_applyNewPrice', 'applyNewPrice');
  • 写回答

1条回答 默认 最新

  • drui0508 2016-02-18 22:15
    关注

    The function you are trying to call does not exist. This is the function you need.

    $product = wc_setup_product_data( $product_id );
    

    No sure where you are coming up with those functions. I could not find this function either?

    $woocommerce->clear_product_transients( $product_id );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真