How to pass an attribute from a HivePress listing into a WooCommerce snippet?

How to pass an attribute from a hivepress listing (dimension attribute in my case) into a woocommerce product when we create a new product via a snippet?

I would like to replace the content of my variable class_slug by the listing->dimension attribute.

add_action( 'woocommerce_new_product', 'set_default_shipping_class' );

function set_default_shipping_class( $product_id ) {
     $class_slug = 'classe-1'
    //wp_set_object_terms( $product_id, $class_slug, 'product_shipping_class' );
}

How to do it ?

Thanks in advance !

In your code snippet, it is possible to get the listing object in this way.

// Get product.
$product = wc_get_product( $product_id );

$listing = null;

// Get listing ID.
$listing_id = $product->get_parent_id();

if ( $listing_id ) {

// Get listing.
$listing = \HivePress\Models\Listing::query()->get_by_id( $listing_id );
}

Then it is possible to get listing attribute information in this way.

$listing->get_your_attribute_field_name();

Please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

Thank you for the first answer and for your reactivity! :smiley:

I adapted my snippet like this, the $listing object is not null, but impossible to get the dimension attribute or any of its other attributes for that matter. The ids of the retrieved listing and of the product are however existing and not null.

add_action( 'woocommerce_new_product', 'set_default_shipping_class' );

function set_default_shipping_class( $product_id ) {
	$_product = wc_get_product( $product_id );
	$_listing_id = $_product->get_parent_id();
	$_listing = HivePress\Models\Listing::query()->get_by_id($_listing_id);
	$class_slug = $_listing->get_dimension();
    wp_set_object_terms( $product_id, $class_slug, 'product_shipping_class' );
}

It’s work if a change $class_slug by an hard coded variable like ‘classe-1’ but when i try with a dynamic value, impossible.

Please ensure you put the correct attribute field name/slug in the code. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

I followed your instructions, unfortunately it still does not work. I have duplicated and adapted a snippet that works, which I use when I update a vendor. I took as reference your documentation:

https://hivepress.github.io/hook-reference/hivepress_v1_models_%7Bmodel_name%7D_%7Baction_type%7D.html

The hook is launched correctly, listing_id is also correct, but impossible to get anything from the listing object.

Here is the code that i used :

add_action('hivepress/v1/models/listing/create', 'update_product', 10, 2);

// remove action to execute the callback once
function update_product($listing_id, $listing) {
	remove_action('hivepress/v1/models/listing/create', 'update_product', 10, 2);
	
	// Send request.
	wp_remote_post(
		'URL',
		array(
                'body' => array(
					'dimension' =>  $listing->get_dimension(),
                )
            )
        );
}

Here is the declaration of the attribute that I want to retrieve.

I also tested with the price and the title, but nothing works either.

Please try to use this PHP snippet which works whenever the Dimension attribute value in a listing is updated.

add_action(
	'hivepress/v1/models/listing/update_dimension',
	function($listing_id, $value){
		// Send request.
		wp_remote_post(
		'URL',
		array(
                'body' => array(
					'dimension' =>  $value,
                )
            )
        );
	},
	1000,
	2
);

Because the hivepress/v1/models/listing/create hook returns a listing object without information as it calls when the auto-draft listing is created

But please note that it can require further customization. If you are not familiar with the code customization, then please consider hiring someone for custom work https://fvrr.co/32e7LvY

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.