使用自定义信息和价格将产品添加到购物车 [英] Adding a product to cart with custom info and price

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

问题描述

我已安装woocommerce来处理产品输入和一家WordPress商店的结帐流程.

I have installed woocommerce to handle product input & checkout process of a wordpress shop.

商店页面是自定义构建的,它允许用户从列表中选择产品并对其进行自定义,从而基于数据库中存储的信息以javascript输出价格.

The shop page is custom built which allows the user to pick a product from a list and customise it which outputs a price in javascript based on information stored in the database.

存储在数据库中的产品的价格为0.00,因为根据选择的变量它们的价格不同.

The products stored in the db are valued at 0.00 because they're different prices depending on the variables chosen.

我准备传递给woocommerce的输出数据如下:

The output data I'm ready to pass to woocommerce is as follows:

  • WC产品ID(与数据库中的产品匹配)
  • 自定义价格
  • 自定义图片
  • 自定义说明(例如100mm x 100mm)
  • 构建数据(将针对商品存储,但在结帐时看不到)

我正在尝试找到一种使用产品ID将产品添加到购物车(使其有效)的方法,然后使用自定义价格覆盖价格并附加元数据,其中大多数将在结帐时看到,将被隐藏,直到在wordpress管理员中看到为止.

I'm trying to find a way to add a product to the cart using the product ID (to make it valid) and then overriding the price with the custom price and attaching meta data which most will be seen at checkout and one will be hidden until seen in the wordpress admin.

使用以下方法将产品添加到购物车中

Adding the product to the cart is achieved by using:

$woocommerce->cart->add_to_cart($_POST['custom_product_id']);

在那之后,我发现无法覆盖价格并添加其他信息.

After which point I'm finding it impossible to override the price and add additional information.

推荐答案

所有这些代码都进入了functions.php

All of this code goes into functions.php

  1. 这会捕获其他已发布的信息(全部以一个数组发送)

  1. This captures additional posted information (all sent in one array)

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);
function wdm_add_item_data($cart_item_data, $product_id) {

    global $woocommerce;
    $new_value = array();
    $new_value['_custom_options'] = $_POST['custom_options'];

    if(empty($cart_item_data)) {
        return $new_value;
    } else {
        return array_merge($cart_item_data, $new_value);
    }
}

  • 这将从上一个功能中捕获信息并将其附加到项目中.

  • This captures the information from the previous function and attaches it to the item.

    add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
    function wdm_get_cart_items_from_session($item,$values,$key) {
    
        if (array_key_exists( '_custom_options', $values ) ) {
            $item['_custom_options'] = $values['_custom_options'];
        }
    
        return $item;
    }
    

  • 这将在购物篮&从附加到该项目的附加信息中结帐.

  • This displays extra information on basket & checkout from within the added info that was attached to the item.

    add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3);
    function add_usr_custom_session($product_name, $values, $cart_item_key ) {
    
        $return_string = $product_name . "<br />" . $values['_custom_options']['description'];// . "<br />" . print_r($values['_custom_options']);
        return $return_string;
    
    }
    

  • 这会将信息添加为元数据,以便可以将其视为订单的一部分(要向客户隐藏任何元数据,只需在其下划线就可以了)

  • This adds the information as meta data so that it can be seen as part of the order (to hide any meta data from the customer just start it with an underscore)

    add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
    function wdm_add_values_to_order_item_meta($item_id, $values) {
        global $woocommerce,$wpdb;
    
        wc_add_order_item_meta($item_id,'item_details',$values['_custom_options']['description']);
        wc_add_order_item_meta($item_id,'customer_image',$values['_custom_options']['another_example_field']);
        wc_add_order_item_meta($item_id,'_hidden_field',$values['_custom_options']['hidden_info']);
    
    }
    

  • 如果您想覆盖价格,可以使用针对产品保存的信息来实现

  • If you want to override the price you can use information saved against the product to do so

    add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
    function update_custom_price( $cart_object ) {
        foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {       
            // Version 2.x
            //$value['data']->price = $value['_custom_options']['custom_price'];
            // Version 3.x / 4.x
            $value['data']->set_price($value['_custom_options']['custom_price']);
        }
    }
    

  • 您将所有自定义信息显示在客户电子邮件和订单中,只要您将其添加为元数据即可(4.)

    All your custom information will appear in the customer email and order from within wordpress providing you added it as meta data (4.)

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

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