本文整理汇总了PHP中WC_Product_Variation::set_parent_id方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::set_parent_id方法的具体用法?PHP WC_Product_Variation::set_parent_id怎么用?PHP WC_Product_Variation::set_parent_id使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::set_parent_id方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1:
function test_varation_save_attributes()
{
// Create a variable product with a color attribute.
$product = new WC_Product_Variable();
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name('color');
$attribute->set_options(explode(WC_DELIMITER, 'green | red'));
$attribute->set_visible(true);
$attribute->set_variation(true);
$product->set_attributes(array($attribute));
$product->save();
// Create a new variation with the color 'green'.
$variation = new WC_Product_Variation();
$variation->set_parent_id($product->get_id());
$variation->set_attributes(array('color' => 'green'));
$variation->set_status('private');
$variation->save();
// Now update some value unrelated to attributes.
$variation = wc_get_product($variation->get_id());
$variation->set_status('publish');
$variation->save();
// Load up the updated variation and verify that the saved state is correct.
$loaded_variation = wc_get_product($variation->get_id());
$this->assertEquals('publish', $loaded_variation->get_status('edit'));
$_attribute = $loaded_variation->get_attributes('edit');
$this->assertEquals('green', $_attribute['color']);
}
示例2: prepare_item_for_database
/**
* Prepare a single variation for create or update.
*
* @param WP_REST_Request $request Request object.
* @return WP_Error|stdClass $data Post object.
*/
protected function prepare_item_for_database($request)
{
if (isset($request['id'])) {
$variation = wc_get_product(absint($request['id']));
} else {
$variation = new WC_Product_Variation();
}
$variation->set_parent_id(absint($request['product_id']));
/**
* Filter the query_vars used in `get_items` for the constructed query.
*
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
* prepared for insertion.
*
* @param WC_Product_Variation $variation An object representing a single item prepared
* for inserting or updating the database.
* @param WP_REST_Request $request Request object.
*/
return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}", $variation, $request);
}
示例3: link_all_variations
/**
* Link all variations via ajax function.
*/
public static function link_all_variations()
{
check_ajax_referer('link-variations', 'security');
if (!current_user_can('edit_products')) {
die(-1);
}
wc_maybe_define_constant('WC_MAX_LINKED_VARIATIONS', 49);
wc_set_time_limit(0);
$post_id = intval($_POST['post_id']);
if (!$post_id) {
die;
}
$variations = array();
$product = wc_get_product($post_id);
if ($product->is_type('variable')) {
$attributes = wc_list_pluck(array_filter($product->get_attributes(), 'wc_attributes_array_filter_variation'), 'get_slugs');
if (!empty($attributes)) {
// Get existing variations so we don't create duplicates.
$existing_variations = array_map('wc_get_product', $product->get_children());
$existing_attributes = array();
foreach ($existing_variations as $existing_variation) {
$existing_attributes[] = $existing_variation->get_attributes();
}
$added = 0;
$possible_attributes = wc_array_cartesian($attributes);
foreach ($possible_attributes as $possible_attribute) {
if (in_array($possible_attribute, $existing_attributes)) {
continue;
}
$variation = new WC_Product_Variation();
$variation->set_parent_id($post_id);
$variation->set_attributes($possible_attribute);
do_action('product_variation_linked', $variation->save());
if ($added++ > WC_MAX_LINKED_VARIATIONS) {
break;
}
}
echo $added;
}
}
die;
}
示例4: save_variations_data
/**
* Save variations.
*
* @throws WC_REST_Exception REST API exceptions.
* @param WC_Product $product Product instance.
* @param WP_REST_Request $request Request data.
* @param bool $single_variation True if saving only a single variation.
* @return bool
*/
protected function save_variations_data($product, $request, $single_variation = false)
{
global $wpdb;
if ($single_variation) {
$variations = array($request);
} else {
$variations = $request['variations'];
}
foreach ($variations as $menu_order => $data) {
$variation_id = isset($data['id']) ? absint($data['id']) : 0;
$variation = new WC_Product_Variation($variation_id);
// Create initial name and status.
if (!$variation->get_slug()) {
/* translators: 1: variation id 2: product name */
$variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
$variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
}
// Parent ID.
$variation->set_parent_id($product->get_id());
// Menu order.
$variation->set_menu_order($menu_order);
// Status.
if (isset($data['visible'])) {
$variation->set_status(false === $data['visible'] ? 'private' : 'publish');
}
// SKU.
if (isset($data['sku'])) {
$variation->set_sku(wc_clean($data['sku']));
}
// Thumbnail.
if (isset($data['image']) && is_array($data['image'])) {
$image = $data['image'];
$image = current($image);
if (is_array($image)) {
$image['position'] = 0;
}
$variation = $this->save_product_images($variation, array($image));
}
// Virtual variation.
if (isset($data['virtual'])) {
$variation->set_virtual($data['virtual']);
}
// Downloadable variation.
if (isset($data['downloadable'])) {
$variation->set_downloadable($data['downloadable']);
}
// Downloads.
if ($variation->get_downloadable()) {
// Downloadable files.
if (isset($data['downloads']) && is_array($data['downloads'])) {
$variation = $this->save_downloadable_files($variation, $data['downloads']);
}
// Download limit.
if (isset($data['download_limit'])) {
$variation->set_download_limit($data['download_limit']);
}
// Download expiry.
if (isset($data['download_expiry'])) {
$variation->set_download_expiry($data['download_expiry']);
}
}
// Shipping data.
$variation = $this->save_product_shipping_data($variation, $data);
// Stock handling.
if (isset($data['manage_stock'])) {
$variation->set_manage_stock($data['manage_stock']);
}
if (isset($data['in_stock'])) {
$variation->set_stock_status(true === $data['in_stock'] ? 'instock' : 'outofstock');
}
if (isset($data['backorders'])) {
$variation->set_backorders($data['backorders']);
}
if ($variation->get_manage_stock()) {
if (isset($data['stock_quantity'])) {
$variation->set_stock_quantity($data['stock_quantity']);
} elseif (isset($data['inventory_delta'])) {
$stock_quantity = wc_stock_amount($variation->get_stock_amount());
$stock_quantity += wc_stock_amount($data['inventory_delta']);
$variation->set_stock_quantity($stock_quantity);
}
} else {
$variation->set_backorders('no');
$variation->set_stock_quantity('');
}
// Regular Price.
if (isset($data['regular_price'])) {
$variation->set_regular_price($data['regular_price']);
}
// Sale Price.
if (isset($data['sale_price'])) {
//.........这里部分代码省略.........
示例5: save_variations
/**
* Save variations
*
* @since 2.2
* @param WC_Product $product
* @param array $request
* @return WC_Product
* @throws WC_API_Exception
*/
protected function save_variations($product, $request)
{
global $wpdb;
$id = $product->get_id();
$attributes = $product->get_variation_attributes();
foreach ($request['variations'] as $menu_order => $data) {
$variation_id = isset($data['id']) ? absint($data['id']) : 0;
$variation = new WC_Product_Variation($variation_id);
// Create initial name and status.
if (!$variation->get_slug()) {
/* translators: 1: variation id 2: product name */
$variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
$variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
}
// Parent ID.
$variation->set_parent_id($product->get_id());
// Menu order.
$variation->set_menu_order($menu_order);
// Status.
if (isset($data['visible'])) {
$variation->set_status(false === $data['visible'] ? 'private' : 'publish');
}
// SKU.
if (isset($data['sku'])) {
$variation->set_sku(wc_clean($data['sku']));
}
// Thumbnail.
if (isset($data['image']) && is_array($data['image'])) {
$image = current($data['image']);
if (is_array($image)) {
$image['position'] = 0;
}
$variation = $this->save_product_images($variation, array($image));
}
// Virtual variation.
if (isset($data['virtual'])) {
$variation->set_virtual($data['virtual']);
}
// Downloadable variation.
if (isset($data['downloadable'])) {
$is_downloadable = $data['downloadable'];
$variation->set_downloadable($is_downloadable);
} else {
$is_downloadable = $variation->get_downloadable();
}
// Downloads.
if ($is_downloadable) {
// Downloadable files.
if (isset($data['downloads']) && is_array($data['downloads'])) {
$variation = $this->save_downloadable_files($variation, $data['downloads']);
}
// Download limit.
if (isset($data['download_limit'])) {
$variation->set_download_limit($data['download_limit']);
}
// Download expiry.
if (isset($data['download_expiry'])) {
$variation->set_download_expiry($data['download_expiry']);
}
}
// Shipping data.
$variation = $this->save_product_shipping_data($variation, $data);
// Stock handling.
$manage_stock = (bool) $variation->get_manage_stock();
if (isset($data['managing_stock'])) {
$manage_stock = $data['managing_stock'];
}
$variation->set_manage_stock($manage_stock);
$stock_status = $variation->get_stock_status();
if (isset($data['in_stock'])) {
$stock_status = true === $data['in_stock'] ? 'instock' : 'outofstock';
}
$variation->set_stock_status($stock_status);
$backorders = $variation->get_backorders();
if (isset($data['backorders'])) {
$backorders = $data['backorders'];
}
$variation->set_backorders($backorders);
if ($manage_stock) {
if (isset($data['stock_quantity'])) {
$variation->set_stock_quantity($data['stock_quantity']);
}
} else {
$variation->set_backorders('no');
$variation->set_stock_quantity('');
}
// Regular Price.
if (isset($data['regular_price'])) {
$variation->set_regular_price($data['regular_price']);
}
// Sale Price.
//.........这里部分代码省略.........