I'm creating multiple woocommerce products programmatically.
In order to create translated products and according to WPML documentation i should use:
$my_translated_post = array(
'post_title' => $this->title_en,
'post_content' => $this->description_en,
'post_status' => $this->status,
'post_type' => 'product',
'post_author' => 1,
);
$translated_post_id = wp_insert_post( $my_translated_post );
$wpml_element_type = apply_filters( 'wpml_element_type', 'product' );
$get_language_args = array('element_id' => $post_id, 'element_type' => 'product' );
$original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );
$product = wc_get_product( $translated_post_id );
$set_language_args = array(
'element_id' => $translated_post_id,
'element_type' => $wpml_element_type,
'trid' => $original_post_language_info->trid,
'language_code' => 'en', //language code of secondary language
'source_language_code' => $original_post_language_info->language_code
);
do_action( 'wpml_set_element_language_details', $set_language_args );
$product->save();
which creates the product with title and description. However in order to "sync" prices, stock, images,categories and other metadata i have to go to "products" in the dashboard click quick edit and then click update without changing anything.
From what i understand there's a "trigger" somewhere in that process that "syncs" the translated product with its parent.
Any clues on how can i trigger this programmatically?
Related
Basically I'm trying to add a new bookable product in Woocommerce using a custom form I've made, so I have to add the products programmatically. Creating the product is fine, the problem is that I can't figure out how to add a new person type to my product so I can fix multiple prices.
Does someone have an idea how to do this?
This is the code I have so far.
$post_id = wp_insert_post( array(
'post_title' => $_POST["title"],
'post_content' => $_POST["description"],
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'booking', 'product_type' );
The following will enable "has persons" and create your person types for a bookable product:
// Create the product
$product_id = wp_insert_post( array(
'post_title' => $_POST["title"],
'post_content' => $_POST["description"],
'post_status' => 'publish',
'post_type' => "product",
) );
// Set the product type
wp_set_object_terms( $product_id, 'booking', 'product_type' );
// Get an instance of the WC_Product Object
$product = wc_get_product($product_id);
// Enable persons and save
$product->set_has_persons(true);
$product->save();
// Here define your person types (one array by person type)
$persons_type_data = array(
// First person type
array(
'block_cost' => 0,
'cost' => 16.50,
'description' => '',
'max' => '',
'min' => '',
'name' => __('Adults'),
),
// Second person type
array(
'block_cost' => 0,
'cost' => 9.20,
'description' => '',
'max' => '',
'min' => '',
'name' => __('Childs'),
),
);
// Loop Through persons type data
foreach( $persons_type_data as $key => $values ){
$person_type = new WC_Product_Booking_Person_Type();
$person_type->set_block_cost($values['block_cost']);
$person_type->set_cost($values['cost']);
$person_type->set_description($values['description']);
$person_type->set_max($values['max']);
$person_type->set_min($values['min']);
$person_type->set_name($values['name']);
$person_type->set_parent_id($product_id);
$person_type->set_sort_order($key); // Sorting is based on the array order
// Save the person type
$person_type->save();
// Add the person type to the array
$persons[] = $person_type;
}
// Set person types and save the product
$product->set_person_types($persons);
$product->save();
Tested and works.
When I execute the code below, there will be added a product to WooCommerce Products with variations of the attributes 'pa_size' and 'pa_color' after I pushed a button.
When I preview the product, everything works fine.
However, when I'm going to edit and save that product, there are no attributes existing. On top of that, the variations disappear when I'm saving the product again.
I've searched the internet for at least three days, so if someone knows a proper solution I would be grateful.
Code:
class Product {
function __construct () {
// In a class constructor
$this->size_tax = wc_attribute_taxonomy_name( 'Size' );
$this->color_tax = wc_attribute_taxonomy_name( 'Color' );
}
function addProduct() {
// Insert the main product first
// It will be used as a parent for other variations (think of it as a container)
$product_id = wp_insert_post( array(
'post_title' => "Product Example",
'post_content' => "Product post content goes here...",
'post_status' => "publish",
'post_excerpt' => "This is the description",
'post_name' => "test_prod_vars2", //name/slug
'post_type' => "product"
) );
// Insert the attributes (I will be using size and color for variations)
$attributes = array(
$this->size_tax => array(
'name' => $this->size_tax,
'value' =>'',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
),
$this->color_tax => array(
'name' => $this->color_tax,
'value' => '',
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
)
);
update_post_meta( $product_id, '_product_attributes', $attributes );
// Assign sizes and colors to the main product
// Set product type as variable
wp_set_object_terms( $product_id, 'variable', 'product_type', false );
// Start creating variations
$sizes = array('small', 'medium', 'large');
$prices = array('5', '10', '15');
$colors = array('red', 'white', 'blue');
for($i=0; $i<count($sizes); $i++) {
for($j=0; $j<count($colors); $j++) {
$parent_id = $product_id;
$variation = array(
'post_title' => 'Product #' . $parent_id . ' Variation',
'post_content' => '',
'post_status' => 'publish',
'post_parent' => $parent_id,
'post_type' => 'product_variation'
);
// The variation id
$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_price', $prices[$i] );
// Assign the size and color of this variation
update_post_meta( $variation_id, 'attribute_' . $this->size_tax, $sizes[$i] );
update_post_meta( $variation_id, 'attribute_' . $this->color_tax, $colors[$j] );
// Update parent if variable so price sorting works and stays in sync with the cheapest child
WC_Product_Variable::sync( $parent_id );
}
}
}
}
if(isset($_POST['button'])) {
$product = new Product;
$product->addProduct();
}
If your attribute
'is_taxonomy' => '1'
then you have to insert taxonomy slug instead of text based value.
Try 'is_taxonomy' => '0' as a fast check.
I am developing a Wordpress plugin. I have some links for showing product Brands name and Product categories listing, in the url: http://testsite.com/product-categories and http://testsite.com/brands like that. So I want this link will be auto generated when plugin is activated and place in the default menu in Wordpress. How can I do that. I tried this code but its not working.
register_activation_hook( __FILE__, array( $this, 'myplugin_activate' ) );
public function myplugin_activate() {
$product_categories = array(
'post_content' => '',
'post_name' => 'product-categories',
'post_title' => 'Product Categories',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
$brands = array(
'post_content' => '',
'post_name' => 'brands',
'post_title' => 'Brands',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
wp_insert_post( $product_categories );
wp_insert_post( $brands );
}
Can anyone tell me how to do that? One thing I can not do that as page post type.
Searching for wp_insert_post and nav_menu_item, I found Programmatically add a Navigation menu and menu items where wp_create_nav_menu is suggested.
Should be a matter of using the functions wp_get_nav_menu_object, wp_create_nav_menu, wp_update_nav_menu_item and get_nav_menu_locations.
I am using a acf frontend form to create posts. All the fields are working but on frontend I want a field that should automatically asiign category at backend to the post.
I have custom post type name "person".
For adding the post from frontend form, here is the code
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['fields']['field_53c8f0941cec0'] ,
'post_category' => array(43,47) ,
'post_type' => 'person' ,
'submit_value' => 'Submit' ,
);
// insert the post
$post_id = wp_insert_post( $post );
.
The custom taxonomy name for my "person" custom post type is "person_type"
All the fields get saved but category does not gets saved at backend.
ANy help is really appreciated.
This should be pretty straight
Use
'tax_input' => array( 'CUSTOM_TAXONOMY_NAME' => array( COMMA SEPARTED VALUES OF TERMS)),
Example
$post = array(
'post_status' => 'Pending' ,
'post_title' => $new_title ,
'post_type' => 'products' ,
'tax_input' => array( 'products_type' => array( 11,33)),
'post_content' => $contentBlock,
'submit_value' => 'Submit' ,
);
$term = get_term_by( 'slug', 'your custom term slug', 'your custom taxonomy' );
$term_id = $term->term_id;
'tax_input' => array( 'your taxonomy' => $term_id )
this may help
I use the code like below in a submit page.
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_excerpt' => $excerpt,
'post_category' => array($_POST['cat']),
'tags_input' => array($tags),
'post_status' => 'publish',
'post_type' => 'custom_post_type'
);
$pid = wp_insert_post($new_post);
The $_POST['cat'] fetches the correct `category id. My custom taxonomy is named directory.
I am able to view in the saved post in the backend but the category checkbox is not checked there and in the front end i am displaying the post under these custom category, but it doesn't appear.
Is there a way to save the custom category of a custom taxonomy properly.
To assign a taxonomy to a post:
$cat_ids = array( $new_cat_id );
$cat_ids = array_map('intval', $cat_ids);
wp_set_object_terms( $post_id, $cat_ids, 'my_tax_name' );