本文整理汇总了PHP中WC_Product_Variation::get_id方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::get_id方法的具体用法?PHP WC_Product_Variation::get_id怎么用?PHP WC_Product_Variation::get_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::get_id方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: array
/**
* Test variable and variations.
*
* @since 2.7.0
*/
function test_variables_and_variations()
{
$product = new WC_Product_Variable();
$product->set_name('Variable Product');
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name('pa_color');
$attribute->set_options(explode(WC_DELIMITER, 'green | red'));
$attribute->set_visible(false);
$attribute->set_variation(true);
$product->set_attributes(array($attribute));
$product->save();
$this->assertEquals('Variable Product', $product->get_name());
$variation = new WC_Product_Variation();
$variation->set_name('Variation #1 of Dummy Variable CRUD Product');
$variation->set_parent_id($product->get_id());
$variation->set_regular_price(10);
$variation->set_sku('CRUD DUMMY SKU VARIABLE GREEN');
$variation->set_manage_stock('no');
$variation->set_downloadable('no');
$variation->set_virtual('no');
$variation->set_stock_status('instock');
$variation->set_attributes(array('pa_color' => 'green'));
$variation->save();
$this->assertEquals('Variation #1 of Dummy Variable CRUD Product', $variation->get_name());
$this->assertEquals('CRUD DUMMY SKU VARIABLE GREEN', $variation->get_sku());
$this->assertEquals(10, $variation->get_price());
$product = new WC_Product_Variable($product->get_id());
$children = $product->get_children();
$this->assertEquals($variation->get_id(), $children[0]);
$expected_attributes = array('pa_color' => array('green'));
$this->assertEquals($expected_attributes, $product->get_variation_attributes());
$variation_2 = new WC_Product_Variation();
$variation_2->set_name('Variation #2 of Dummy Variable CRUD Product');
$variation_2->set_parent_id($product->get_id());
$variation_2->set_regular_price(10);
$variation_2->set_sku('CRUD DUMMY SKU VARIABLE RED');
$variation_2->set_manage_stock('no');
$variation_2->set_downloadable('no');
$variation_2->set_virtual('no');
$variation_2->set_stock_status('instock');
$variation_2->set_attributes(array('pa_color' => 'red'));
$variation_2->save();
$this->assertEquals('Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name());
$this->assertEquals('CRUD DUMMY SKU VARIABLE RED', $variation_2->get_sku());
$this->assertEquals(10, $variation_2->get_price());
$product = new WC_Product_Variable($product->get_id());
$children = $product->get_children();
$this->assertEquals($variation_2->get_id(), $children[1]);
$this->assertEquals(2, count($children));
$expected_attributes = array('pa_color' => array('green', 'red'));
$this->assertEquals($expected_attributes, $product->get_variation_attributes());
$variation_2->set_name('UPDATED - Variation #2 of Dummy Variable CRUD Product');
$variation_2->set_regular_price(15);
$variation_2->set_sale_price(9.99);
$variation_2->set_date_on_sale_to('32532537600');
$variation_2->save();
$product = new WC_Product_Variable($product->get_id());
$expected_prices['price'][$children[0]] = 10.0;
$expected_prices['price'][$children[1]] = 9.99;
$expected_prices['regular_price'][$children[0]] = 10.0;
$expected_prices['regular_price'][$children[1]] = 15.0;
$expected_prices['sale_price'][$children[0]] = 10.0;
$expected_prices['sale_price'][$children[1]] = 9.99;
$this->assertEquals($expected_prices, $product->get_variation_prices());
$this->assertEquals('UPDATED - Variation #2 of Dummy Variable CRUD Product', $variation_2->get_name());
$product->set_name('Renamed Variable Product');
$product->save();
$this->assertEquals('Renamed Variable Product', $product->get_name());
$product->delete();
}
示例2: 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'])) {
//.........这里部分代码省略.........
示例3: get_attributes
/**
* Get the attributes for a product or product variation
*
* @since 2.1
* @param WC_Product|WC_Product_Variation $product
* @return array
*/
private function get_attributes($product)
{
$attributes = array();
if ($product->is_type('variation')) {
// variation attributes
foreach ($product->get_variation_attributes() as $attribute_name => $attribute) {
// taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
$attributes[] = array('name' => ucwords(str_replace('attribute_', '', str_replace('pa_', '', $attribute_name))), 'option' => $attribute);
}
} else {
foreach ($product->get_attributes() as $attribute) {
$attributes[] = array('name' => ucwords(str_replace('pa_', '', $attribute['name'])), 'position' => $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute));
}
}
return $attributes;
}
示例4: 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();
$variations = $request['variations'];
$attributes = (array) maybe_unserialize(get_post_meta($id, '_product_attributes', true));
foreach ($variations as $menu_order => $variation) {
$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']);
} 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('');
}
//.........这里部分代码省略.........