本文整理汇总了PHP中WC_Product_Variation::get_slug方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::get_slug方法的具体用法?PHP WC_Product_Variation::get_slug怎么用?PHP WC_Product_Variation::get_slug使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::get_slug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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'])) {
//.........这里部分代码省略.........
示例2: 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.
//.........这里部分代码省略.........