No products in the cart.
Forum Replies Created
Viewing 2 posts - 1 through 2 (of 2 total)
- AuthorPosts
-
dee_ell
Participant'post_type' => 'any',
This gets all posts except revisions. https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
So your query would be:
query_posts(array( 'post_type' => 'any', 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_type' => array('posttype1', 'postype2', 'posttype3') ) );
November 7, 2019 at 3:11 am in reply to: Create programmatically a variable product and two new attributes in WooCommerce #10012dee_ell
ParticipantSarakinos answer only worked with two small but important modifications.
- The
$attribute->set_id()
needs to be set to0
. - The
$attribute->set_name();
and$variation->set_attributes()
do not need thepa_
prefix. The methods already handle the prefixes.
So a working code would be:
//Create main product $product = new WC_Product_Variable(); //Create the attribute object $attribute = new WC_Product_Attribute(); //pa_size tax id $attribute->set_id( 0 ); // -> SET to 0 //pa_size slug $attribute->set_name( 'size' ); // -> removed 'pa_' prefix //Set terms slugs $attribute->set_options( array( 'blue', 'grey' ) ); $attribute->set_position( 0 ); //If enabled $attribute->set_visible( 1 ); //If we are going to use attribute in order to generate variations $attribute->set_variation( 1 ); $product->set_attributes(array($attribute)); //Save main product to get its id $id = $product->save(); $variation = new WC_Product_Variation(); $variation->set_regular_price(10); $variation->set_parent_id($id); //Set attributes requires a key/value containing // tax and term slug $variation->set_attributes(array( 'size' => 'blue' // -> removed 'pa_' prefix )); //Save variation, returns variation id echo get_permalink ( $variation->save() ); // echo get_permalink( $id ); // -> returns a link to check the newly created product
Keep in mind, the product will be bare bones, and will be named ‘product’. You need to set those values in the
$product
before you save it with$id = $product->save();
. - The
- AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)