本文整理汇总了PHP中wc_get_product_id_by_sku函数的典型用法代码示例。如果您正苦于以下问题:PHP wc_get_product_id_by_sku函数的具体用法?PHP wc_get_product_id_by_sku怎么用?PHP wc_get_product_id_by_sku使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wc_get_product_id_by_sku函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_wc_get_product_id_by_sku
/**
* Test wc_get_product_id_by_sku()
*
* @since 2.3
*/
public function test_wc_get_product_id_by_sku()
{
// Create product
$product = \WC_Helper_Product::create_simple_product();
$this->assertEquals($product->id, wc_get_product_id_by_sku($product->sku));
// Delete Product
\WC_Helper_Product::delete_product($product->id);
}
示例2: get_product_sku
public function get_product_sku($skus = array())
{
$return = array();
foreach ($skus as $sku) {
if (empty($sku)) {
continue;
}
$return[] = wc_get_product_id_by_sku($sku);
}
return $return;
}
示例3: bulk
/**
* Bulk update or insert products
* Accepts an array with products in the formats supported by
* WC_API_Products->create_product() and WC_API_Products->edit_product()
*
* @since 2.4.0
* @param array $data
* @return array
*/
public function bulk($data)
{
try {
if (!isset($data['products'])) {
throw new WC_API_Exception('woocommerce_api_missing_products_data', sprintf(__('No %1$s data specified to create/edit %1$s', 'woocommerce'), 'products'), 400);
}
$data = $data['products'];
$limit = apply_filters('woocommerce_api_bulk_limit', 100, 'products');
// Limit bulk operation
if (count($data) > $limit) {
throw new WC_API_Exception('woocommerce_api_products_request_entity_too_large', sprintf(__('Unable to accept more than %s items for this request', 'woocommerce'), $limit), 413);
}
$products = array();
foreach ($data as $_product) {
$product_id = 0;
$product_sku = '';
// Try to get the product ID
if (isset($_product['id'])) {
$product_id = intval($_product['id']);
}
if (!$product_id && isset($_product['sku'])) {
$product_sku = wc_clean($_product['sku']);
$product_id = wc_get_product_id_by_sku($product_sku);
}
// Product exists / edit product
if ($product_id) {
$edit = $this->edit_product($product_id, array('product' => $_product));
if (is_wp_error($edit)) {
$products[] = array('id' => $product_id, 'sku' => $product_sku, 'error' => array('code' => $edit->get_error_code(), 'message' => $edit->get_error_message()));
} else {
$products[] = $edit['product'];
}
} else {
$new = $this->create_product(array('product' => $_product));
if (is_wp_error($new)) {
$products[] = array('id' => $product_id, 'sku' => $product_sku, 'error' => array('code' => $new->get_error_code(), 'message' => $new->get_error_message()));
} else {
$products[] = $new['product'];
}
}
}
return array('products' => apply_filters('woocommerce_api_products_bulk_response', $products, $this));
} catch (WC_API_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}
示例4: set_line_item
/**
* Create or update a line item.
*
* @param WC_Order $order Order data.
* @param array $item Line item data.
* @param string $action 'create' to add line item or 'update' to update it.
* @throws WC_REST_Exception Invalid data, server error.
*/
protected function set_line_item($order, $item, $action = 'create')
{
$creating = 'create' === $action;
$item_args = array();
// Product is always required.
if (empty($item['product_id']) && empty($item['sku']) && empty($item['variation_id'])) {
throw new WC_REST_Exception('woocommerce_rest_required_product_reference', __('Product ID or SKU is required.', 'woocommerce'), 400);
}
if (!empty($item['sku'])) {
$product_id = (int) wc_get_product_id_by_sku($item['sku']);
} elseif (!empty($item['product_id']) && empty($item['variation_id'])) {
$product_id = (int) $item['product_id'];
} elseif (!empty($item['variation_id'])) {
$product_id = (int) $item['variation_id'];
}
// When updating, ensure product ID provided matches.
if ('update' === $action && !empty($item['id'])) {
$item_product_id = (int) wc_get_order_item_meta($item['id'], '_product_id');
$item_variation_id = (int) wc_get_order_item_meta($item['id'], '_variation_id');
if ($product_id !== $item_product_id && $product_id !== $item_variation_id) {
throw new WC_REST_Exception('woocommerce_rest_required_product_reference', __('Product ID or variation ID provided does not match this line item.', 'woocommerce'), 400);
}
}
$product = wc_get_product($product_id);
// Must be a valid WC_Product.
if (!is_object($product)) {
throw new WC_REST_Exception('woocommerce_rest_invalid_product', __('Product is invalid.', 'woocommerce'), 400);
}
// Quantity must be positive float.
if (isset($item['quantity']) && 0 >= floatval($item['quantity'])) {
throw new WC_REST_Exception('woocommerce_rest_invalid_product_quantity', __('Product quantity must be a positive float.', 'woocommerce'), 400);
}
// Quantity is required when creating.
if ($creating && !isset($item['quantity'])) {
throw new WC_REST_Exception('woocommerce_rest_invalid_product_quantity', __('Product quantity is required.', 'woocommerce'), 400);
}
// Get variation attributes.
if (method_exists($product, 'get_variation_attributes')) {
$item_args['variation'] = $product->get_variation_attributes();
}
// Quantity.
if (isset($item['quantity'])) {
$item_args['qty'] = $item['quantity'];
}
// Total.
if (isset($item['total'])) {
$item_args['totals']['total'] = floatval($item['total']);
}
// Total tax.
if (isset($item['total_tax'])) {
$item_args['totals']['tax'] = floatval($item['total_tax']);
}
// Subtotal.
if (isset($item['subtotal'])) {
$item_args['totals']['subtotal'] = floatval($item['subtotal']);
}
// Subtotal tax.
if (isset($item['subtotal_tax'])) {
$item_args['totals']['subtotal_tax'] = floatval($item['subtotal_tax']);
}
if ($creating) {
$item_id = $order->add_product($product, $item_args['qty'], $item_args);
if (!$item_id) {
throw new WC_REST_Exception('woocommerce_rest_cannot_create_line_item', __('Cannot create line item, try again.', 'woocommerce'), 500);
}
} else {
$item_id = $order->update_product($item['id'], $product, $item_args);
if (!$item_id) {
throw new WC_REST_Exception('woocommerce_rest_cannot_update_line_item', __('Cannot update line item, try again.', 'woocommerce'), 500);
}
}
}
示例5: product_add_to_cart_url
/**
* Get the add to cart URL for a product
*
* @param array $atts
* @return string
*/
public static function product_add_to_cart_url($atts)
{
global $wpdb;
if (empty($atts)) {
return '';
}
if (isset($atts['id'])) {
$product_data = get_post($atts['id']);
} elseif (isset($atts['sku'])) {
$product_id = wc_get_product_id_by_sku($atts['sku']);
$product_data = get_post($product_id);
} else {
return '';
}
if ('product' !== $product_data->post_type) {
return '';
}
$_product = wc_get_product($product_data);
return esc_url($_product->add_to_cart_url());
}
示例6: product_add_to_cart_url
/**
* Get the add to cart URL for a product.
*
* @param array $atts
* @return string
*/
public static function product_add_to_cart_url($atts)
{
global $wpdb;
if (empty($atts)) {
return '';
}
if (isset($atts['id'])) {
$product_data = get_post($atts['id']);
} elseif (isset($atts['sku'])) {
$product_id = wc_get_product_id_by_sku($atts['sku']);
$product_data = get_post($product_id);
} else {
return '';
}
$product = is_object($product_data) && in_array($product_data->post_type, array('product', 'product_variation')) ? wc_setup_product_data($product_data) : false;
if (!$product) {
return '';
}
$_product = wc_get_product($product_data);
return esc_url($_product->add_to_cart_url());
}
示例7: set_line_item
/**
* Create or update a line item
*
* @since 2.2
* @param \WC_Order $order
* @param array $item line item data
* @param string $action 'create' to add line item or 'update' to update it
* @throws WC_API_Exception invalid data, server error
*/
protected function set_line_item($order, $item, $action)
{
$creating = 'create' === $action;
// product is always required
if (!isset($item['product_id']) && !isset($item['sku'])) {
throw new WC_API_Exception('woocommerce_api_invalid_product_id', __('Product ID or SKU is required', 'woocommerce'), 400);
}
// when updating, ensure product ID provided matches
if ('update' === $action) {
$item_product_id = wc_get_order_item_meta($item['id'], '_product_id');
$item_variation_id = wc_get_order_item_meta($item['id'], '_variation_id');
if ($item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id) {
throw new WC_API_Exception('woocommerce_api_invalid_product_id', __('Product ID provided does not match this line item', 'woocommerce'), 400);
}
}
if (isset($item['product_id'])) {
$product_id = $item['product_id'];
} elseif (isset($item['sku'])) {
$product_id = wc_get_product_id_by_sku($item['sku']);
}
// variations must each have a key & value
$variation_id = 0;
if (isset($item['variations']) && is_array($item['variations'])) {
foreach ($item['variations'] as $key => $value) {
if (!$key || !$value) {
throw new WC_API_Exception('woocommerce_api_invalid_product_variation', __('The product variation is invalid', 'woocommerce'), 400);
}
}
$variation_id = $this->get_variation_id(wc_get_product($product_id), $item['variations']);
}
$product = wc_get_product($variation_id ? $variation_id : $product_id);
// must be a valid WC_Product
if (!is_object($product)) {
throw new WC_API_Exception('woocommerce_api_invalid_product', __('Product is invalid', 'woocommerce'), 400);
}
// quantity must be positive float
if (isset($item['quantity']) && floatval($item['quantity']) <= 0) {
throw new WC_API_Exception('woocommerce_api_invalid_product_quantity', __('Product quantity must be a positive float', 'woocommerce'), 400);
}
// quantity is required when creating
if ($creating && !isset($item['quantity'])) {
throw new WC_API_Exception('woocommerce_api_invalid_product_quantity', __('Product quantity is required', 'woocommerce'), 400);
}
if ($creating) {
$item = new WC_Order_Item_Product();
} else {
$item = new WC_Order_Item_Product($item['id']);
}
$item->set_product($product);
$item->set_order_id($order->id);
if (isset($item['quantity'])) {
$item->set_quantity($item['quantity']);
}
if (isset($item['total'])) {
$item->set_total(floatval($item['total']));
}
if (isset($item['total_tax'])) {
$item->set_total_tax(floatval($item['total_tax']));
}
if (isset($item['subtotal'])) {
$item->set_subtotal(floatval($item['subtotal']));
}
if (isset($item['subtotal_tax'])) {
$item->set_subtotal_tax(floatval($item['subtotal_tax']));
}
if ($variation_id) {
$item->set_variation_id($variation_id);
$item->set_variation($item['variations']);
}
$item_id = $item->save();
if (!$item_id) {
throw new WC_API_Exception('woocommerce_cannot_create_line_item', __('Cannot create line item, try again', 'woocommerce'), 500);
}
}
示例8: wc_autoship_import_muenster_file
function wc_autoship_import_muenster_file()
{
set_time_limit(300);
header('Content-Type: application/json');
// Check if file is valid
if (empty($_FILES['uploadedFiles']['error']) || $_FILES['uploadedFiles']['error'][0] !== UPLOAD_ERR_OK) {
echo json_encode(array('error' => 'Upload error code: ' . $_FILES['uploadedFiles']['error'][0]));
die;
}
// Open file
$file = fopen($_FILES['uploadedFiles']['tmp_name'][0], 'r');
if (!$file) {
echo json_encode(array('error' => 'Could not open file'));
die;
}
// Read column headers
$column_headers = fgetcsv($file);
if (empty($column_headers)) {
echo json_encode(array('error' => 'Invalid file format'));
die;
}
$columns = array();
foreach ($column_headers as $index => $name) {
$columns[$name] = $index;
}
$column_names = array('Customer Email', 'Next Shipping Date', 'Shipping Method', 'Frequency', 'Quantity', 'Product SKU');
foreach ($column_names as $name) {
if (!isset($columns[$name])) {
echo json_encode(array('error' => 'Missing column: ' . $name));
die;
}
}
// Include dependencies
require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-customer.php';
require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule.php';
require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule-item.php';
// Process file
$result = array();
while ($row = fgetcsv($file)) {
// Get email
$email = $row[$columns['Customer Email']];
if (empty($email)) {
// Email is empty, skip this record
continue;
}
$result_item = array('data' => array_combine($column_headers, $row));
// Find existing user
$user_id = 0;
$user = get_user_by('email', $email);
if (!$user) {
$result_item['error'] = "User not found for email {$email}";
$result[] = $result_item;
continue;
} else {
$user_id = $user->ID;
}
$result_item['data']['user_id'] = $user_id;
if (empty($user_id) || is_object($user_id)) {
$result_item['error'] = 'Invalid user_id';
$result[] = $result_item;
continue;
}
// Create autoship customer
$customer = new WC_Autoship_Customer($user_id);
$customer->set('shipping_method', $row[$columns['Shipping Method']]);
if (false === $customer->save()) {
// Error creating autoship customer
$result_item['error'] = 'Error creating autoship customer';
$result[] = $result_item;
continue;
}
// Create autoship schedule item
$product_id = wc_get_product_id_by_sku($row[$columns['Product SKU']]);
if (empty($product_id)) {
// Product does not exist
$result_item['error'] = 'Product SKU not found';
$result[] = $result_item;
continue;
}
$result_item['data']['product_id'] = $product_id;
$product = wc_get_product($product_id);
$item = new WC_Autoship_Schedule_Item();
if ($product->is_type('simple')) {
$item->set('product_id', $product_id);
} elseif ($product->is_type('variation')) {
$item->set('variation_id', $product_id);
$item->set('product_id', $product->get_parent());
}
$item->set('qty', $row[$columns['Quantity']]);
// Create autoship schedule
$frequency_desc = $row[$columns['Frequency']];
$frequency_matches = null;
if (!preg_match('/(\\d+) Month/', $frequency_desc, $frequency_matches)) {
// Error saving autoship schedule
$result_item['error'] = "Invalid Frequency: {$frequency_desc}";
$result[] = $result_item;
continue;
}
$frequency = 30 * (int) $frequency_matches[1];
$schedule = WC_Autoship_Schedule::get_schedule($user_id, $frequency);
//.........这里部分代码省略.........
开发者ID:jonrules,项目名称:woocommerce-autoship-import-muenster,代码行数:101,代码来源:woocommerce-autoship-import-muenster.php
示例9: get_product_id
/**
* Gets the product ID from the SKU or posted ID.
* @param array $posted Request data
* @return int
*/
protected function get_product_id($posted)
{
if (!empty($posted['sku'])) {
$product_id = (int) wc_get_product_id_by_sku($posted['sku']);
} elseif (!empty($posted['product_id']) && empty($posted['variation_id'])) {
$product_id = (int) $posted['product_id'];
} elseif (!empty($posted['variation_id'])) {
$product_id = (int) $posted['variation_id'];
} else {
throw new WC_REST_Exception('woocommerce_rest_required_product_reference', __('Product ID or SKU is required.', 'woocommerce'), 400);
}
return $product_id;
}
示例10: get_product_by_sku
/**
* Get product by SKU
*
* @since 2.3.0
* @param int $sku the product SKU
* @param string $fields
* @return array
*/
public function get_product_by_sku($sku, $fields = null)
{
try {
$id = wc_get_product_id_by_sku($sku);
if (empty($id)) {
throw new WC_API_Exception('woocommerce_api_invalid_product_sku', __('Invalid product SKU', 'woocommerce'), 404);
}
return $this->get_product($id, $fields);
} catch (WC_API_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}
示例11: wc_autoship_import_paymentxp_file
function wc_autoship_import_paymentxp_file()
{
set_time_limit(300);
header('Content-Type: application/json');
// Check if file is valid
if (empty($_FILES['uploadedFiles']['error']) || $_FILES['uploadedFiles']['error'][0] !== UPLOAD_ERR_OK) {
echo json_encode(array('error' => 'Upload error code: ' . $_FILES['uploadedFiles']['error'][0]));
die;
}
// Open file
$file = fopen($_FILES['uploadedFiles']['tmp_name'][0], 'r');
if (!$file) {
echo json_encode(array('error' => 'Could not open file'));
die;
}
// Read column headers
$column_headers = fgetcsv($file);
if (empty($column_headers)) {
echo json_encode(array('error' => 'Invalid file format'));
die;
}
$columns = array();
foreach ($column_headers as $index => $name) {
$columns[$name] = $index;
}
$column_names = array('email', 'autoship frequency', 'next order date', 'product sku', 'item quantity', 'username', 'password', 'pxp_customer_id', 'pmt_desc', 'display_name', 'user_nicename', 'first_name', 'last_name', 'phone', 'billing_phone', 'billing_address', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_zip', 'billing_postcode', 'billing_country', 'billing_last_name', 'billing_first_name', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_postcode', 'shipping_country', 'shipping_last_name', 'shipping_first_name', 'shipping_method', 'role', 'affwp_lc_email', 'affwp_lc_affiliate_id');
$usermeta_column_names = array('phone', 'billing_phone', 'billing_address', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_zip', 'billing_postcode', 'billing_country', 'billing_last_name', 'billing_first_name', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_postcode', 'shipping_country', 'shipping_last_name', 'shipping_first_name', 'affwp_lc_email', 'affwp_lc_affiliate_id');
foreach ($column_names as $name) {
if (!isset($columns[$name])) {
echo json_encode(array('error' => 'Missing column: ' . $name));
die;
}
}
// Include dependencies
require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-customer.php';
require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule.php';
require_once WP_PLUGIN_DIR . '/woocommerce-autoship/classes/wc-autoship-schedule-item.php';
// Process file
$result = array();
while ($row = fgetcsv($file)) {
// Get email
$email = $row[$columns['email']];
if (empty($email)) {
// Email is empty, skip this record
continue;
}
$result_item = array('data' => array_combine($column_headers, $row));
// Find existing user
$user_id = 0;
$user = get_user_by('email', $email);
if (!$user) {
// Create new user
$user_id = @wp_create_user($row[$columns['username']], $row[$columns['password']], $email);
if (!$user_id) {
$result_item['error'] = 'Error creating user';
$result[] = $result_item;
continue;
}
@wp_update_user(array('ID' => $user_id, 'user_nicename' => $row[$columns['user_nicename']], 'display_name' => $row[$columns['display_name']], 'first_name' => $row[$columns['first_name']], 'last_name' => $row[$columns['last_name']], 'role' => $row[$columns['role']]));
// Add user meta
foreach ($usermeta_column_names as $name) {
add_user_meta($user_id, $name, $row[$columns[$name]], true);
}
} else {
$user_id = $user->ID;
}
$result_item['data']['user_id'] = $user_id;
if (empty($user_id) || is_object($user_id)) {
$result_item['error'] = 'Invalid user_id';
$result[] = $result_item;
continue;
}
// Create autoship customer
$customer = new WC_Autoship_Customer($user_id);
$customer->store_payment_method('wc_autoship_paymentxp', $row[$columns['pxp_customer_id']], array('CustomerID' => $row[$columns['pxp_customer_id']], 'CardNumber' => $row[$columns['pmt_desc']]));
$customer->set('shipping_method', $row[$columns['shipping_method']]);
if (false === $customer->save()) {
// Error creating autoship customer
$result_item['error'] = 'Error creating autoship customer';
$result[] = $result_item;
continue;
}
// Create autoship schedule item
$product_id = wc_get_product_id_by_sku($row[$columns['product sku']]);
if (empty($product_id)) {
// Product does not exist
$result_item['error'] = 'Product SKU not found';
$result[] = $result_item;
continue;
}
$result_item['data']['product_id'] = $product_id;
$product = wc_get_product($product_id);
$item = new WC_Autoship_Schedule_Item();
if ($product->is_type('simple')) {
$item->set('product_id', $product_id);
} elseif ($product->is_type('variation')) {
$item->set('variation_id', $product_id);
$item->set('product_id', $product->get_parent());
}
$item->set('qty', $row[$columns['item quantity']]);
//.........这里部分代码省略.........
开发者ID:jonrules,项目名称:woocommerce-autoship-import-paymentxp,代码行数:101,代码来源:woocommerce-autoship-import-paymentxp.php
示例12: sync
//.........这里部分代码省略.........
exit;
}
} else {
if ($type == 'sync') {
if ($_SERVER['HTTP_X_ACTION'] === 'TEMPLATE') {
if (!$this->check_hash()) {
exit;
}
$ebayDesignDir = WP_CONTENT_DIR . '/ebay/';
$tmpPath = wp_tempnam();
@file_put_contents($tmpPath, file_get_contents('php://input'));
$db = new PDO('sqlite:' . $tmpPath);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA synchronous=0');
$db->exec('PRAGMA temp_store=2');
$db->exec('PRAGMA page_size=65536');
$db->exec('PRAGMA encoding=\'UTF-8\'');
$db->exec('PRAGMA cache_size=15000');
$db->exec('PRAGMA soft_heap_limit=67108864');
$db->exec('PRAGMA journal_mode=MEMORY');
$files = $db->prepare('SELECT Name, Content FROM File');
$files->execute();
$files->bindColumn(1, $name);
$files->bindColumn(2, $content);
while ($files->fetch()) {
$fileName = $ebayDesignDir . $name;
if (strpos($name, '..') === false) {
if (!file_exists($fileName)) {
$dir = dirname($fileName);
if (!is_dir($dir)) {
mkdir($dir . '/', 0755, true);
}
@file_put_contents($fileName, $content);
}
}
}
$db = null;
unlink($tmpPath);
$this->sendHttpHeaders('200 OK', array('Content-Type' => 'application/json', 'Cache-Control' => 'no-cache, no-store', 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT', 'Pragma' => 'no-cache'));
echo $this->json_encode(array('ack' => 'ok'));
exit;
}
} else {
if ($type == 'index/calc') {
$product_ids = array();
$quantities = array();
for ($i = 0;; $i++) {
if (!isset($_POST['PRODUCTCODE(' . $i . ')'])) {
break;
}
$productid = (int) $_POST['PRODUCTID(' . $i . ')'];
if (!$productid) {
$productcode = $_POST['PRODUCTCODE(' . $i . ')'];
$productid = wc_get_product_id_by_sku($productcode);
}
$productqty = $_POST['PRODUCTQUANTITY(' . $i . ')'];
if (!$productqty && $productqty != 0) {
$productqty = 1;
}
WC()->cart->add_to_cart($productid, $productqty);
}
WC()->customer->set_location($_POST['COUNTRYCODE'], $_POST['DIVISION'], $_POST['POSTALCODE'], $_POST['PLACE']);
WC()->customer->set_shipping_location($_POST['COUNTRYCODE'], $_POST['DIVISION'], $_POST['POSTALCODE'], $_POST['PLACE']);
WC()->cart->calculate_totals();
WC()->cart->calculate_shipping();
$response = '';
$idx = 0;
$methods = WC()->shipping()->get_shipping_methods();
foreach ($methods as $method) {
if (file_exists(plugin_dir_path(__FILE__) . 'shipping/' . $method->id)) {
include plugin_dir_path(__FILE__) . 'shipping/' . $method->id;
} else {
foreach ($method->rates as $method => $rate) {
$method_name = $rate->get_label();
if (!$method_name) {
$method_name = 'Shipping';
}
$method_cost = $rate->cost;
if (is_numeric($method_cost)) {
if (isset($rate->taxes) && is_array($rate->taxes)) {
foreach ($rate->taxes as $tax) {
if (is_numeric($tax)) {
$method_cost += $tax;
}
}
}
$response .= ($idx > 0 ? '&' : '') . 'FREIGHTNAME(' . $idx . ')=' . rawurlencode($method_name) . '&FREIGHTCHARGEINCTAX(' . $idx . ')=' . number_format((double) $method_cost, 2, '.', '');
$idx++;
}
}
}
}
$this->sendHttpHeaders('200 OK', array('Content-Type' => 'application/json', 'Cache-Control' => 'no-cache, no-store', 'Expires' => 'Thu, 01 Jan 1970 00:00:00 GMT', 'Pragma' => 'no-cache'));
echo $response;
exit;
}
}
}
}
}
示例13: get_product_by_id
public function get_product_by_id($sku = null)
{
global $wpdb;
$product_id = null;
/* ! @since 3.0.6 use new function wc_get_product_id_by_sku to get SKU */
if (function_exists('wc_get_product_id_by_sku')) {
$product_id = wc_get_product_id_by_sku($sku);
} else {
$product_id = $wpdb->get_var($wpdb->prepare("SELECT max(post_id) FROM {$wpdb->postmeta} a, {$wpdb->posts} b\n\t\t\t\tWHERE a.post_id= b.id and meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku));
}
if ($product_id) {
$product['ID'] = $product_id;
}
return $product_id = apply_filters('woocsv_get_product_id', $product_id, $sku);
}
示例14: set_line_item
/**
* Create or update a line item
*
* @since 2.5.0
* @param WC_Order $order
* @param array $item line item data
* @param string $action 'create' to add line item or 'update' to update it
* @throws WC_CLI_Exception invalid data, server error
*/
protected function set_line_item($order, $item, $action)
{
$creating = 'create' === $action;
// product is always required
if (!isset($item['product_id']) && !isset($item['sku'])) {
throw new WC_CLI_Exception('woocommerce_cli_invalid_product_id', __('Product ID or SKU is required', 'woocommerce'));
}
// when updating, ensure product ID provided matches
if ('update' === $action) {
$item_product_id = wc_get_order_item_meta($item['id'], '_product_id');
$item_variation_id = wc_get_order_item_meta($item['id'], '_variation_id');
if ($item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id) {
throw new WC_CLI_Exception('woocommerce_cli_invalid_product_id', __('Product ID provided does not match this line item', 'woocommerce'));
}
}
if (isset($item['product_id'])) {
$product_id = $item['product_id'];
} elseif (isset($item['sku'])) {
$product_id = wc_get_product_id_by_sku($item['sku']);
}
// variations must each have a key & value
$variation_id = 0;
if (isset($item['variations']) && is_array($item['variations'])) {
foreach ($item['variations'] as $key => $value) {
if (!$key || !$value) {
throw new WC_CLI_Exception('woocommerce_cli_invalid_product_variation', __('The product variation is invalid', 'woocommerce'));
}
}
$item_args['variation'] = $item['variations'];
$variation_id = $this->get_variation_id(wc_get_product($product_id), $item_args['variation']);
}
$product = wc_get_product($variation_id ? $variation_id : $product_id);
// must be a valid WC_Product
if (!is_object($product)) {
throw new WC_CLI_Exception('woocommerce_cli_invalid_product', __('Product is invalid.', 'woocommerce'));
}
// quantity must be positive float
if (isset($item['quantity']) && floatval($item['quantity']) <= 0) {
throw new WC_CLI_Exception('woocommerce_cli_invalid_product_quantity', __('Product quantity must be a positive float.', 'woocommerce'));
}
// quantity is required when creating
if ($creating && !isset($item['quantity'])) {
throw new WC_CLI_Exception('woocommerce_cli_invalid_product_quantity', __('Product quantity is required.', 'woocommerce'));
}
$item_args = array();
// quantity
if (isset($item['quantity'])) {
$item_args['qty'] = $item['quantity'];
}
// total
if (isset($item['total'])) {
$item_args['totals']['total'] = floatval($item['total']);
}
// total tax
if (isset($item['total_tax'])) {
$item_args['totals']['tax'] = floatval($item['total_tax']);
}
// subtotal
if (isset($item['subtotal'])) {
$item_args['totals']['subtotal'] = floatval($item['subtotal']);
}
// subtotal tax
if (isset($item['subtotal_tax'])) {
$item_args['totals']['subtotal_tax'] = floatval($item['subtotal_tax']);
}
$item_args = apply_filters('woocommerce_cli_order_line_item_args', $item_args, $item, $order, $action);
if ($creating) {
$item_id = $order->add_product($product, $item_args['qty'], $item_args);
if (!$item_id) {
throw new WC_CLI_Exception('woocommerce_cannot_create_line_item', __('Cannot create line item, try again.', 'woocommerce'));
}
} else {
$item_id = $order->update_product($item['id'], $product, $item_args);
if (!$item_id) {
throw new WC_CLI_Exception('woocommerce_cannot_update_line_item', __('Cannot update line item, try again.', 'woocommerce'));
}
}
}