Attribut beim Speichern hinzufügen, wenn das Produkt nicht festgelegt ist: WooCommerce
P粉555682718
P粉555682718 2023-12-13 15:49:42
0
1
443

Wenn ich ein Produkt speichere, möchte ich prüfen, ob das Produkt ein bestimmtes Attribut hat. Soweit es mich betrifft, pa_region. Wenn nicht, möchte ich dem Produkt Attributsätze und Attributbegriffe hinzufügen. Wenn die Eigenschaft pa_region bereits festgelegt ist, möchte ich sie nicht aktualisieren/ändern.

Ich sehe, es gibt eine Methode namens wp_set_object_terms 的函数(文档)。我尝试了一些方法,但我认为 update_post_meta, die der richtige Weg ist.

Aus dieser Antwort weiß ich, wie ich überprüfen kann, ob ein Produkt ein Attribut hat. Ich werde diesen Scheck später hinzufügen.

Derzeit versuche ich zuerst, das Attribut hinzuzufügen. Funktioniert noch nicht richtig.

Ich habe hier eine ähnliche Frage gefunden und versucht, diesen Code zu verwenden, um mein Ziel zu erreichen. Aber das funktioniert nicht. Ich vermute, der Grund liegt darin, dass die Funktion Eigenschaften erfordert, die bereits im Produkt vorhanden sind? ! Bearbeiten: Ich habe nachgesehen. Obwohl das Attribut pa_region im Produkt festgelegt ist, aktualisiert der Code seinen Wert nicht.

Das ist mein aktueller Code:

add_action('woocommerce_update_product', 'save_product_region');
function save_product_region( $post )
{
    if( in_array( $post->post_type, array( 'product' ) ) ){

        $test = 'test';
        $product_id = $post->ID;

        $product_attributes = get_post_meta( $product_id ,'_product_attributes', true);
        var_dump($product_attributes);

        // Loop through product attributes
        foreach( $product_attributes as $attribute => $attribute_data ) {
            // Target specif attribute  by its name
            if( 'pa_region' === $attribute_data['name'] ) {
                // Set the new value in the array
                $product_attributes[$attribute]['value'] = $test;
                break; // stop the loop
            }
        }

        update_post_meta( $product_id ,'_product_attributes', $product_attributes );

    }
}

P粉555682718
P粉555682718

Antworte allen(1)
P粉520204081

第一个 $post 不是对象。将返回 ID,这很好。

add_action('woocommerce_update_product', 'save_product_region');
function save_product_region( $product_id ) {

    //Get product object from the ID
    $_product = wc_get_product($product_id);
    $attributes = $_product->get_attributes();

    $add_option = wp_set_object_terms( $product_id, 'canada', 'pa_region', true );
    $curr_options = $attributes['pa_region']['options'];
    
    //Check if we have this attribute set already 
    if(!in_array($add_option,$curr_options)):
        $updated_options = array_push($curr_options,$add_option);
        $data = array(
            'pa_region' => array(
                'name'=>'pa_region',
                'options'=> $updated_options,
                'is_visible' => '1',
                'is_variation' => '0',
                'is_taxonomy' => '1'
            )
        );
        //First getting the Post Meta
        $_product_attributes = get_post_meta($product_id, '_product_attributes', TRUE);
        //Updating the Post Meta
        update_post_meta($product_id, '_product_attributes', array_merge($_product_attributes, $data));
    endif;
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!