最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - How to remove product attribute row woocommerce using code

programmeradmin3浏览0评论

I am trying to remove an attribute row in a product attribute tab using code.

Woocommerce uses javascript to hide attribute row then uses ajax to save changes.

I need this row to be disappeared and removed after product page was refreshed.

Is there a function or a method to help me remove this row?

I am trying to remove an attribute row in a product attribute tab using code.

Woocommerce uses javascript to hide attribute row then uses ajax to save changes.

I need this row to be disappeared and removed after product page was refreshed.

Is there a function or a method to help me remove this row?

Share Improve this question asked Apr 16, 2019 at 19:59 MeMoMeMo 812 silver badges7 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

I have found a solution after some digging in database:

$product_attributes = get_post_meta( $product->id , '_product_attributes' );

                $temp_product_attributes = [];
                foreach($product_attributes as $product_attribute) {
                    foreach($product_attribute as $pt_k => $pt_v) {
                        if ($pt_k !== $attribute_name) {
                            $temp_product_attributes[$pt_k] = $pt_v;
                        }
                    }
                }
update_post_meta( get_the_ID(), '_product_attributes', $temp_product_attributes);

First we need to get all of product attributes then we traverse the array and remove the attribute we don't want any more (by the way I have store unwanted attribute in $attribute_name).

At the end we save the post meta with new product attributes without the one we wanted to eliminate.

$temp_product_attributes is for saving the attributes we already have.

发布评论

评论列表(0)

  1. 暂无评论