Woocommerce在购物车和结帐时使用自定义值覆盖产品价格

时间:2017-06-05 15:00:04

标签: woocommerce customization custom-fields

我想知道是否有人对以下情况有过一些经验。

我正在使用WooCommerce列出我的产品,准备出售。有些产品(大约20个)有大量的变化,所以我决定将产品数据放在单独的表格中,并通过SKU(简单查询获取数据)将它们加载到woocommerce中。到目前为止,一切都很好。这些产品工作正常。它们的变化显示出来,用户可以选择它们,将它们添加到购物车中。

事实上,我已经将这20个产品设置为单品,并给它们1美元的价格,只是为了显示添加到购物车按钮( .single_add_to_cart_button ),但是当我选择我的变化时(从外部表格/表格中)产品可以很好地添加到购物车中,但在这种情况下,它显示每种产品,价格为1美元,包含所有自定义字段(这很好)。据我所知,这些自定义字段来自会话)。下面是我在我的functions.php中使用的所有代码(不是我的,在网上找到,修复它以适应我的情况)

// Step 1: Add Data in a Custom Session, on ‘Add to Cart’ Button Click

add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');

function wdm_add_user_custom_data_options_callback()
{

    $product_id = $_POST['id'];
    $product_category = $_POST['product_category'];

    $custom_data_1 = $_POST['custom_data_1'];
    $custom_data_2 = $_POST['custom_data_2'];
    $custom_data_3 = $_POST['custom_data_3'];
    $custom_data_4 = $_POST['custom_data_4'];
    $custom_data_5 = $_POST['custom_data_5'];
    session_start();

    $_SESSION['custom_data_1'] = $custom_data_1;
    $_SESSION['custom_data_2'] = $custom_data_2;
    $_SESSION['custom_data_3'] = $custom_data_3;
    $_SESSION['custom_data_4'] = $custom_data_4;
    $_SESSION['custom_data_5'] = $custom_data_5;

    die();
}

// Step 2: Add Custom Data in WooCommerce Session

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,2);

if(!function_exists('wdm_add_item_data'))
{

    function wdm_add_item_data($cart_item_data,$product_id)

    {
        global $woocommerce;
        session_start();
        $new_value = array();
        if (isset($_SESSION['custom_data_1'])) {
            $option1 = $_SESSION['custom_data_1'];
            $new_value['custom_data_1'] =  $option1;
        }
        if (isset($_SESSION['custom_data_2'])) {
            $option2 = $_SESSION['custom_data_2'];
            $new_value['custom_data_2'] =  $option2;
        }
        if (isset($_SESSION['custom_data_3'])) {
            $option3 = $_SESSION['custom_data_3'];
            $new_value['custom_data_3'] =  $option3;
        }
        if (isset($_SESSION['custom_data_4'])) {
            $option4 = $_SESSION['custom_data_4'];
            $new_value['custom_data_4'] =  $option4;
        }
        if (isset($_SESSION['custom_data_5'])) {
            $option5 = $_SESSION['custom_data_5'];
            $new_value['custom_data_5'] =  $option5;
        }

        if( empty($option1) && empty($option2) && empty($option3) && empty($option4) && empty($option5)  )
            return $cart_item_data;
        else
        {
            if(empty($cart_item_data))
                return $new_value;
            else
                return array_merge($cart_item_data,$new_value);
        }

        unset($_SESSION['custom_data_1']);
        unset($_SESSION['custom_data_2']);
        unset($_SESSION['custom_data_3']);
        unset($_SESSION['custom_data_4']);
        unset($_SESSION['custom_data_5']);

    }
}

// Step 3: Extract Custom Data from WooCommerce Session and Insert it into Cart Object

add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
    function wdm_get_cart_items_from_session($item,$values,$key)
    {
        if (array_key_exists( 'custom_data_1', $values ) )
        {
            $item['custom_data_1'] = $values['custom_data_1'];
        }
        if (array_key_exists( 'custom_data_2', $values ) )
        {
            $item['custom_data_2'] = $values['custom_data_2'];
        }
        if (array_key_exists( 'custom_data_3', $values ) )
        {
            $item['custom_data_3'] = $values['custom_data_3'];
        }
        if (array_key_exists( 'custom_data_4', $values ) )
        {
            $item['custom_data_4'] = $values['custom_data_4'];
        }
        if (array_key_exists( 'custom_data_5', $values ) )
        {
            $item['custom_data_5'] = $values['custom_data_5'];
        }

        return $item;
    }
}

// Step 4: Display User Custom Data on Cart and Checkout page

add_filter('woocommerce_checkout_cart_item_quantity','wdm_add_user_custom_option_from_session_into_cart',1,3);
add_filter('woocommerce_cart_item_price','wdm_add_user_custom_option_from_session_into_cart',1,3);

if(!function_exists('wdm_add_user_custom_option_from_session_into_cart'))
{


    function wdm_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
    {

                        if(count($values['custom_data_1']) > 0)
                        {

                            $return_string = $product_name . "";
                            $return_string .= "";
                            $return_string .= "SKU : " . $values['custom_data_1'] . "

"; //$return_string .= "Code : " . $values['custom_data_2'] . "

"; $return_string .= "Width : " . $values['custom_data_3'] . " cm

"; $return_string .= "Height : " . $values['custom_data_4'] . " cm

"; $return_string .= "Price : € " . $values['custom_data_5'] . "

"; $return_string .= ""; return $return_string; } else { return $product_name; } } } // Step 5: Add Custom Data as Metadata to the Order Items add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2); if(!function_exists('wdm_add_values_to_order_item_meta')) { function wdm_add_values_to_order_item_meta($item_id, $values) { global $woocommerce,$wpdb; $user_custom_values = $values['wdm_user_custom_data_value']; if(!empty($user_custom_values)) { wc_add_order_item_meta($item_id,'wdm_user_custom_data',$user_custom_values); } $custom_data_1 = $values['custom_data_1']; if(!empty($custom_data_1)) { wc_add_order_item_meta($item_id,'custom_data_1',$custom_data_1); } $custom_data_2 = $values['custom_data_2']; if(!empty($custom_data_2)) { wc_add_order_item_meta($item_id,'custom_data_2',$custom_data_2); } $custom_data_3 = $values['custom_data_3']; if(!empty($custom_data_3)) { wc_add_order_item_meta($item_id,'custom_data_3',$custom_data_3); } $custom_data_4 = $values['custom_data_4']; if(!empty($custom_data_4)) { wc_add_order_item_meta($item_id,'custom_data_4',$custom_data_4); } $custom_data_5 = $values['custom_data_5']; if(!empty($custom_data_5)) { wc_add_order_item_meta($item_id,'custom_data_5',$custom_data_5); } } } // Step 6: Remove User Custom Data, if Product is Removed from Cart add_action('woocommerce_before_cart_item_quantity_zero','wdm_remove_user_custom_data_options_from_cart',1,1); if(!function_exists('wdm_remove_user_custom_data_options_from_cart')) { function wdm_remove_user_custom_data_options_from_cart($cart_item_key) { global $woocommerce; // Get cart $cart = $woocommerce->cart->get_cart(); // For each item in cart, if item is upsell of deleted product, delete it foreach( $cart as $key => $values) { if ( $values['wdm_user_custom_data_value'] == $cart_item_key ) unset( $woocommerce->cart->cart_contents[ $key ] ); } } }

我试图包含以下代码

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

    function add_custom_price( $cart_object ) {
        $custom_price = 999; // This is the custom price  
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $value['data']->price = $custom_price;
        }
    }

它将价格改为999,因为它固定了,但在我的情况下,我无法包含价值

$custom_data_5 = $_POST['custom_data_5']; // thats my unit price which needs to replace the $1 value of the simple product value from the product

任何帮助都会在我这边得到赞赏,因为我现在已经没有选择了,这个解决方案似乎是为了纠正一个。

1 个答案:

答案 0 :(得分:0)

好吧,我想我已经完成了,在stackoverflow.com上搜索和搜索当然......

这就是我所做的:

 function ipe_add_to_cart_link() {

        global $product;
        $custom_price = 30;

        echo sprintf( '%s',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( isset( $quantity ) ? $quantity : 1 ),
                $myCustomPrice, // Thats the value im getting from my add_to_cart_button - hidden
                esc_attr( $product->id ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                esc_html( $product->add_to_cart_text() )
        ); 
}

add_filter('woocommerce_loop_add_to_cart_link','ipe_add_to_cart_link');



function ipe_product_custom_price( $cart_item_data, $product_id ) {

     if( isset( $_POST['myCustomPrice'] ) && !empty($_POST['myCustomPrice'])) {     

         $cart_item_data[ "myCustomPrice" ] = $_POST['myCustomPrice'];     
     }
     return $cart_item_data;

 }

 add_filter( 'woocommerce_add_cart_item_data', 'ipe_product_custom_price', 99, 2 );


function ipe_apply_custom_price_to_cart_item( $cart_object ) {  
    if( !WC()->session->__isset( "reload_checkout" )) {

        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["myCustomPrice"] ) ) {
                $value['data']->price = $value["myCustomPrice"];
            }
        }   
    }   
}

add_action( 'woocommerce_before_calculate_totals', 'ipe_apply_custom_price_to_cart_item', 99 );

并将所有内容放在functions.php中 - 像魅力一样工作。希望这个解决方案可以帮助那些人。