Auto set specific attribute term value to purchased products on Woocommerce

痴心易碎 提交于 2021-02-08 06:58:18

问题


I want to automatically add a specific attribute value (which was previously set up) to ordered products when the order is placed and has "on-hold" status.

I sell unique products and I have set up the "STOCK" attribute and the "Out Of Stock" (out-of-stock) value.

When an order is placed and has "on-hold" status, I want to automatically change the featured status of the ordered products and also to add the out-of-stock attribute value to it.

The featured part is done and works, but I can't figure out how to add a specific attribute value to the products.

Here's my code:

add_action('woocommerce_order_status_on-hold', 'order_status_on_hold_update_products', 20, 2);

function order_status_on_hold_update_products( $order_id, $order ) {
  foreach ( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product();
    $product->set_featured(true);
    $product->set_attributes(???); // I don't know if and how set_attributes() should be used
    $product->save();
}

回答1:


To set stock status "Out of Stock" you will use the WC_Product method set_stock_status() this way:

 $product->set_stock_status('outofstock'); // Or "instock"
 $product->save();

To set a product attribute term in your hooked function (work for variable products too):

add_action('woocommerce_order_status_on-hold', 'order_status_on_hold_update_products', 20, 2);
function order_status_on_hold_update_products( $order_id, $order ) {
    foreach ( $order->get_items() as $item_id => $item ) {
        $product = $item->get_product();

        // Handling variable products
        $_product = $product->is_type('variation') ? wc_get_product( $item->get_product_id() ) : $product;

        $_product->set_featured( true );

        // Your product attribute settings
        $taxonomy   = 'pa_stock'; // The taxonomy
        $term_name  = "Out Of Stock"; // The term

        $attributes = (array) $_product->get_attributes();
        $term_id    = get_term_by( 'name', $term_name, $taxonomy )->term_id;

        // 1) If The product attribute is set for the product
        if( array_key_exists( $taxonomy, $attributes ) ) {
            foreach( $attributes as $key => $attribute ){
                if( $key == $taxonomy ){
                    $attribute->set_options( array( $term_id ) );
                    $attributes[$key] = $attribute;
                    break;
                }
            }
            $_product->set_attributes( $attributes );
        }
        // 2. The product attribute is not set for the product
        else {
            $attribute = new WC_Product_Attribute();

            $attribute->set_id( sizeof( $attributes) + 1 );
            $attribute->set_name( $taxonomy );
            $attribute->set_options( array( $term_id ) );
            $attribute->set_position( sizeof( $attributes) + 1 );
            $attribute->set_visible( true );
            $attribute->set_variation( false );
            $attributes[] = $attribute;

            $_product->set_attributes( $attributes );
        }

        $_product->save();

        // Append the new term in the product
        if( ! has_term( $term_name, $taxonomy, $_product->get_id() ) )
            wp_set_object_terms($_product->get_id(), $term_slug, $taxonomy, true );
    }
}

Code goes in function.php file of your active child theme (or active theme). it should works.



来源:https://stackoverflow.com/questions/53944532/auto-set-specific-attribute-term-value-to-purchased-products-on-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!