簡體   English   中英

從自定義 url 鏈接將購物車商品數據添加到 woocommerce

[英]Adding cart item data to woocommerce from custom url link

我已經為產品存檔頁面構建了自定義Buy Now按鈕,使用以下 URL 結構將產品添加到購物車:

www.mydomain.com/shop/?add-to-cart=30 <-product ID

它工作完美,但我需要使用鈎子woocommerce_add_cart_item_data並且該鈎子不響應我的 URL 請求。

這是我嘗試過的:

// Save the "grams_quantity" custom product field data in Cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_in_cart_the_custom_product_field', 10, 2 );
function save_in_cart_the_custom_product_field( $cart_item_data, $product_id ) {
    if( isset( $_POST['grams_quantity'] ) ) {
        $cart_item_data[ 'grams_quantity' ] = $_POST['grams_quantity'];

        // When add to cart action make an unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $_POST['grams_quantity'] );
    }
    return $cart_item_data;
}

該代碼在single-product.php頁面中運行良好,因為那里的按鈕不是自定義的。

這是我將自定義字段添加到存檔商店產品循環的方式:

// Add a product custom field "grams_quantity" that will update the displayed price
add_action('woocommerce_after_shop_loop_item', 'custom_shop_loop', 30);
function custom_shop_loop(){
    
        // get the current post/product ID
        $product_id = get_the_ID();
    
        // get the product based on the ID
        $product = wc_get_product( $product_id );
    
    if( $product->is_type( 'simple' ) ){


    // Only for products under certain category
    if ( ! ( has_term( 'מגשי פירות', 'product_cat', $product_id ) ) ) return;
    ?>
        <div class="grams-field">
            <label for="grams_quantity"><?php _e('גרמים: ','woocoomerce'); ?><span></span><br>
                <input type="number" step="100" name="grams_quantity" class="grams_quantity" id="grams_quantity" value="100" min="100">
            </label>
        </div><br>

    <?php
    }
}

現在,當我單擊“ Buy Now按鈕時,它會將我的產品添加到購物車,而沒有grams_quantity數據屬性。

如何在存檔商店頁面中正確使用woocommerce_add_cart_item_data掛鈎?

編輯:

密碼筆

得到它通過$_GET解決,不是最好的解決方案,但它的工作原理。

// Add a product custom field "grams_quantity" that will update the displayed price
add_action('woocommerce_after_shop_loop_item', 'custom_shop_loop', 30);
function custom_shop_loop(){
        global $wp;
       // get the "Checkout Page" URL
        $url = home_url( $wp->request );
        
    
        // get the current post/product ID
        $product_id = get_the_ID();
    
        // get the product based on the ID
        $product = wc_get_product( $product_id );
    
    if( $product->is_type( 'simple' ) ){


    // Only for products sold by gram
    if ( ! ( has_term( 'מגשי פירות', 'product_cat', $product_id ) ) ) return;
    ?>
        <style>
            .pewc-total-field{
                display: none;
            }
        </style>
        <div class="grams-field">
            <label for="grams_quantity"><?php _e('גרמים: ','woocoomerce'); ?><span></span><br>
                <input type="number" step="100" name="grams_quantity" class="grams_quantity" id="grams_quantity<?php echo $product_id; ?>" value="100" min="100">
            </label>
            <span class="custom-price-shop<?php echo $product_id; ?>"></span>
        </div><br>
        <a style="display: block; text-align: center;" href="<?php echo $url; ?>/?add-to-cart=<?php echo $product_id; ?>" class="buy-now button buy-<?php echo $product_id; ?>">קנה עכשיו</a>

        <script type="text/javascript">
            (function($){
                // variables initialization
                var priceByGram = <?php echo $product->get_price(); ?>,
                    currencySymbol = $(".woocommerce-Price-currencySymbol").html(),
                    updatedPrice;
                console.log(priceByGram);
                $(".custom-price-shop<?php echo $product_id; ?>").html('סך הכל: ' + priceByGram * 100 +'&nbsp;'+currencySymbol);

                // On live event: imput number fields
                $('input#grams_quantity<?php echo $product_id; ?>').on("change", function() {
                    if (+$(this).val() > 500) {
                        $("#grams_quantity<?php echo $product_id; ?>").attr('step', 500);
                        $("#grams_quantity<?php echo $product_id; ?>").attr('min', 0);
                        if (+this.value < 1000) this.value = 1000;
                        this.value = Math.round(this.value / 500) * 500;
                    }

                    if (+$(this).val() <= 500) {
                        $("#grams_quantity<?php echo $product_id; ?>").attr('step', 100);
                        $("#grams_quantity<?php echo $product_id; ?>").attr('min', 100);
                        this.value = Math.max(100, Math.round(this.value / 100) * 100);
                    }
                });
                $('input#grams_quantity<?php echo $product_id; ?>').on( "click blur", function(){
                    updatedPrice = ($(this).val() * priceByGram).toFixed(2);
                    $(".custom-price-shop<?php echo $product_id; ?>").html('סך הכל: ' + updatedPrice +'&nbsp;'+currencySymbol);
                });
                $('.buy-<?php echo $product_id; ?>').on("click", function(e){
                    e.preventDefault();
                    $link = $(this).attr("href");
                    $grams = $("#grams_quantity<?php echo $product_id; ?>").val();
                    window.location.href= $link + "&grams=" + $grams;
                });
            })(jQuery);
        </script>
    <?php
    }
}

暫無
暫無

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

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