本文整理汇总了PHP中WC函数的典型用法代码示例。如果您正苦于以下问题:PHP WC函数的具体用法?PHP WC怎么用?PHP WC使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WC函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_order
/**
* Create a order.
*
* @since 2.4
*
* @return WC_Order Order object.
*/
public static function create_order()
{
// Create product
$product = WC_Helper_Product::create_simple_product();
WC_Helper_Shipping::create_simple_flat_rate();
$order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => '');
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Required, else wc_create_order throws an exception
$order = wc_create_order($order_data);
// Add order products
$item_id = $order->add_product($product, 4);
// Set billing address
$billing_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => 'admin@example.org', 'phone' => '555-32123');
$order->set_address($billing_address, 'billing');
// Add shipping costs
$shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
$order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate'));
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['bacs']);
// Set totals
$order->set_total(10, 'shipping');
$order->set_total(0, 'cart_discount');
$order->set_total(0, 'cart_discount_tax');
$order->set_total(0, 'tax');
$order->set_total(0, 'shipping_tax');
$order->set_total(40, 'total');
// 4 x $10 simple helper product
return wc_get_order($order->id);
}
示例2: update_orders_value
public static function update_orders_value( $post_id, $post ) {
if ( is_int( wp_is_post_revision( $post_id ) ) ) return;
if ( is_int( wp_is_post_autosave( $post_id ) ) ) return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;
if ( $post->post_type != 'product' ) return $post_id;
if ( version_compare( WC()->version, '2.2.0', '<' ) ) {
$product = get_product( $post );
} else {
$product = wc_get_product( $post );
}
if ( $product ) {
if ( $product->is_on_sale() ) {
update_post_meta( $post_id, '_psad_onsale_order', 2 );
} else {
update_post_meta( $post_id, '_psad_onsale_order', 1 );
}
if ( $product->is_featured() ) {
update_post_meta( $post_id, '_psad_featured_order', 2 );
} else {
update_post_meta( $post_id, '_psad_featured_order', 1 );
}
}
}
示例3: record_user_event
public function record_user_event($event_type, $data = array())
{
if (!function_exists('jetpack_tracks_record_event')) {
$this->log('Error. jetpack_tracks_record_event is not defined.');
return;
}
$user = wp_get_current_user();
$site_url = get_option('siteurl');
// Check for WooCommerce
$wc_version = 'unavailable';
if (function_exists('WC')) {
$wc_version = WC()->version;
}
// Check for Jetpack
$jp_version = 'unavailable';
if (defined('JETPACK__VERSION')) {
$jp_version = JETPACK__VERSION;
}
$jetpack_blog_id = -1;
if (class_exists('Jetpack_Options') && method_exists('Jetpack_Options', 'get_option')) {
$jetpack_blog_id = Jetpack_Options::get_option('id');
}
$data['_via_ua'] = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$data['_via_ip'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$data['_lg'] = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
$data['blog_url'] = $site_url;
$data['blog_id'] = $jetpack_blog_id;
$data['jetpack_version'] = $jp_version;
$data['wc_version'] = $wc_version;
$data['wp_version'] = get_bloginfo('version');
$event_type = self::$product_name . '_' . $event_type;
$this->log('Tracked the following event: ' . $event_type);
return jetpack_tracks_record_event($user, $event_type, $data);
}
示例4: bulky_woocommerce_cart_shipping_packages
public static function bulky_woocommerce_cart_shipping_packages($packages)
{
// Reset the packages
$packages = array();
// Bulky items
$free_items = array();
$regular_items = array();
// Sort free from others
foreach (WC()->cart->get_cart() as $item) {
if ($item['data']->needs_shipping()) {
if ($item['data']->get_shipping_class() == 'free' || $item['data']->get_shipping_class() == '') {
$free_items[] = $item;
} else {
$regular_items[] = $item;
}
}
}
// Put inside packages
if ($free_items) {
$packages[] = array('ship_via' => array('free_shipping'), 'contents' => $free_items, 'contents_cost' => array_sum(wp_list_pluck($free_items, 'line_total')), 'applied_coupons' => WC()->cart->applied_coupons, 'destination' => array('country' => WC()->customer->get_shipping_country(), 'state' => WC()->customer->get_shipping_state(), 'postcode' => WC()->customer->get_shipping_postcode(), 'city' => WC()->customer->get_shipping_city(), 'address' => WC()->customer->get_shipping_address(), 'address_2' => WC()->customer->get_shipping_address_2()));
}
if ($regular_items) {
$packages[] = array('ship_via' => array('flat_rate'), 'contents' => $regular_items, 'contents_cost' => array_sum(wp_list_pluck($regular_items, 'line_total')), 'applied_coupons' => WC()->cart->applied_coupons, 'destination' => array('country' => WC()->customer->get_shipping_country(), 'state' => WC()->customer->get_shipping_state(), 'postcode' => WC()->customer->get_shipping_postcode(), 'city' => WC()->customer->get_shipping_city(), 'address' => WC()->customer->get_shipping_address(), 'address_2' => WC()->customer->get_shipping_address_2()));
}
return $packages;
}
开发者ID:essenmitsosse,项目名称:woocommerce-bulk-shipping-seperated,代码行数:26,代码来源:woocommerce-bulk-shipping-seperated.php
示例5: widget
/**
* widget function.
*
* @see WP_Widget
* @access public
* @param array $args
* @param array $instance
* @return void
*/
public function widget($args, $instance)
{
if ($this->get_cached_widget($args)) {
return;
}
ob_start();
extract($args);
$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
$number = absint($instance['number']);
add_filter('posts_clauses', array(WC()->query, 'order_by_rating_post_clauses'));
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product');
$query_args['meta_query'] = WC()->query->get_meta_query();
$r = new WP_Query($query_args);
if ($r->have_posts()) {
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
}
echo '<ul class="product_list_widget">';
while ($r->have_posts()) {
$r->the_post();
wc_get_template('content-widget-product.php', array('show_rating' => true));
}
echo '</ul>';
echo $after_widget;
}
remove_filter('posts_clauses', array(WC()->query, 'order_by_rating_post_clauses'));
wp_reset_postdata();
$content = ob_get_clean();
echo $content;
$this->cache_widget($args, $content);
}
示例6: test_is_customer_outside_base
/**
* Test the is_customer_outside_base method
*/
public function test_is_customer_outside_base()
{
// Get the original settings for the session and the WooCommerce options
$original_chosen_shipping_methods = \WC_Helper_Customer::get_chosen_shipping_methods();
$original_tax_based_on = \WC_Helper_Customer::get_tax_based_on();
$original_customer_details = \WC_Helper_Customer::get_customer_details();
$customer = \WC_Helper_Customer::create_mock_customer();
// Create dummy product, and add the product to the cart.
$product = \WC_Helper_Product::create_simple_product();
WC()->cart->add_to_cart($product->id, 1);
// Customer is going with the Local Pickup option, and the store calculates tax based on the store location.
\WC_Helper_Customer::set_chosen_shipping_methods(array('local_pickup'));
\WC_Helper_Customer::set_tax_based_on('base');
$this->assertEquals($customer->is_customer_outside_base(), false);
// Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address.
\WC_Helper_Customer::set_chosen_shipping_methods(array('local_pickup'));
\WC_Helper_Customer::set_tax_based_on('billing');
$this->assertEquals($customer->is_customer_outside_base(), false);
// Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address.
\WC_Helper_Customer::set_chosen_shipping_methods(array('free_shipping'));
\WC_Helper_Customer::set_tax_based_on('billing');
$this->assertEquals($customer->is_customer_outside_base(), true);
// Customer is going with the Free Shipping option, and the store calculates tax based on the store base location.
\WC_Helper_Customer::set_chosen_shipping_methods(array('free_shipping'));
\WC_Helper_Customer::set_tax_based_on('base');
$this->assertEquals($customer->is_customer_outside_base(), false);
//Now reset the settings back to the way they were before this test
\WC_Helper_Customer::set_chosen_shipping_methods($original_chosen_shipping_methods);
\WC_Helper_Customer::set_tax_based_on($original_tax_based_on);
\WC_Helper_Customer::set_customer_details($original_customer_details);
// Clean up the cart
WC()->cart->empty_cart();
// Clean up product
\WC_Helper_Product::delete_product($product->id);
}
示例7: add_position_to_session_callback
function add_position_to_session_callback()
{
WC()->session->set('latitude', esc_attr($_POST['lat']));
WC()->session->set('longitude', esc_attr($_POST['lng']));
echo 'done';
die;
}
示例8: __construct
/**
* Constructor
*/
public function __construct()
{
// Init settings
$this->init_form_fields();
$this->init_settings();
// Save settings hook
add_action('woocommerce_update_options_email_' . $this->id, array($this, 'process_admin_options'));
// Default template base if not declared in child constructor
if (is_null($this->template_base)) {
$this->template_base = WC()->plugin_path() . '/templates/';
}
// Settings
$this->heading = $this->get_option('heading', $this->heading);
$this->subject = $this->get_option('subject', $this->subject);
$this->email_type = $this->get_option('email_type');
$this->enabled = $this->get_option('enabled');
// Find/replace
$this->find['blogname'] = '{blogname}';
$this->find['site-title'] = '{site_title}';
$this->replace['blogname'] = $this->get_blogname();
$this->replace['site-title'] = $this->get_blogname();
// For multipart messages
add_filter('phpmailer_init', array($this, 'handle_multipart'));
// For default inline styles
add_filter('woocommerce_email_style_inline_tags', array($this, 'style_inline_tags'));
add_filter('woocommerce_email_style_inline_h1_tag', array($this, 'style_inline_h1_tag'));
add_filter('woocommerce_email_style_inline_h2_tag', array($this, 'style_inline_h2_tag'));
add_filter('woocommerce_email_style_inline_h3_tag', array($this, 'style_inline_h3_tag'));
add_filter('woocommerce_email_style_inline_a_tag', array($this, 'style_inline_a_tag'));
add_filter('woocommerce_email_style_inline_img_tag', array($this, 'style_inline_img_tag'));
}
示例9: get_output
public function get_output($placeholder = true, $placeholder_src = 'default')
{
$picker = '';
$href = apply_filters('woocommerce_swatches_get_swatch_href', '#', $this);
$anchor_class = apply_filters('woocommerce_swatches_get_swatch_anchor_css_class', 'swatch-anchor', $this);
$image_class = apply_filters('woocommerce_swatches_get_swatch_image_css_class', 'swatch-img', $this);
$image_alt = apply_filters('woocommerce_swatches_get_swatch_image_alt', 'thumbnail', $this);
if ($this->type == 'photo' || $this->type == 'image') {
$picker .= '<a href="' . $href . '" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" title="' . esc_attr($this->term_label) . '" class="' . $anchor_class . '">';
$picker .= '<img src="' . apply_filters('woocommerce_swatches_get_swatch_image', $this->thumbnail_src, $this->term_slug, $this->taxonomy_slug, $this) . '" alt="' . $image_alt . '" class="wp-post-image swatch-photo' . $this->meta_key() . ' ' . $image_class . '" width="' . $this->width . '" height="' . $this->height . '"/>';
$picker .= '</a>';
} elseif ($this->type == 'color') {
$picker .= '<a href="' . $href . '" style="text-indent:-9999px;width:' . $this->width . 'px;height:' . $this->height . 'px;background-color:' . apply_filters('woocommerce_swatches_get_swatch_color', $this->color, $this->term_slug, $this->taxonomy_slug, $this) . ';" title="' . $this->term_label . '" class="' . $anchor_class . '">' . $this->term_label . '</a>';
} elseif ($placeholder) {
if ($placeholder_src == 'default') {
$src = apply_filters('woocommerce_placeholder_img_src', WC()->plugin_url() . '/assets/images/placeholder.png');
} else {
$src = $placeholder_src;
}
$picker .= '<a href="' . $href . '" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" title="' . esc_attr($this->term_label) . '" class="' . $anchor_class . '">';
$picker .= '<img src="' . $src . '" alt="' . $image_alt . '" class="wp-post-image swatch-photo' . $this->meta_key() . ' ' . $image_class . '" width="' . $this->width . '" height="' . $this->height . '"/>';
$picker .= '</a>';
} else {
return '';
}
$out = '<div class="select-option swatch-wrapper' . ($this->selected ? ' selected' : '') . '" data-attribute="' . esc_attr($this->taxonomy_slug) . '" data-value="' . esc_attr($this->term_slug) . '">';
$out .= apply_filters('woocommerce_swatches_picker_html', $picker, $this);
$out .= '</div>';
return $out;
}
示例10: wc_get_account_menu_items
/**
* Get My Account menu items.
*
* @since 2.6.0
* @return array
*/
function wc_get_account_menu_items()
{
$endpoints = array('orders' => get_option('woocommerce_myaccount_orders_endpoint', 'orders'), 'downloads' => get_option('woocommerce_myaccount_downloads_endpoint', 'downloads'), 'edit-address' => get_option('woocommerce_myaccount_edit_address_endpoint', 'edit-address'), 'payment-methods' => get_option('woocommerce_myaccount_payment_methods_endpoint', 'payment-methods'), 'edit-account' => get_option('woocommerce_myaccount_edit_account_endpoint', 'edit-account'), 'customer-logout' => get_option('woocommerce_logout_endpoint', 'customer-logout'));
$items = array('dashboard' => __('Dashboard', 'woocommerce'), 'orders' => __('Orders', 'woocommerce'), 'downloads' => __('Downloads', 'woocommerce'), 'edit-address' => __('Addresses', 'woocommerce'), 'payment-methods' => __('Payment methods', 'woocommerce'), 'edit-account' => __('Account details', 'woocommerce'), 'customer-logout' => __('Logout', 'woocommerce'));
// Remove missing endpoints.
foreach ($endpoints as $endpoint_id => $endpoint) {
if (empty($endpoint)) {
unset($items[$endpoint_id]);
}
}
// Check if payment gateways support add new payment methods.
if (isset($items['payment-methods'])) {
$support_payment_methods = false;
foreach (WC()->payment_gateways->get_available_payment_gateways() as $gateway) {
if ($gateway->supports('add_payment_method') || $gateway->supports('tokenization')) {
$support_payment_methods = true;
break;
}
}
if (!$support_payment_methods) {
unset($items['payment-methods']);
}
}
return apply_filters('woocommerce_account_menu_items', $items);
}
示例11: cart_get_global_stock_quantities
/**
* Gets the total number of tickets requested *per event* (of course, we're only
* interested in events that maintain global stock where tickets for those events
* that utilize global stock are in the cart).
*
* @return array
*/
protected function cart_get_global_stock_quantities()
{
$cart = WC()->cart;
$current = $cart->get_cart_item_quantities();
$woo_tickets = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance();
$quantities = array();
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
$event = $woo_tickets->get_event_for_ticket($product->id);
// Skip non-tickets or tickets that do not utilize global stock
if (!$event || !$woo_tickets->uses_global_stock($event->ID) || !$product->managing_stock()) {
continue;
}
$tickets = $this->get_event_tickets($event->ID);
if (!isset($tickets[$product->id])) {
continue;
}
// We only need to accumulate the stock quantities of tickets using *global* stock
if (Tribe__Tickets__Global_Stock::OWN_STOCK_MODE === $tickets[$product->id]->global_stock_mode()) {
continue;
}
// Make sure ticket caps haven't been exceeded
if (Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE === $tickets[$product->id]->global_stock_mode()) {
if ($current[$product->id] > $tickets[$product->id]->global_stock_cap()) {
$this->cart_flag_capped_stock_error($product->id);
}
}
if (!isset($quantities[$event->ID])) {
$quantities[$event->ID] = 0;
}
$quantities[$event->ID] += $current[$product->id];
}
return $quantities;
}
示例12: calclateShippingForPackage
private function calclateShippingForPackage($package, $package_key)
{
//sanity check. Just to be sure
if (!$this->isEnabled() || empty($package)) {
return false;
}
// Check if we need to recalculate shipping for this package
$package_hash = 'wc_ship_' . md5(json_encode($package) . Cache::getItem('shipping'));
$status_options = get_option('woocommerce_status_options', array());
$session_key = 'shipping_for_package_' . $package_key;
$stored_rates = WC()->session->get($session_key);
if (!is_array($stored_rates) || $package_hash !== $stored_rates['package_hash'] || !empty($status_options['shipping_debug_mode'])) {
// Calculate shipping method rates
$package['rates'] = array();
foreach ($this->loadShippingMethods($package) as $shipping_method) {
// Shipping instances need an ID
$package['rates'] = $package['rates'] + Event::trigger('getRatesForPackage', array('shipping_method' => $shipping_method), 'filter');
}
$package['rates'] = apply_filters('woocommerce_package_rates', $package['rates'], $package);
// Store in session to avoid recalculation
WC()->session->set($session_key, array('package_hash' => $package_hash, 'rates' => $package['rates']));
} else {
$package['rates'] = $stored_rates['rates'];
}
return $package;
}
示例13: send_next_payment_date_email
/**
* Init the mailer and call for the skipped next payment email notification hook.
*
* @since 1.0.0
* @access public
* @static
* @param $order WC_Order
* @param $subscription WC_Subscription
*/
public static function send_next_payment_date_email($order, $subscription)
{
WC()->mailer();
if ($subscription->has_status(array('active')) && 'true' !== get_post_meta($subscription->id, '_skipped_next_payment_email_sent', true)) {
do_action('subscription_skipped_notification', $order, $subscription);
}
}
示例14: filter_available_payment_gateways_per_category
/**
* filter_available_payment_gateways_per_category.
*/
function filter_available_payment_gateways_per_category($available_gateways)
{
//if ( ! is_checkout() ) return $available_gateways;
foreach ($available_gateways as $gateway_id => $gateway) {
$categories_in = get_option('wcj_gateways_per_category_' . $gateway_id);
if (!empty($categories_in)) {
$do_skip = true;
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$product_categories = get_the_terms($values['product_id'], 'product_cat');
if (empty($product_categories)) {
continue;
}
// ... to next product in the cart
foreach ($product_categories as $product_category) {
if (in_array($product_category->term_id, $categories_in)) {
// Current gateway is OK, breaking to check next gateway (no need to check other categories of the product)
$do_skip = false;
break;
}
}
if (!$do_skip) {
// Current gateway is OK, breaking to check next gateway (no need to check other products in the cart)
break;
}
}
if ($do_skip) {
// Skip (i.e. hide/unset) current gateway - no products of needed categories found in the cart
unset($available_gateways[$gateway_id]);
}
}
}
return $available_gateways;
}
示例15: rena_mobile_menu_button
/**
* RenaRomano Mobile Menu Button
* Outputs the Primary Navigation Menu Button for the Pushy Menu
*
* @author Jordan Pakrosnis
*/
function rena_mobile_menu_button()
{
// Output Button
?>
<div id="rena-mobile-topnav">
<a class="cart-contents" href="<?php
echo WC()->cart->get_cart_url();
?>
" title="<?php
_e('View your shopping cart');
?>
">
<i class="fa fa-shopping-cart"></i>
<?php
if (WC()->cart->cart_contents_count > 0) {
echo '<span>' . WC()->cart->cart_contents_count . '</span>';
}
?>
</a><button type="button" class="menu-btn">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div><!-- /#rena-mobile-topnav -->
<?php
}