本文整理汇总了PHP中WC_Product::exists方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product::exists方法的具体用法?PHP WC_Product::exists怎么用?PHP WC_Product::exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product
的用法示例。
在下文中一共展示了WC_Product::exists方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: array
/**
* Gets a user's downloadable products if they are logged in.
*
* @access public
* @return array Array of downloadable products
*/
function get_downloadable_products()
{
global $wpdb;
$downloads = array();
if (is_user_logged_in()) {
$user_info = get_userdata(get_current_user_id());
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "woocommerce_downloadable_product_permissions WHERE user_id = '%s';", get_current_user_id()));
if ($results) {
foreach ($results as $result) {
if ($result->order_id > 0) {
$order = new WC_Order($result->order_id);
if ($order->status != 'completed' && $order->status != 'processing') {
continue;
}
$product_post = get_post($result->product_id);
if (!$product_post) {
continue;
}
if ($product_post->post_type == 'product_variation') {
$_product = new WC_Product_Variation($result->product_id);
} else {
$_product = new WC_Product($result->product_id);
}
if ($_product->exists()) {
$download_name = $_product->get_title();
} else {
$download_name = '#' . $result->product_id;
}
$downloads[] = array('download_url' => add_query_arg('download_file', $result->product_id, add_query_arg('order', $result->order_key, add_query_arg('email', $result->user_email, home_url()))), 'product_id' => $result->product_id, 'download_name' => $download_name, 'order_id' => $order->id, 'order_key' => $order->order_key, 'downloads_remaining' => $result->downloads_remaining);
}
}
}
}
return apply_filters('woocommerce_customer_get_downloadable_products', $downloads);
}
示例2: foreach
<th class="product-name"> </th>
<th class="product-price"> </th>
<th class="product-add-to-cart"> </th>
<th class="product-remove"> </th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($aUsersWishlist as $iProductId => $aWshlistItemData) {
if ($aWshlistItemData['type'] == 'variable' && !empty($aWshlistItemData['vid'])) {
foreach ($aWshlistItemData['vid'] as $iVariableProductId) {
$oProduct = new WC_Product_Variation($iVariableProductId, $iProductId);
if ($oProduct->exists()) {
include UniWishlist()->plugin_path() . '/includes/views/item-variation-table-row.php';
}
}
} else {
$oProduct = new WC_Product($iProductId);
if ($oProduct->exists()) {
include UniWishlist()->plugin_path() . '/includes/views/item-table-row.php';
}
}
$i++;
}
?>
</tbody>
</table>
<?php
do_action('uni_wishlist_after_table_action');
示例3: array
/**
* Add a product to the cart
*
* @param string product_id contains the id of the product to add to the cart
* @param string quantity contains the quantity of the item to add
* @param int variation_id
* @param array variation attribute values
*/
function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = '')
{
global $woocommerce;
if ($quantity < 1) {
return false;
}
// Load cart item data - may be added by other plugins
$cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', array(), $product_id);
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart($cart_id);
if ($variation_id > 0) {
$product_data = new WC_Product_Variation($variation_id);
} else {
$product_data = new WC_Product($product_id);
}
// Type/Exists check
if ($product_data->is_type('external') || !$product_data->exists()) {
$woocommerce->add_error(__('This product cannot be purchased.', 'woocommerce'));
return false;
}
// Price set check
if ($product_data->get_price() === '') {
$woocommerce->add_error(__('This product cannot be purchased - the price is not yet set.', 'woocommerce'));
return false;
}
// Stock check - only check if we're managing stock and backorders are not allowed
if (!$product_data->has_enough_stock($quantity)) {
$woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woocommerce'), $product_data->get_stock_quantity()));
return false;
} elseif (!$product_data->is_in_stock()) {
$woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce'));
return false;
}
if ($cart_item_key) {
$quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
// Stock check - this time accounting for whats already in-cart
if (!$product_data->has_enough_stock($quantity)) {
$woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock and you already have %s in your cart.', 'woocommerce'), $product_data->get_stock_quantity(), $this->cart_contents[$cart_item_key]['quantity']));
return false;
} elseif (!$product_data->is_in_stock()) {
$woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce'));
return false;
}
$this->set_quantity($cart_item_key, $quantity);
} else {
// Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
$this->cart_contents[$cart_id] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)));
}
$this->set_session();
return true;
}
示例4:
if (isset($item['variation_id']) && $item['variation_id'] > 0) :
$_product = new WC_Product_Variation( $item['variation_id'] );
else :
$_product = new WC_Product( $item['id'] );
endif;
echo '
<tr class = "' . esc_attr( apply_filters('woocommerce_order_table_item_class', 'order_table_item', $item, $order ) ) . '">
<td class="product-name">';
echo '<a href="'.get_permalink( $item['id'] ).'">' . $item['name'] . '</a>';
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
$item_meta->display();
if ( $_product->exists() && $_product->is_downloadable() && $_product->has_file() && ( $order->status=='completed' || ( get_option( 'woocommerce_downloads_grant_access_after_payment' ) == 'yes' && $order->status == 'processing' ) ) ) :
echo '<br/><small><a href="' . $order->get_downloadable_file_url( $item['id'], $item['variation_id'] ) . '">' . __('Download file →', 'woocommerce') . '</a></small>';
endif;
echo '</td><td class="product-quantity">'.$item['qty'].'</td><td class="product-total">' . $order->get_formatted_line_subtotal($item) . '</td></tr>';
// Show any purchase notes
if ($order->status=='completed' || $order->status=='processing') :
if ($purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) :
echo '<tr class="product-purchase-note"><td colspan="3">' . apply_filters('the_content', $purchase_note) . '</td></tr>';
endif;
endif;
endforeach;
示例5: array
/**
* Add a product to the cart
*
* @param string $product_id contains the id of the product to add to the cart
* @param string $quantity contains the quantity of the item to add
* @param int $variation_id
* @param array $variation attribute values
* @param array $cart_item_data extra cart item data we want to pass into the item
* @return bool
*/
function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array())
{
global $woocommerce;
if ($quantity < 1) {
return false;
}
// Load cart item data - may be added by other plugins
$cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', $cart_item_data, $product_id);
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart($cart_id);
if ($variation_id > 0) {
$product_data = new WC_Product_Variation($variation_id);
} else {
$product_data = new WC_Product($product_id);
}
// Force quantity to 1 if sold individually
if ($product_data->is_sold_individually()) {
$quantity = 1;
}
// Type/Exists check
if ($product_data->is_type('external') || !$product_data->exists()) {
$woocommerce->add_error(__('This product cannot be purchased.', 'woocommerce'));
return false;
}
// Price set check
if ($product_data->get_price() === '') {
$woocommerce->add_error(__('This product cannot be purchased - the price is not yet set.', 'woocommerce'));
return false;
}
// Stock check - only check if we're managing stock and backorders are not allowed
if (!$product_data->has_enough_stock($quantity)) {
$woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woocommerce'), $product_data->get_stock_quantity()));
return false;
} elseif (!$product_data->is_in_stock()) {
$woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce'));
return false;
}
// Downloadable/virtual qty check
if ($product_data->is_sold_individually()) {
$in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] + $quantity : $quantity;
// If its greater than 1, its already in the cart
if ($in_cart_quantity > 1) {
$woocommerce->add_error(sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('You already have this item in your cart.', 'woocommerce')));
return false;
}
}
// Stock check - this time accounting for whats already in-cart
$product_qty_in_cart = $this->get_cart_item_quantities();
if ($product_data->managing_stock()) {
// Variations
if ($variation_id && $product_data->variation_has_stock) {
if (isset($product_qty_in_cart[$variation_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$variation_id] + $quantity)) {
$woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a> You cannot add that amount to the cart — we have %s in stock and you already have %s in your cart.', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), $product_data->get_stock_quantity(), $product_qty_in_cart[$variation_id]));
return false;
}
// Products
} else {
if (isset($product_qty_in_cart[$product_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$product_id] + $quantity)) {
$woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a> You cannot add that amount to the cart — we have %s in stock and you already have %s in your cart.', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), $product_data->get_stock_quantity(), $product_qty_in_cart[$product_id]));
return false;
}
}
}
// If cart_item_key is set, the item is already in the cart
if ($cart_item_key) {
$new_quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
$this->set_quantity($cart_item_key, $new_quantity);
} else {
$cart_item_key = $cart_id;
// Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
$this->cart_contents[$cart_item_key] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)), $cart_item_key);
}
do_action('woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data);
$woocommerce->cart_has_contents_cookie(true);
$this->set_session();
return true;
}
示例6: empty
/**
* Get the cart data from the PHP session and store it in class variables.
*
* @access public
* @return void
*/
function get_cart_from_session()
{
global $woocommerce;
// Load the coupons
if (get_option('woocommerce_enable_coupons') == 'yes') {
$this->applied_coupons = empty($_SESSION['coupons']) ? array() : array_unique(array_filter((array) $_SESSION['coupons']));
}
// Load the cart
if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
$cart = $_SESSION['cart'];
foreach ($cart as $key => $values) {
if ($values['variation_id'] > 0) {
$_product = new WC_Product_Variation($values['variation_id']);
} else {
$_product = new WC_Product($values['product_id']);
}
if ($_product->exists() && $values['quantity'] > 0) {
// Put session data into array. Run through filter so other plugins can load their own session data
$this->cart_contents[$key] = apply_filters('woocommerce_get_cart_item_from_session', array('product_id' => $values['product_id'], 'variation_id' => $values['variation_id'], 'variation' => $values['variation'], 'quantity' => $values['quantity'], 'data' => $_product), $values, $key);
}
}
if (!is_array($this->cart_contents)) {
$this->cart_contents = array();
}
} else {
$this->cart_contents = array();
}
// Cookie
if (sizeof($this->cart_contents) > 0) {
$woocommerce->cart_has_contents_cookie(true);
} else {
$woocommerce->cart_has_contents_cookie(false);
}
// Load totals
$this->cart_contents_total = isset($_SESSION['cart_contents_total']) ? $_SESSION['cart_contents_total'] : 0;
$this->cart_contents_weight = isset($_SESSION['cart_contents_weight']) ? $_SESSION['cart_contents_weight'] : 0;
$this->cart_contents_count = isset($_SESSION['cart_contents_count']) ? $_SESSION['cart_contents_count'] : 0;
$this->cart_contents_tax = isset($_SESSION['cart_contents_tax']) ? $_SESSION['cart_contents_tax'] : 0;
$this->total = isset($_SESSION['total']) ? $_SESSION['total'] : 0;
$this->subtotal = isset($_SESSION['subtotal']) ? $_SESSION['subtotal'] : 0;
$this->subtotal_ex_tax = isset($_SESSION['subtotal_ex_tax']) ? $_SESSION['subtotal_ex_tax'] : 0;
$this->tax_total = isset($_SESSION['tax_total']) ? $_SESSION['tax_total'] : 0;
$this->taxes = isset($_SESSION['taxes']) ? $_SESSION['taxes'] : array();
$this->shipping_taxes = isset($_SESSION['shipping_taxes']) ? $_SESSION['shipping_taxes'] : array();
$this->discount_cart = isset($_SESSION['discount_cart']) ? $_SESSION['discount_cart'] : 0;
$this->discount_total = isset($_SESSION['discount_total']) ? $_SESSION['discount_total'] : 0;
$this->shipping_total = isset($_SESSION['shipping_total']) ? $_SESSION['shipping_total'] : 0;
$this->shipping_tax_total = isset($_SESSION['shipping_tax_total']) ? $_SESSION['shipping_tax_total'] : 0;
$this->shipping_label = isset($_SESSION['shipping_label']) ? $_SESSION['shipping_label'] : '';
// Queue re-calc if subtotal is not set
if (!$this->subtotal && sizeof($this->cart_contents) > 0) {
$this->set_session();
}
}
示例7: activated_subscription
/**
* Handle group assignment : assign the user to the groups related to the subscription's product.
* @param int $user_id
* @param string $subscription_key
*/
public static function activated_subscription($user_id, $subscription_key)
{
$subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key);
if (isset($subscription['product_id']) && isset($subscription['order_id'])) {
$product_id = $subscription['product_id'];
$order_id = $subscription['order_id'];
// Leasving this here for reference, it can be assumed that normally,
// if the product's groups are modified, a reactivation should take its
// data from the current product, not from its previous state.
// See if the subscription was activated before and try to get subscription's groups.
// If there are any, use these instead of those from the product.
// This is necessary when a subscription has been cancelled and re-activated and the
// original product groups were modified since and we do NOT want to make group
// assignments based on the current state of the product.
$done = false;
//$groups_product_groups = get_user_meta( $user_id, '_groups_product_groups', true );
//if ( isset( $groups_product_groups[$order_id] ) && isset( $groups_product_groups[$order_id][$product_id] ) &&
// isset( $groups_product_groups[$order_id][$product_id]['groups'] ) &&
// isset( $groups_product_groups[$order_id][$product_id]['subscription_key'] ) &&
// ( $groups_product_groups[$order_id][$product_id]['subscription_key'] === $subscription_key )
//) {
// foreach( $groups_product_groups[$order_id][$product_id]['groups'] as $group_id ) {
// Groups_User_Group::create( $user_id, $group_id );
// }
// $done = true;
//}
// maybe unschedule pending expiration
wp_clear_scheduled_hook('groups_ws_subscription_expired', array('user_id' => $user_id, 'subscription_key' => $subscription_key));
if (!$done) {
// get the product from the subscription
$product = new WC_Product($product_id);
if ($product->exists()) {
// get the groups related to the product
if ($product_groups = get_post_meta($product_id, '_groups_groups', false)) {
if (count($product_groups) > 0) {
// add the groups to the subscription (in case the product is changed later on, the subscription is still valid)
$groups_product_groups = get_user_meta($user_id, '_groups_product_groups', true);
if (empty($groups_product_groups)) {
$groups_product_groups = array();
}
$groups_product_groups[$order_id][$product_id]['groups'] = $product_groups;
$groups_product_groups[$order_id][$product_id]['subscription_key'] = $subscription_key;
update_user_meta($user_id, '_groups_product_groups', $groups_product_groups);
// add the user to the groups
foreach ($product_groups as $group_id) {
$result = Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
}
Groups_WS_Terminator::mark_as_eternal($user_id, $group_id);
}
}
// remove from groups
if ($product_groups_remove = get_post_meta($product_id, '_groups_groups_remove', false)) {
if (count($product_groups_remove) > 0) {
$groups_product_groups_remove = get_user_meta($user_id, '_groups_product_groups_remove', true);
if (empty($groups_product_groups_remove)) {
$groups_product_groups_remove = array();
}
$groups_product_groups_remove[$order_id][$product_id]['groups'] = $product_groups_remove;
update_user_meta($user_id, '_groups_product_groups_remove', $groups_product_groups_remove);
// remove the user from the groups
foreach ($product_groups_remove as $group_id) {
$result = Groups_User_Group::delete($user_id, $group_id);
}
}
}
}
}
}
}
示例8: generate_googlecheckout_form
/**
* Generate the Google Checkout button link
**/
public function generate_googlecheckout_form($order_id)
{
global $woocommerce;
require_once GOOGLE_CHECKOUT_LIB . 'googlecart.php';
require_once GOOGLE_CHECKOUT_LIB . 'googleitem.php';
require_once GOOGLE_CHECKOUT_LIB . 'googleshipping.php';
require_once GOOGLE_CHECKOUT_LIB . 'googletax.php';
$order = new WC_Order($order_id);
$shipping_name = explode(' ', $order->shipping_method);
// Check if this is a test purchase
if ($this->testmode == 'yes') {
$server_type = "sandbox";
} else {
$server_type = "checkout";
}
$merchant_id = $this->merchant_id;
// Your Merchant ID
$merchant_key = $this->merchant_key;
// Your Merchant Key
$currency = get_option('woocommerce_currency');
$cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $currency);
// Specify <edit-cart-url>
$cart->SetEditCartUrl(get_permalink(get_option('woocommerce_cart_page_id')));
// Specify "Return to xyz" link
$cart->SetContinueShoppingUrl(add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('woocommerce_thanks_page_id')))));
// Order key
$cart->SetMerchantPrivateData(new MerchantPrivateData(array("cart-id" => $order->id)));
// Request buyer's phone number
$cart->SetRequestBuyerPhone(true);
// Default tax - for shipping, if used
if ($order->order_shipping_tax > 0) {
// We manually calculate the shipping tax percentage here
$calculated_shipping_tax_percentage = $order->order_shipping_tax / $order->order_shipping;
$tax_rule_for_shipping = new GoogleDefaultTaxRule($calculated_shipping_tax_percentage, 'true');
$tax_rule_for_shipping->SetWorldArea(true);
$cart->AddDefaultTaxRules($tax_rule_for_shipping);
}
// Shipping Cost
if ($order->order_shipping > 0) {
$ship_1 = new GoogleFlatRateShipping($order->shipping_method, number_format($order->order_shipping, 2));
$restriction_1 = new GoogleShippingFilters();
$restriction_1->SetAllowedWorldArea(true);
$ship_1->AddShippingRestrictions($restriction_1);
$cart->AddShipping($ship_1);
}
// Cart Contents
$item_loop = 0;
$myvat = array();
if (sizeof($order->get_items()) > 0) {
foreach ($order->get_items() as $item) {
$_product = new WC_Product($item['id']);
if ($_product->exists() && $item['qty']) {
$item_loop++;
// Change tax format from 25.00 to 0.25
$item_tax_percentage = number_format($order->get_item_tax($item, false) / $order->get_item_total($item, false, false) * 100, 2, '.', '');
$item_vat = $item_tax_percentage / 100;
$myvat[$item_loop] = $item_vat;
${"item_" . $item_loop} = new GoogleItem($item['name'], "", $item['qty'], $order->get_item_total($item, false, false));
// Name the alternate-tax-table
$vat_name = "vat" . $item_vat;
${"item_" . $item_loop}->SetMerchantItemId($item['id']);
${"item_" . $item_loop}->SetTaxTableSelector($vat_name);
$cart->AddItem(${"item_" . $item_loop});
}
}
}
// Discount
if ($order->order_discount > 0) {
$item_loop++;
${"item_" . $item_loop} = new GoogleItem(__('Discount', 'woothemes'), "", "1", -$order->order_discount);
${"item_" . $item_loop}->SetTaxTableSelector("no_tax");
$cart->AddItem(${"item_" . $item_loop});
}
// Tax
// Loops through all tax classes that has been added to the cart and add these as Alternate tax tables to google Checkout.
$taxrule_loop = 1;
$no_duplicate_vat = array_unique($myvat);
foreach ($no_duplicate_vat as $value) {
// Name the alternate-tax-table
$vat_name = "vat" . $value;
$tax_table = new GoogleAlternateTaxTable($vat_name);
${"tax_rule_" . $taxrule_loop} = new GoogleAlternateTaxRule($value);
${"tax_rule_" . $taxrule_loop}->SetWorldArea(true);
$tax_table->AddAlternateTaxRules(${"tax_rule_" . $taxrule_loop});
$cart->AddAlternateTaxTables($tax_table);
$taxrule_loop++;
}
// The form
return $cart->CheckoutButtonCode("SMALL") . '<script type="text/javascript">
jQuery(function(){
jQuery("body").block(
{
message: "<img src=\\"' . $woocommerce->plugin_url() . '/assets/images/ajax-loader.gif\\" alt=\\"Redirecting...\\" />' . __('Thank you for your order. We are now redirecting you to Google Checkout to make payment.', 'woothemes') . '",
overlayCSS:
{
background: "#fff",
opacity: 0.6
//.........这里部分代码省略.........
示例9: has_voucher
/**
* Returns true if the given product is downloadable with an attached
* voucher
*
* @since 1.2
* @param WC_Product $product the product to check for a voucher
* @return boolean true if $product has a voucher, false otherwise
*/
public static function has_voucher($product)
{
if (!$product->exists()) {
return false;
}
return null !== self::get_voucher($product);
}