Can anyone help me with the below code? I am trying to add a custom purchase note to a newly created product only if the Product Name contains the string "Fiat".
add_action( 'transition_post_status', 'bbloomer_add_custom_meta_on_publish_product', 9999, 3 );
function bbloomer_add_custom_meta_on_publish_product( $new_status, $old_status, $post ) {
$product = wc_get_product( id );
$prod_name = $product->get_title();
$string_to_find = 'Fiat';
if ( 'product' === $post->post_type && 'publish' === $new_status && 'publish' !== $old_status && strpos( strtolower($product_name), $string_to_find ) === true ) {
update_post_meta( $post->ID, '_purchase_note', 'This is my Fiat text' );
}}
CodePudding user response:
add_action('woocommerce_before_product_object_save', 'save_product_before_action', 10, 2);
function save_product_before_action($product, $data_store) {
if ($product->get_id()) {
$prod_name = $product->get_name();
$string_to_find = 'Fiat';
if (strpos($prod_name, $string_to_find) !== false) {
$product->set_purchase_note('This is my Fiat text');
}
}
}