【问题标题】:Get the id from a product custom field with linked products in WooCommerce从带有 WooCommerce 中链接产品的产品自定义字段中获取 id
【发布时间】:2021-05-05 15:57:38
【问题描述】:

目标: 将产品 B 作为自定义链接产品添加到产品 A 中,并希望获取 B 的产品 ID。为什么?我们需要创建一个包含产品 B 的 ID 的链接(或理想的添加到购物车按钮),因为我们需要在产品 A 的原始 add-to-cart-btn 之后插入此链接。

背景:我们在 Woocommerce 的产品设置中添加了一个自定义“链接产品”字段(实现类似于“How to add more custom field in Linked Product of Woocommerce”),以便链接两个产品:因为产品 B 将是除了产品 A,客户还可以添加到购物篮中。 我们在 function.php 中包含了一些代码,但不知何故我们无法将产品 B 的产品 ID 作为输出。

我们在产品设置中创建和保存自定义链接产品字段的代码(代码 #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,将其作为输出(代码 #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 作为输出 - 我们会

更改 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 对任何想法、提示和/或代码反馈都很满意!太棒了

【问题讨论】:

  • 使用 CODE #2,到目前为止,我们得到了“使用此测试以获取示例产品的 ID,即:数组”作为输出。

标签: php wordpress woocommerce product custom-fields


【解决方案1】:

您的代码 #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>';
}

【讨论】:

  • 谢谢,但是看起来我做错了,因为我仍然得到“Array”作为输出。如何确保它只是一个产品样本ID而不是更多ID?
猜你喜欢
  • 2019-06-25
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2014-07-04
相关资源
最近更新 更多