2016-09-14 12 views
4

私は現在、カスタム属性からアイテムの価格を変更するチェックボックス(カート内の各アイテム)のオプションをカートに追加する必要があります。各項目のチェックボックスを表示するためのカスタムフィールドでカートの商品価格を変更する、Woocommerce

これは、(私はすでにカスタムフィールドを作成して、ボタン「更新カート」をクリックしたときだけ、価格更新機能を必要とする)ことの実例である

enter image description here

コード(/ woocommerce /テンプレート/カート/ cart.php):

<td class="product-url"> 
    <?php 
     $html = sprintf('<div class="lorem"><input type="checkbox" name="cart[%s][lorem]" value="%s" size="4" class="input-text url text" /> Lorem price</div>', $cart_item_key, esc_attr($values['url'])); 
     echo $html; 
    ?> 
</td> 

答えて

1

ここで私はlorem priceがmeta_key your_custom_meta_field

に関連付けられたカスタムメタフィールドに格納されていることを仮定しています

使用の答えを待っている間、あなたのテーマのfunction.phpファイル

add_action('woocommerce_before_calculate_totals', 'my_custom_calculate_totals'); 
function my_custom_calculate_totals($cart) { 
    if (! empty($cart->cart_contents)) { 
     $lorem_price = array(); 
     if (! empty($_REQUEST['cart'])) {  // check if any of the checkboxes is checked 
      WC()->session->set('my_lorem_price', $_REQUEST['cart']);  // set all checkboxes information in session 
      $lorem_price = $_REQUEST['cart']; 
     } 
     if (empty($lorem_price)) { 
      $lorem_price = WC()->session->get('my_lorem_price');  // fetch all checkboxes information from session 
     } 
     if (empty($lorem_price)) { 
      return;  // don't do anything if any of the checkboxes is not checked 
     } 
     foreach ($cart->cart_contents as $cart_item_key => $cart_item) { 
      if (isset($lorem_price[ $cart_item_key ]['lorem'])) { 
       // Use following line if lorem price is set at variation level 
       $id = (! empty($cart_item['variation_id']) && $cart_item['variation_id'] > 0) ? $cart_item['variation_id'] : $cart_item['product_id']; 
       // Use following line if lorem price is set at product level 
       // $id = $cart_item['product_id']; 
       $new_price = get_post_meta($id, 'your_custom_meta_field', true);  // fetch price from custom field 
       $cart_item['data']->price = $new_price; 
      } 
     } 
    } 
} 
+0

内のコードを以下の私は感謝とにかく、これに似解決策を持って、私はあなたのコードの一部を取りました。 – harisdev

関連する問題