PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / class-wc-product-external.php
class-wc-product-external.php
198 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * External Product
4 *
5 * External products cannot be bought; they link offsite. Extends simple products.
6 *
7 * @package WooCommerce\Classes\Products
8 * @version 3.0.0
9 */
10
11 use Automattic\WooCommerce\Enums\ProductStockStatus;
12 use Automattic\WooCommerce\Enums\ProductType;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Product external class.
18 */
19 class WC_Product_External extends WC_Product {
20
21 /**
22 * Stores product data.
23 *
24 * @var array
25 */
26 protected $extra_data = array(
27 'product_url' => '',
28 'button_text' => '',
29 );
30
31 /**
32 * Get internal type.
33 *
34 * @return string
35 */
36 public function get_type() {
37 return ProductType::EXTERNAL;
38 }
39
40 /*
41 |--------------------------------------------------------------------------
42 | Getters
43 |--------------------------------------------------------------------------
44 |
45 | Methods for getting data from the product object.
46 */
47
48 /**
49 * Get product url.
50 *
51 * @param string $context What the value is for. Valid values are 'view' and 'edit'.
52 * @return string
53 */
54 public function get_product_url( $context = 'view' ) {
55 return esc_url_raw( $this->get_prop( 'product_url', $context ) );
56 }
57
58 /**
59 * Get button text.
60 *
61 * @param string $context What the value is for. Valid values are 'view' and 'edit'.
62 * @return string
63 */
64 public function get_button_text( $context = 'view' ) {
65 return $this->get_prop( 'button_text', $context );
66 }
67
68 /*
69 |--------------------------------------------------------------------------
70 | Setters
71 |--------------------------------------------------------------------------
72 |
73 | Functions for setting product data. These should not update anything in the
74 | database itself and should only change what is stored in the class
75 | object.
76 */
77
78 /**
79 * Set product URL.
80 *
81 * @since 3.0.0
82 * @param string $product_url Product URL.
83 */
84 public function set_product_url( $product_url ) {
85 $this->set_prop( 'product_url', htmlspecialchars_decode( $product_url ) );
86 }
87
88 /**
89 * Set button text.
90 *
91 * @since 3.0.0
92 * @param string $button_text Button text.
93 */
94 public function set_button_text( $button_text ) {
95 $this->set_prop( 'button_text', $button_text );
96 }
97
98 /**
99 * External products cannot be stock managed.
100 *
101 * @since 3.0.0
102 * @param bool $manage_stock If manage stock.
103 */
104 public function set_manage_stock( $manage_stock ) {
105 $this->set_prop( 'manage_stock', false );
106
107 if ( true === $manage_stock ) {
108 $this->error( 'product_external_invalid_manage_stock', __( 'External products cannot be stock managed.', 'woocommerce' ) );
109 }
110 }
111
112 /**
113 * External products cannot be stock managed.
114 *
115 * @since 3.0.0
116 *
117 * @param string $stock_status Stock status.
118 */
119 public function set_stock_status( $stock_status = '' ) {
120 $this->set_prop( 'stock_status', ProductStockStatus::IN_STOCK );
121
122 if ( ProductStockStatus::IN_STOCK !== $stock_status ) {
123 $this->error( 'product_external_invalid_stock_status', __( 'External products cannot be stock managed.', 'woocommerce' ) );
124 }
125 }
126
127 /**
128 * External products cannot be backordered.
129 *
130 * @since 3.0.0
131 * @param string $backorders Options: 'yes', 'no' or 'notify'.
132 */
133 public function set_backorders( $backorders ) {
134 $this->set_prop( 'backorders', 'no' );
135
136 if ( 'no' !== $backorders ) {
137 $this->error( 'product_external_invalid_backorders', __( 'External products cannot be backordered.', 'woocommerce' ) );
138 }
139 }
140
141 /*
142 |--------------------------------------------------------------------------
143 | Other Actions
144 |--------------------------------------------------------------------------
145 */
146
147 /**
148 * Returns false if the product cannot be bought.
149 *
150 * @access public
151 * @return bool
152 */
153 public function is_purchasable() {
154 return apply_filters( 'woocommerce_is_purchasable', false, $this );
155 }
156
157 /**
158 * Get the add to url used mainly in loops.
159 *
160 * @access public
161 * @return string
162 */
163 public function add_to_cart_url() {
164 return apply_filters( 'woocommerce_product_add_to_cart_url', $this->get_product_url(), $this );
165 }
166
167 /**
168 * Get the add to cart button text for the single page.
169 *
170 * @access public
171 * @return string
172 */
173 public function single_add_to_cart_text() {
174 return apply_filters( 'woocommerce_product_single_add_to_cart_text', $this->get_button_text() ? $this->get_button_text() : _x( 'Buy product', 'placeholder', 'woocommerce' ), $this );
175 }
176
177 /**
178 * Get the add to cart button text.
179 *
180 * @access public
181 * @return string
182 */
183 public function add_to_cart_text() {
184 return apply_filters( 'woocommerce_product_add_to_cart_text', $this->get_button_text() ? $this->get_button_text() : _x( 'Buy product', 'placeholder', 'woocommerce' ), $this );
185 }
186
187 /**
188 * Get the add to cart button text description - used in aria tags.
189 *
190 * @since 3.3.0
191 * @return string
192 */
193 public function add_to_cart_description() {
194 /* translators: %s: Product title */
195 return apply_filters( 'woocommerce_product_add_to_cart_description', $this->get_button_text() ? $this->get_button_text() : sprintf( __( 'Buy &ldquo;%s&rdquo;', 'woocommerce' ), $this->get_name() ), $this );
196 }
197 }
198