stast
stast

Reputation: 25

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

Aim: Added product B as custom Linked Product to product A, and want to get the product ID of B. WHY? We need to create a link (or ideally Add-to-cart Button) that contains this ID of product B beacause we need to insert this link after the original add-to-cart-btn of product A.

Background: We added a custom 'Linked Product' field within the product settings in Woocommerce (implementation similar to " How to add more custom field in Linked Product of Woocommerce ") in order to link two products: as product B will be an addition to product A, a customer may add to the basket. We included some code to the function.php but somehow we can't get the product ID of product B as output.

Our Code to create and save Custom Linked Product field within product setting (CODE #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 );
}

To get the produt ID we tested with this as 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>';
}

Later - once we geht the correct ID as output - we would

change CODE #2 and include the ID in a button (CODE #3) : maybe something like this

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>';
}

We managed to

BUT: Not getting the Product ID of Product B within the product page of Product A.

Suuuuper happy about any Ideas, tipps and/ or Code-feedback! THx

Upvotes: 2

Views: 526

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

Your code #2 should better be (as this custom field is an array of sample product ids):

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>';
}

And then you code #3 should be (Here we get the first sample product):

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>';
}

Upvotes: 2

Related Questions