Some products have the standard SKU (inventory) field from woocommerce (SKU1) and an extra SKU (sku2) custom field. I added a mandatory selection box: "With barcode", "without barcode", if the user doesn't select something then "add to cart" button is disabled . If the user selects "with barcode" then the product will be added to the cart using SKU1 as SKU value otherwise if "without barcode" is selected then the product will be added to the cart using SKU2 as SKU value. If the product has only SKU1 or SKU2 then the selection box must not be shown and the user can directly click add to cart button.

Is it possible to achieve something like this? Preferably without the use of a plugin


What I have tried until now:

if the product has sku1 and sku2 and user selects "without barcode" then I store the old sku in a variable like this:

$old_sku = get_post_meta( $post->ID, '_sku', true );

Then I update _sku with sku2:

$sku2 = get_field('sku2', $product->ID); 
update_post_meta( $post->ID, '_sku', $sku2 );

And after user clicks Add to cart button then I restore sku back: update_post_meta( $post->ID, '_sku', $old_sku );

But the question is how I can update _sku with sku2 when the user clicks Add to cart ? And then restore sku back? And if I restore sku before user checks out then the order will be with sku2 or sku?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.