I'm trying to write a function that should delete Amazon S3 objects within a designated bucket, when I delete a Woocommerce product(s). But I keep getting an fatal error.
Here's how I'm trying to accomplish this:
- The S3 objects (images) are used as Woocommerce downloadable product variations.
- Using Woocommerce's
before_delete_post
I loop through a products variations and trigger$s3->deleteObject
. - I have a custom field on each variation called
s3path
. This stores the path of the S3 object egpath/object.jpg
.
Here's my function so far. This is saved in functions.php
:
function delete_s3_product_images() {
global $product;
require ABSPATH . 'vendor/autoload.php';
$s3 = new Aws\S3\S3Client([
'region' => 'ap-southeast-2',
'version' => 'latest',
'credentials' => [
'key' => "--Amazon S3 Key--",
'secret' => "--Amazon S3 Secret--",
]
]);
$variations = $product->get_available_variations();
foreach ( $variations as $key => $value ) {
$result = $s3->deleteObject([
'Bucket' => '--Bucket Name--',
'Key' => $value['s3path'] //value outputs as "path/object.jpg"
]);
}
}
add_action( 'before_delete_post', 'delete_s3_product_images', 10, 1 );
Here's the error:
Fatal error: Uncaught Error: Call to a member function get_available_variations() on null
I'm assuming it's throwing the error because it thinks the $product
is empty. How can I retrieve the $product
correctly?
CodePudding user response:
Try the following
function delete_s3_product_images($id){
$product = wc_get_product($id);
if(!$product){
return;
}
require ABSPATH ...rest of the function
}
CodePudding user response:
You can create the product object using wc_get_product()
. The action [before_delete_post][1]
has 2 available parameters: $postid
and $post
. Use it to create the product object.
function delete_s3_product_images( $postid ) {
$product = wc_get_product( $postid );
if ( !$product ) { return; }
require ABSPATH . 'vendor/autoload.php';
$s3 = new Aws\S3\S3Client([
'region' => 'ap-southeast-2',
'version' => 'latest',
'credentials' => [
'key' => "--Amazon S3 Key--",
'secret' => "--Amazon S3 Secret--",
]
]);
$variations = $product->get_available_variations();
foreach ( $variations as $key => $value ) {
$result = $s3->deleteObject([
'Bucket' => '--Bucket Name--',
'Key' => $value['s3path'] //value outputs as "path/object.jpg"
]);
}
}
add_action( 'before_delete_post', 'delete_s3_product_images', 10, 1 );