duanduo7400 2018-03-08 15:39
浏览 48
已采纳

在添加到购物车时,通过Woocommerce中的GET请求将自定义数据传递到购物车项目

How to pass custom meta data to cart item via parameters in the URL (using a GET request) when adding to cart a product?

Example:

  • custom_price = 99.99
  • custom_reference_meta = REF0019

So the Add to cart URL will be something like:
http://yourdomain.com/?add-to-cart=25&custom_price=99.99&custom_reference_meta=REF0019

I need to do this because I'm doing a adding product via query string.

  • 写回答

1条回答 默认 最新

  • duanjue9296 2018-03-08 21:54
    关注

    This can be done easily with a custom function hooked in woocommerce_add_cart_item_data filter hook, that will store your custom data in cart item, once product is added to cart via a GET request.

    Optionally you can use a 2nd hooked function to display that data on cart and checkout. This will also allow you to check that the data is correctly set in cart item.

    The code:

    // Set custom data as custom cart data in the cart item
    add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_in_cart_object', 30, 3 );
    function save_custom_data_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
        if( ! isset($_GET['custom_price']) || ! isset($_GET['custom_reference_meta']) )
            return $cart_item_data; // Exit
    
        // Get the data from the GET request
        $custom_price          = esc_attr( $_GET['custom_price'] );
        $custom_reference_meta = esc_attr( $_GET['custom_reference_meta'] );
    
        // Set the data as custom cart data for the cart item
        $cart_item_data['custom_data']['custom_price'] = esc_attr( $_GET['custom_price'] );
        $cart_item_data['custom_data']['custom_reference_meta'] = esc_attr( $_GET['custom_reference_meta'] );
    
        return $cart_item_data;
    }
    
    // Optionally display Custom data in cart and checkout pages
    add_filter( 'woocommerce_get_item_data', 'custom_data_on_cart_and_checkout', 99, 2 );
    function custom_data_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    
        if( isset( $cart_item['custom_data']['custom_price'] ) )
            $cart_data[] = array(
                'name' => 'Custom price',
                'value' => $cart_item['custom_data']['custom_price']
            );
    
        if( isset( $cart_item['custom_data']['custom_reference_meta'] ) )
            $cart_data[] = array(
                'name' => 'Custom reference',
                'value' => $cart_item['custom_data']['custom_reference_meta']
            );
    
        return $cart_data;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示