2017-12-12 11 views
1

現在、私はフロントエンドの各製品にカスタムフォームを用意しています。ここでは、顧客は、今私が直面する問題はWoocommerceでカスタムデータを持つ商品の個別のカートアイテムとして追加

お客様は、製品1を追加している私は、次のコードを記述し、このために

を変更し、それが

add_filter('woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2); 
function add_custom_cart_item_data($cart_item_data, $cart_item_key) { 

    if(isset($_POST['new-width'])) 
     $cart_item_data['new-width'] = $_POST['new-width']; 
    if(isset($_POST['new-height'])) 
     $cart_item_data['new-height'] = $_POST['new-height']; 

    return $cart_item_data; 
} 


add_action('woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 10, 1); 
function set_custom_cart_item_price($wc_cart) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    foreach ($wc_cart->get_cart() as $cart_item){ 
     if(! empty($cart_item['new-width']) && ! empty($cart_item['new-height'])){ 
      $new_value=$cart_item['new-width']*$cart_item['new-height']; 
      $cart_item['data']->set_price($new_value); 
      } 
    } 
} 

を働いてこの価格ごとにカスタムの幅と高さを挿入することができますwidth=30 and height=30.とカートに入れるので、総額は30*30=90 [$cart_item['new-width']*$cart_item['new-height']ベース] となり、お客様はwidth=40 and height=40と同じ商品1を追加します。私の公式によると、価格は40*40=160になります。そして、チェックアウトの総価格は160+90=250 ことしかし、私は何を取得することは、私が必要なものをチェックアウトページに90 + 90 = 180

である必要があります

(1)product-1(width=30, height=30) 90 
(2)product-1(width=40, height=40) 160 
total 250 

である。しかし、現在、それは

(1)product-1(width=30, height=30) 90 qty(2) 

total 180 

を示します助けてください

答えて

1

まずはあなたがすべきですwoocommerce_add_cart_item_dataフックではなく、さまざまなアイテムを取得するには、この方法:

add_action('woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2); 
function save_custom_fields_data_to_cart($cart_item_data, $product_id) { 
    $data = array(); 

    // Set the custom data in the cart item 
    if(! empty($_POST['new-height']) && ! empty($_POST['new-height'])) 
    { 
     $cart_item_data['custom_data']['new_width'] = $_POST['new-width']; 
     $data['new_width'] = $_POST['new-width']; 

     $cart_item_data['custom_data']['new_height'] = $_POST['new-height']; 
     $data['new_height'] = $_POST['new-height']; 

     // below statement make sure every add to cart action as unique line item 
     $cart_item_data['custom_data']['unique_key'] = md5(microtime().rand()); 
     WC()->session->set('custom_data', $data); 
    } 
    return $cart_item_data; 
} 

は、その後いくつかの小さな変更で:

add_action('woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 10, 1); 
function set_custom_cart_item_price($wc_cart) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    foreach ($wc_cart->get_cart() as $cart_item){ 
     if(! empty($cart_item['custom_data'])){ 
      $new_value = $cart_item['custom_data']['new_width'] * $cart_item['custom_data']['new_height']; 
      $cart_item['data']->set_price($new_value); 
     } 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに行きますかすべてのプラグインファイルでも使用できます。

は、テストの結果、

注意を作品:30 x 3090はありませんが、それは90040 x 40160ではないですが、それはこれが機能している1600

+0

です。ありがとうございました –

関連する問題