簡體   English   中英

從 WooCommerce 中鏈接產品的產品自定義字段中獲取 id

[英]Get the id from a product custom field with linked products in WooCommerce

目的:將產品B作為自定義鏈接產品添加到產品A,並希望獲得B的產品ID。為什么? 我們需要創建一個包含產品 B 的 ID 的鏈接(或理想的添加到購物車按鈕),因為我們需要在產品 A 的原始 add-to-cart-btn 之后插入此鏈接。

背景:我們在 Woocommerce 的產品設置中添加了一個自定義的“鏈接產品”字段(實現類似於“ 如何在 Woocommerce 的鏈接產品中添加更多自定義字段”),以便鏈接兩個產品:因為產品 B 將添加到產品 A,客戶可以添加到購物籃中。 我們在 function.php 中包含了一些代碼,但不知何故我們無法將產品 B 的產品 ID 設為 output。

我們在產品設置中創建和保存自定義鏈接產品字段的代碼(代碼 #1):

add_action( 'woocommerce_product_options_related', 'custom_woocommerce_linked_products_data_field' );
add_action( 'woocommerce_process_product_meta', 'custom_woocommerce_linked_products_data_field_save' );

function custom_woocommerce_linked_products_data_field() {
    global $woocommerce, $post;
?>
<p class="form-field">
    <label for="sample_products"><?php _e( 'Sample Product', 'woocommerce' ); ?></label>
    <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="sample_products" name="sample_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
        <?php
            $sample_product_ids = get_post_meta( $post->ID, '_sample_products_ids', true );

            foreach ( $sample_product_ids as $sample_product_id ) {
                $product = wc_get_product( $sample_product_id );
                if ( is_object( $product ) ) {
                    echo '<option value="' . esc_attr( $sample_product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
                }
            }
        ?>
    </select> <?php echo wc_help_tip( __( 'Select a Sample Products here.', 'woocommerce' ) ); ?>
</p>
<?php
}

function custom_woocommerce_linked_products_data_field_save( $post_id ){
    $product_field_type =  $_POST['sample_products'];
    update_post_meta( $post_id, '_sample_products_ids', $product_field_type );
}

為了得到我們測試的產品 ID output (CODE #2):

add_action( 'woocommerce_single_product_summary', 'custom_sample_product_button_after_cart', 30 );

function custom_sample_product_button_after_cart() {
    global $woocommerce, $post;
    $sample_id = get_post_meta( get_the_ID(), '_sample_products_ids', true);
        echo '<div>Testing with this to get the ID of the sample product, which is: ' . $sample_id . '</div>';
}

稍后 - 一旦我們得到正確的 ID 為 output - 我們會

更改 CODE #2 並將 ID 包含在按鈕中(CODE #3):可能是這樣的

add_action( 'woocommerce_single_product_summary', 'custom_button_after_product_summary', 30 );

function custom_sample_product_button_after_cart() {
  global $product;
  echo '<a href="https://example.com/page_of_product_a/?add-to-cart=' . $sample_id . '&quantity=1">Add Product B</a>';
}

我們設法

  • 創建自定義鏈接產品字段
  • 添加並保存我們字段的數據(=將產品B連接到產品A)
  • 在單個產品頁面上的“添加到購物車”按鈕之后(產品 A

但是:沒有在產品 A 的產品頁面中獲取產品 B 的產品 ID。

Suuuuper 對任何想法、提示和/或代碼反饋感到高興! 謝謝

您的代碼 #2 應該更好(因為此自定義字段是示例產品 ID 的數組)

add_action( 'woocommerce_single_product_summary', 'custom_sample_product_button_after_cart', 30 );
function custom_sample_product_button_after_cart() {
    global $product;

    $sample_products_ids = (array) $product->get_meta('_sample_products_ids');

    $sample_products_ids = implode( ', ', $sample_products_ids );

    echo '<div>Testing with this to get the IDs of the sample products, which are: ' . $sample_products_ids . '</div>';
}

然后你的代碼 #3 應該是(這里我們得到第一個示例產品)

add_action( 'woocommerce_single_product_summary', 'custom_button_after_product_summary', 30 );
function custom_sample_product_button_after_cart() {
    global $product;

    $sample_products_ids = (array) $product->get_meta('_sample_products_ids');

    // Display an add to cart button for the first product sample
    echo '<a href="https://example.com/page_of_product_a/?add-to-cart=' . reset(sample_products_ids); . '&quantity=1">' . __("Add Product B") . '</a>';
}

暫無
暫無

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

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