簡體   English   中英

更新購物籃/購物車頁面上的購物籃自定義屬性 WooCommerce 產品附加組件

[英]Update basket custom attribute on basket/cart page WooCommerce Product Add-ons

我一直在嘗試這樣做,以便我可以使用WooCommerce Product Add-ons更新我在購物車/購物車頁面上添加的自定義字段(每個產品)。

我確實得到了一種作為測試的方法,但它確實是一個很好的解決方案,而且大部分都是測試/錯誤代碼。 由於某種原因,它也只能第二次工作 - 好像緩存正在破壞它,它將通過文本區域進行更改。 但我現在已經設置了一個值,看看我是否可以讓它工作。

因此,澄清一下,在第一次提交時,沒有任何反應,第二次更新產品,但是它現在不更新屬性,它更新數量等(當我在代碼中添加一個值時)而不是屬性。

我已將其設置為之后刪除產品,但是當我刪除它時它會丟失屬性,所以我暫時將重復項留在了那里,直到我修復了更新頁面所需的雙重更新!

有沒有任何人可以看到可以將我指向更快修復的方向? 非常感謝任何幫助!!!

正如您從評論中看到的那樣,我一直在嘗試幾種方法,但一直失敗!

<?php
if ( ! empty( $_POST['update_cart'] ) )  {
    $cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            if ( isset( $cart_totals[ $cart_item_key ]['addons'] ) ) {
                WC()->cart->remove_cart_item($cart_item_key);
                #print_r($woocommerce->cart->cart_contents[ $cart_item_key ]['addons']);                    
                #$cart_item['addons'] = array('name' => 'Add your message', 'value' => 'testing the message box', 'price' => 0, 'field_name' => $cart_item['product_id'].'-add-your-message-1', 'field_type' => 'custom_textarea', 'price_type' => 'quantity_based' );
                $data = array('name' => 'Add your message', 'value' => 'testing the message box', 'price' => 0, 'field_name' => $cart_item['product_id'].'-add-your-message-1', 'field_type' => 'custom_textarea', 'price_type' => 'quantity_based' );
                #WC()->cart->add_to_cart($cart_item['product_id'], $cart_item['quantity'], $cart_item['variation_id'], $cart_item['addons']);
                WC()->cart->add_to_cart($cart_item['product_id'], $cart_item['quantity'], $cart_item['variation_id'], $cart_item['addons']);
                #$woocommerce->cart->cart_contents[$cart_item_key]['addons'][0]['value'] = 'test';
                $woocommerce->cart->cart_contents[$cart_item]['addons'] = $data;
                
            }
        }
    }
}

編輯:下面是更新每個項目的消息框,這很好,正是需要的,只是試圖讓 session 接受更新。 購物車是多頁購物車,因為這是所請求的,所以當您 go 到下一頁/結帳頁面時,您可以看到它已恢復為原始消息,您可以在購物車 session 中看到它。 此外,如果您只是刷新頁面,它會恢復(在您已經更新頁面之后)。

我試圖通過 WC()->session->set 進行編輯,但我認為我設置了錯誤的位,我得到了一些 session 設置,但它沒有設置正確的條目!

add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
    // loop through cart items, passing the $item array by reference, so it can be updated
    foreach (WC()->cart->cart_contents as $key => &$item) {
        // set the values of the addons array as required
        $test = $_POST['cart'][$key]['addons'];
        $item['addons'][0]['value'] = $test;
        print_r(WC()->session->get( 'cart'));
    }
});

編輯 - 工作代碼:感謝@benJ 的解決方案,非常有幫助,需要設置 session,但不是我認為我必須這樣做的方式!

// hook on woocommerce_update_cart_action_cart_updated
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {
    // loop through cart items, passing the $item array by reference, so it can be updated
    foreach (WC()->cart->cart_contents as $key => &$item) {
        // set the values of the addons array as required
        $addon = $_POST['cart'][$key]['addons'];
        $item['addons'][0]['value'] = $addon;
        // set the session
        WC()->cart->set_session();
    }
});

干杯伊斯坎德爾

如果我正確理解您的問題,您希望在更新購物車時更新每個產品的插件字段。

這可以通過在woocommerce_update_cart_action_cart_updated上添加掛鈎並根據需要編輯購物車項目來實現。

// hook on woocommerce_update_cart_action_cart_updated
add_action('woocommerce_update_cart_action_cart_updated', function ($cartUpdated) {

    // loop through cart items, passing the $item array by reference, so it can be updated
    foreach (WC()->cart->cart_contents as $key => &$item) {
        // set the values of the addons array as required
        $item['addons'][0]['value'] = 'your message here';
    }
});

如果您只想對單個產品執行此操作,則可以在迭代它們時檢查它們的其他值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM