国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

如果產(chǎn)品未設(shè)置,則在保存時添加屬性:WooCommerce
P粉555682718
P粉555682718 2023-12-13 15:49:42
0
1
668

保存產(chǎn)品時,我想檢查該產(chǎn)品是否具有特定屬性。就我而言,pa_region。如果沒有,我想將屬性集和屬性術(shù)語添加到產(chǎn)品中。 如果屬性 pa_region 已設(shè)置,我不想更新/更改它。

我看到有一個名為 wp_set_object_terms 的函數(shù)(文檔)。我嘗試了一些方法,但我認(rèn)為 update_post_meta 是正確的方法。

從這個答案中我知道如何檢查產(chǎn)品是否具有屬性。我稍后會添加該檢查。

目前我嘗試首先添加該屬性。目前還無法正常工作。

我在這里發(fā)現(xiàn)了類似的問題,我嘗試使用該代碼來達(dá)到我的目的。但這不起作用。我猜原因是該功能需要產(chǎn)品中已有的屬性?! 編輯:我檢查過。即使在產(chǎn)品中設(shè)置了屬性 pa_region,代碼也不會更新它的值。

這是我當(dāng)前的代碼:

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

全部回復(fù)(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;
}
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板