If I need to offer a product in a WooCommerce product tab inside another product I can. This can be done with a WooCommerce shortcode
[product id="xxx"]
Now if you also want the customer to be able to add this product to the cart you can add a cart button and connect it to the product id like so
[add_to_cart id="xxx"]
However what I need is to have the sample product in tab to be connected to the parent product. Connected in the way that the main product SKU and and title are connected to the sample product in the tab. The sample product is a separate product this way now. But it is a sample of the main product.
Background
The reason to add it this way is to be able to not add hundreds of sample products besides the main product. And all these sample products cost the same and do not need a separate thumbnail per se.
Pass SKU and Title Main Product to Sample Product
Now, how can I on adding to cart add this sample product to be added with the name and SKU of the product its page it is displayed on in a WooCommerce Tab?
Example
So let's say a loaf of wholewheat bread SKU 1238, price €1.23, sample slice €0.2 - sku 125 - is added to cart with name Wholewheat Bread Sample SKU 1238, price €0.22.
Question in detail
Can I do this with this general sample product somehow? This so the seller sees what sample to send basically? By having the title and SKU of the product the sample is based on. And to not have to great hundreds of sample products?
If I need to offer a product in a WooCommerce product tab inside another product I can. This can be done with a WooCommerce shortcode
[product id="xxx"]
Now if you also want the customer to be able to add this product to the cart you can add a cart button and connect it to the product id like so
[add_to_cart id="xxx"]
However what I need is to have the sample product in tab to be connected to the parent product. Connected in the way that the main product SKU and and title are connected to the sample product in the tab. The sample product is a separate product this way now. But it is a sample of the main product.
Background
The reason to add it this way is to be able to not add hundreds of sample products besides the main product. And all these sample products cost the same and do not need a separate thumbnail per se.
Pass SKU and Title Main Product to Sample Product
Now, how can I on adding to cart add this sample product to be added with the name and SKU of the product its page it is displayed on in a WooCommerce Tab?
Example
So let's say a loaf of wholewheat bread SKU 1238, price €1.23, sample slice €0.2 - sku 125 - is added to cart with name Wholewheat Bread Sample SKU 1238, price €0.22.
Question in detail
Can I do this with this general sample product somehow? This so the seller sees what sample to send basically? By having the title and SKU of the product the sample is based on. And to not have to great hundreds of sample products?
Share Improve this question edited Feb 26, 2020 at 5:17 rhand asked Feb 26, 2020 at 5:05 rhandrhand 3742 gold badges14 silver badges31 bronze badges1 Answer
Reset to default 1I would therefore create a new short code based on the existing one
https://docs.woocommerce/wc-apidocs/source-class-WC_Shortcodes.html#295
Where before adding the product (sample) to the shopping cart the values of the sample product will be updated with the values of the (main) product.
then you could using something like this
$product = wc_get_product( $product_id );
$product->set_sku( 1234 );
$product->save();
href=”http://yourdomain/?add-to-cart=product_id″