Last active
July 28, 2017 16:20
-
-
Save aczietlow/bd1e20fd483974f3efc7fdabae7101c5 to your computer and use it in GitHub Desktop.
Faster way of updating single field of an entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function updateProductCategoryReference($product_id, $new_cat_tid) { | |
$product = new stdClass(); | |
$product->product_id = $product_id; | |
$product->type = 'product'; | |
$product->field_product_category[LANGUAGE_NONE][]['tid'] = $new_cat_tid; | |
$transaction = db_transaction(); | |
try { | |
field_attach_presave('commerce_product', $product); | |
field_attach_update('commerce_product', $product); | |
entity_get_controller('commerce_product')->resetCache([$product_id]); | |
} | |
catch (\Exception $e) { | |
$transaction->rollback(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment