从 WooCommerce 中链接产品的产品自定义字段中获取 ID
Get the id from a product custom field with linked products in WooCommerce
目标: 将产品 B 作为自定义链接产品添加到产品 A,并想获取 B 的产品 ID。为什么?我们需要创建一个包含产品 B 的 ID 的 link(或者最好是添加到购物车按钮),因为我们需要在产品的原始添加到购物车按钮之后插入这个 link A.
背景: 我们在 Woocommerce 的产品设置中添加了一个自定义 'Linked Product' 字段(类似于“ ”的实现),以便 link 两种产品:由于产品 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…', '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 对任何想法、建议和/或代码反馈感到非常高兴!谢谢
您的代码 #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>';
}
目标: 将产品 B 作为自定义链接产品添加到产品 A,并想获取 B 的产品 ID。为什么?我们需要创建一个包含产品 B 的 ID 的 link(或者最好是添加到购物车按钮),因为我们需要在产品的原始添加到购物车按钮之后插入这个 link A.
背景: 我们在 Woocommerce 的产品设置中添加了一个自定义 'Linked Product' 字段(类似于“
我们在产品设置中创建和保存自定义链接产品字段的代码(代码 #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…', '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 对任何想法、建议和/或代码反馈感到非常高兴!谢谢
您的代码 #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>';
}