久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將產品自定義字段值保存在購物車中并將其顯示

Save product custom field value in cart and display it on Cart and checkout(將產品自定義字段值保存在購物車中并將其顯示在購物車和結帳中)
本文介紹了將產品自定義字段值保存在購物車中并將其顯示在購物車和結帳中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在主題的functions.php中使用以下代碼在woocommerce單個產品頁面上添加了一些自定義選項:

I have added some custom options on woocommerce single product page using the code below on my theme's functions.php:

     function options_on_single_product(){
     ?>
      <input type="radio" name="option1" checked="checked" value="option1"> option 1 <br />
      <input type="radio" name="option1" value="option2"> option 2
       <?php
      }
       add_action("woocommerce_before_add_to_cart_button", "options_on_single_product");

現在我想在購物車頁面上顯示選定的選項值.請幫我做這件事.謝謝

Now i want to display the selected option value on cart page. Please help me to do this. Thanks

推薦答案

以下是在購物車對象中存儲產品自定義字段并在購物車和結帳頁面中顯示的完整代碼:

Here is the complete code to Store product custom field in cart object and display that in Cart and Checkout pages:

// Output the Custom field in Product pages
add_action("woocommerce_before_add_to_cart_button", "options_on_single_product", 1);
function options_on_single_product(){
    ?>
        <label for="custom_field">
            <input type="radio" name="custom_field" checked="checked" value="option1"> option 1 <br />
            <input type="radio" name="custom_field" value="option2"> option 2
        </label> <br />
    <?php
}

// Stores the custom field value in Cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_product_field_data', 10, 2 );
function save_custom_product_field_data( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_field'] ) ) {
        $cart_item_data[ 'custom_field' ] = esc_attr($_REQUEST['custom_field']);
        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// Outuput custom Item value in Cart and Checkout pages
add_filter( 'woocommerce_get_item_data', 'output_custom_product_field_data', 10, 2 );
function output_custom_product_field_data( $cart_data, $cart_item ) {
    if( isset( $cart_item['custom_field'] ) ) {
        $cart_data[] = array(
            'key'       => __('Custom Item', 'woocommerce'),
            'value'     => $cart_item['custom_field'],
            'display'   => $cart_item['custom_field'],
        );
    }
    return $cart_data;
}

代碼位于活動子主題(或主題)的 function.php 文件或任何插件文件中.

此代碼已經過測試且有效.

This code is tested and works.

這篇關于將產品自定義字段值保存在購物車中并將其顯示在購物車和結帳中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 在线婷婷 | 国产成人精品一区二 | 亚洲欧美激情精品一区二区 | 国产精品视频在线播放 | 成人在线免费网站 | 国产一区二 | 福利社午夜影院 | 91在线精品一区二区 | 国产黄色在线观看 | 久久只有精品 | 亚洲国产偷 | 日韩免费视频一区二区 | 国产乡下妇女做爰 | 精品国产乱码久久久久久蜜退臀 | 精品久久久久久亚洲精品 | 一区二区三区在线看 | 中国美女撒尿txxxxx视频 | 久久中文网 | 伊人精品国产 | www.日日干 | 做a的各种视频 | 成人在线视频免费观看 | 国产亚洲欧美在线 | 久久久久久国产精品免费免费 | 91欧美| 日韩高清电影 | 人人种亚洲 | 久久精品成人 | 欧美福利 | 欧美极品视频在线观看 | 亚洲 一区| 欧美精品国产精品 | 男人天堂色| 夜夜操操操| 国产在线精品一区二区三区 | 国产激情91久久精品导航 | 欧美日韩国产一区二区三区 | 成人免费视频网站在线观看 | 中文字幕在线网 | 日韩在线免费 | 小h片免费观看久久久久 |